Skip to content

Commit 1d92033

Browse files
committed
Refactor narrowCaps
1 parent dc8b8a0 commit 1d92033

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
@@ -469,29 +469,23 @@ extension (tp: Type)
469469
end CheckContraCaps
470470

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

497491
ref match

0 commit comments

Comments
 (0)