@@ -43,27 +43,32 @@ object CheckCaptures:
43
43
sym
44
44
end Pre
45
45
46
+ enum EnvKind :
47
+ case Regular // normal case
48
+ case NestedInOwner // environment is a temporary one nested in the owner's environment,
49
+ // and does not have a different actual owner symbol
50
+ // (this happens when doing box adaptation).
51
+ case ClosureResult // environment is for the result of a closure
52
+ case Boxed // envrionment is inside a box (in which case references are not counted)
53
+
46
54
/** A class describing environments.
47
- * @param owner the current owner
48
- * @param nestedInOwner true if the environment is a temporary one nested in the owner's environment,
49
- * and does not have a different actual owner symbol (this happens when doing box adaptation).
50
- * @param captured the caputure set containing all references to tracked free variables outside of boxes
51
- * @param isBoxed true if the environment is inside a box (in which case references are not counted)
52
- * @param outer0 the next enclosing environment
55
+ * @param owner the current owner
56
+ * @param kind the environment's kind
57
+ * @param captured the caputure set containing all references to tracked free variables outside of boxes
58
+ * @param outer0 the next enclosing environment
53
59
*/
54
60
case class Env (
55
61
owner : Symbol ,
56
- nestedInOwner : Boolean ,
62
+ kind : EnvKind ,
57
63
captured : CaptureSet ,
58
- isBoxed : Boolean ,
59
64
outer0 : Env | Null ):
60
65
61
66
def outer = outer0.nn
62
67
63
68
def isOutermost = outer0 == null
64
69
65
70
/** If an environment is open it tracks free references */
66
- def isOpen = ! captured.isAlwaysEmpty && ! isBoxed
71
+ def isOpen = ! captured.isAlwaysEmpty && kind != EnvKind . Boxed
67
72
end Env
68
73
69
74
/** Similar normal substParams, but this is an approximating type map that
@@ -226,7 +231,7 @@ class CheckCaptures extends Recheck, SymTransformer:
226
231
report.error(em " $header included in allowed capture set ${res.blocking}" , pos)
227
232
228
233
/** The current environment */
229
- private var curEnv : Env = Env (NoSymbol , nestedInOwner = false , CaptureSet .empty, isBoxed = false , null )
234
+ private var curEnv : Env = Env (NoSymbol , EnvKind . Regular , CaptureSet .empty, null )
230
235
231
236
private val myCapturedVars : util.EqHashMap [Symbol , CaptureSet ] = EqHashMap ()
232
237
@@ -270,7 +275,7 @@ class CheckCaptures extends Recheck, SymTransformer:
270
275
if ! cs.isAlwaysEmpty then
271
276
forallOuterEnvsUpTo(ctx.owner.topLevelClass): env =>
272
277
def isVisibleFromEnv (sym : Symbol ) =
273
- (env.nestedInOwner || env.owner != sym)
278
+ (env.kind == EnvKind . NestedInOwner || env.owner != sym)
274
279
&& env.owner.isContainedIn(sym)
275
280
val included = cs.filter:
276
281
case ref : TermRef => isVisibleFromEnv(ref.symbol.owner)
@@ -549,7 +554,7 @@ class CheckCaptures extends Recheck, SymTransformer:
549
554
if ! Synthetics .isExcluded(sym) then
550
555
val saved = curEnv
551
556
val localSet = capturedVars(sym)
552
- if ! localSet.isAlwaysEmpty then curEnv = Env (sym, nestedInOwner = false , localSet, isBoxed = false , curEnv)
557
+ if ! localSet.isAlwaysEmpty then curEnv = Env (sym, EnvKind . Regular , localSet, curEnv)
553
558
try super .recheckDefDef(tree, sym)
554
559
finally
555
560
interpolateVarsIn(tree.tpt)
@@ -567,7 +572,7 @@ class CheckCaptures extends Recheck, SymTransformer:
567
572
val localSet = capturedVars(cls)
568
573
for parent <- impl.parents do // (1)
569
574
checkSubset(capturedVars(parent.tpe.classSymbol), localSet, parent.srcPos)
570
- if ! localSet.isAlwaysEmpty then curEnv = Env (cls, nestedInOwner = false , localSet, isBoxed = false , curEnv)
575
+ if ! localSet.isAlwaysEmpty then curEnv = Env (cls, EnvKind . Regular , localSet, curEnv)
571
576
try
572
577
val thisSet = cls.classInfo.selfType.captureSet.withDescription(i " of the self type of $cls" )
573
578
checkSubset(localSet, thisSet, tree.srcPos) // (2)
@@ -632,7 +637,7 @@ class CheckCaptures extends Recheck, SymTransformer:
632
637
633
638
tree match
634
639
case _ : RefTree | closureDef(_) =>
635
- curEnv = Env (curEnv.owner, nestedInOwner = false , CaptureSet .Var (), isBoxed = true , curEnv)
640
+ curEnv = Env (curEnv.owner, EnvKind . Boxed , CaptureSet .Var (), curEnv)
636
641
case _ =>
637
642
638
643
try super .recheck(tree, pt)
@@ -768,7 +773,7 @@ class CheckCaptures extends Recheck, SymTransformer:
768
773
covariant : Boolean , boxed : Boolean ,
769
774
reconstruct : (List [Type ], Type ) => Type ): (Type , CaptureSet ) =
770
775
val saved = curEnv
771
- curEnv = Env (curEnv.owner, nestedInOwner = true , CaptureSet .Var (), isBoxed = false , if boxed then null else curEnv)
776
+ curEnv = Env (curEnv.owner, EnvKind . NestedInOwner , CaptureSet .Var (), if boxed then null else curEnv)
772
777
773
778
try
774
779
val (eargs, eres) = expected.dealias.stripCapturing match
@@ -795,7 +800,7 @@ class CheckCaptures extends Recheck, SymTransformer:
795
800
covariant : Boolean , boxed : Boolean ,
796
801
reconstruct : Type => Type ): (Type , CaptureSet ) =
797
802
val saved = curEnv
798
- curEnv = Env (curEnv.owner, nestedInOwner = true , CaptureSet .Var (), isBoxed = false , if boxed then null else curEnv)
803
+ curEnv = Env (curEnv.owner, EnvKind . NestedInOwner , CaptureSet .Var (), if boxed then null else curEnv)
799
804
800
805
try
801
806
val eres = expected.dealias.stripCapturing match
@@ -927,7 +932,7 @@ class CheckCaptures extends Recheck, SymTransformer:
927
932
val actual1 =
928
933
val saved = curEnv
929
934
try
930
- curEnv = Env (clazz, nestedInOwner = true , capturedVars(clazz), isBoxed = false , outer0 = curEnv)
935
+ curEnv = Env (clazz, EnvKind . NestedInOwner , capturedVars(clazz), outer0 = curEnv)
931
936
val adapted = adaptBoxed(actual, expected1, srcPos, alwaysConst = true )
932
937
actual match
933
938
case _ : MethodType =>
0 commit comments