Skip to content

Commit e130c05

Browse files
committed
Refactor narrowCaps
1 parent 5fc498a commit e130c05

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

compiler/src/dotty/tools/dotc/cc/CaptureOps.scala

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -468,29 +468,23 @@ extension (tp: Type)
468468
end CheckContraCaps
469469

470470
object narrowCaps extends TypeMap:
471-
/** Has the variance been flipped at this point? */
472-
private var isFlipped: Boolean = false
473-
474471
def apply(t: Type) =
475-
val saved = isFlipped
476-
try
477-
if variance <= 0 then isFlipped = true
478-
t.dealias match
479-
case t1 @ CapturingType(p, cs) if cs.isUniversal && !isFlipped =>
480-
t1.derivedCapturingType(apply(p), ref.reach.singletonCaptureSet)
481-
case t1 @ FunctionOrMethod(args, res @ Existential(_, _))
482-
if args.forall(_.isAlwaysPure) =>
483-
// Also map existentials in results to reach capabilities if all
484-
// preceding arguments are known to be always pure
485-
apply(t1.derivedFunctionOrMethod(args, Existential.toCap(res)))
486-
case Existential(_, _) =>
487-
t
488-
case _ => t match
489-
case t @ CapturingType(p, cs) =>
490-
t.derivedCapturingType(apply(p), cs) // don't map capture set variables
491-
case t =>
492-
mapOver(t)
493-
finally isFlipped = saved
472+
if variance <= 0 then t
473+
else t.dealiasKeepAnnots match
474+
case t @ CapturingType(p, cs) if cs.isUniversal =>
475+
t.derivedCapturingType(apply(p), ref.reach.singletonCaptureSet)
476+
case t @ AnnotatedType(parent, ann) =>
477+
// Don't map annotations, which includes capture sets
478+
t.derivedAnnotatedType(this(parent), ann)
479+
case t @ FunctionOrMethod(args, res @ Existential(_, _))
480+
if args.forall(_.isAlwaysPure) =>
481+
// Also map existentials in results to reach capabilities if all
482+
// preceding arguments are known to be always pure
483+
apply(t.derivedFunctionOrMethod(args, Existential.toCap(res)))
484+
case Existential(_, _) =>
485+
t
486+
case _ =>
487+
mapOver(t)
494488
end narrowCaps
495489

496490
ref match

0 commit comments

Comments
 (0)