@@ -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)
@@ -512,7 +517,7 @@ class CheckCaptures extends Recheck, SymTransformer:
512
517
if ! Synthetics .isExcluded(sym) then
513
518
val saved = curEnv
514
519
val localSet = capturedVars(sym)
515
- if ! localSet.isAlwaysEmpty then curEnv = Env (sym, nestedInOwner = false , localSet, isBoxed = false , curEnv)
520
+ if ! localSet.isAlwaysEmpty then curEnv = Env (sym, EnvKind . Regular , localSet, curEnv)
516
521
try super .recheckDefDef(tree, sym)
517
522
finally
518
523
interpolateVarsIn(tree.tpt)
@@ -530,7 +535,7 @@ class CheckCaptures extends Recheck, SymTransformer:
530
535
val localSet = capturedVars(cls)
531
536
for parent <- impl.parents do // (1)
532
537
checkSubset(capturedVars(parent.tpe.classSymbol), localSet, parent.srcPos)
533
- if ! localSet.isAlwaysEmpty then curEnv = Env (cls, nestedInOwner = false , localSet, isBoxed = false , curEnv)
538
+ if ! localSet.isAlwaysEmpty then curEnv = Env (cls, EnvKind . Regular , localSet, curEnv)
534
539
try
535
540
val thisSet = cls.classInfo.selfType.captureSet.withDescription(i " of the self type of $cls" )
536
541
checkSubset(localSet, thisSet, tree.srcPos) // (2)
@@ -595,7 +600,7 @@ class CheckCaptures extends Recheck, SymTransformer:
595
600
596
601
tree match
597
602
case _ : RefTree | closureDef(_) =>
598
- curEnv = Env (curEnv.owner, nestedInOwner = false , CaptureSet .Var (), isBoxed = true , curEnv)
603
+ curEnv = Env (curEnv.owner, EnvKind . Boxed , CaptureSet .Var (), curEnv)
599
604
case _ =>
600
605
601
606
try super .recheck(tree, pt)
@@ -729,7 +734,7 @@ class CheckCaptures extends Recheck, SymTransformer:
729
734
covariant : Boolean , boxed : Boolean ,
730
735
reconstruct : (List [Type ], Type ) => Type ): (Type , CaptureSet ) =
731
736
val saved = curEnv
732
- curEnv = Env (curEnv.owner, nestedInOwner = true , CaptureSet .Var (), isBoxed = false , if boxed then null else curEnv)
737
+ curEnv = Env (curEnv.owner, EnvKind . NestedInOwner , CaptureSet .Var (), if boxed then null else curEnv)
733
738
734
739
try
735
740
val (eargs, eres) = expected.dealias.stripCapturing match
@@ -756,7 +761,7 @@ class CheckCaptures extends Recheck, SymTransformer:
756
761
covariant : Boolean , boxed : Boolean ,
757
762
reconstruct : Type => Type ): (Type , CaptureSet ) =
758
763
val saved = curEnv
759
- curEnv = Env (curEnv.owner, nestedInOwner = true , CaptureSet .Var (), isBoxed = false , if boxed then null else curEnv)
764
+ curEnv = Env (curEnv.owner, EnvKind . NestedInOwner , CaptureSet .Var (), if boxed then null else curEnv)
760
765
761
766
try
762
767
val eres = expected.dealias.stripCapturing match
@@ -888,7 +893,7 @@ class CheckCaptures extends Recheck, SymTransformer:
888
893
val actual1 =
889
894
val saved = curEnv
890
895
try
891
- curEnv = Env (clazz, nestedInOwner = true , capturedVars(clazz), isBoxed = false , outer0 = curEnv)
896
+ curEnv = Env (clazz, EnvKind . NestedInOwner , capturedVars(clazz), outer0 = curEnv)
892
897
val adapted = adaptBoxed(actual, expected1, srcPos, alwaysConst = true )
893
898
actual match
894
899
case _ : MethodType =>
0 commit comments