Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore orphan parameters inside a retains annotation during Ycheck #19684

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import collection.mutable
import ProtoTypes.*
import staging.StagingLevel
import inlines.Inlines.inInlineMethod
import cc.{isRetainsLike, CaptureAnnotation}

import dotty.tools.backend.jvm.DottyBackendInterface.symExtensions

Expand Down Expand Up @@ -162,17 +163,34 @@ object TreeChecker {
*/
def checkNoOrphans(tp0: Type, tree: untpd.Tree = untpd.EmptyTree)(using Context): Type = new TypeMap() {
val definedBinders = new java.util.IdentityHashMap[Type, Any]
private var inRetainingAnnot = false

def insideRetainingAnnot[T](op: => T): T =
val saved = inRetainingAnnot
inRetainingAnnot = true
try op finally inRetainingAnnot = saved

def apply(tp: Type): Type = {
tp match {
case tp: BindingType =>
definedBinders.put(tp, tp)
mapOver(tp)
definedBinders.remove(tp)
case tp: ParamRef =>
assert(definedBinders.get(tp.binder) != null, s"orphan param: ${tp.show}, hash of binder = ${System.identityHashCode(tp.binder)}, tree = ${tree.show}, type = $tp0")
val isValidRef =
definedBinders.get(tp.binder) != null
|| inRetainingAnnot
// Inside a normal @retains annotation, the captured references could be ill-formed. See issue #19661.
// But this is ok since capture checking does not rely on them.
assert(isValidRef, s"orphan param: ${tp.show}, hash of binder = ${System.identityHashCode(tp.binder)}, tree = ${tree.show}, type = $tp0")
case tp: TypeVar =>
assert(tp.isInstantiated, s"Uninstantiated type variable: ${tp.show}, tree = ${tree.show}")
apply(tp.underlying)
case tp @ AnnotatedType(underlying, annot) if annot.symbol.isRetainsLike && !annot.isInstanceOf[CaptureAnnotation] =>
val underlying1 = this(underlying)
val annot1 = insideRetainingAnnot:
annot.mapWith(this)
derivedAnnotatedType(tp, underlying1, annot1)
case _ =>
mapOver(tp)
}
Expand Down
8 changes: 8 additions & 0 deletions tests/pos-custom-args/captures/i19661.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import language.experimental.captureChecking

trait MySet[A]:
def collect[B](pf: PartialFunction[A, B]^): MySet[B]^{this, pf}

class Test:
def f(xs: MySet[Int]) = xs collect { case x => x }
def g(xs: MySet[Int]): MySet[Int] = f(xs)
Loading