@@ -43,27 +43,32 @@ object CheckCaptures:
4343 sym
4444 end Pre
4545
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+
4654 /** 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
5359 */
5460 case class Env (
5561 owner : Symbol ,
56- nestedInOwner : Boolean ,
62+ kind : EnvKind ,
5763 captured : CaptureSet ,
58- isBoxed : Boolean ,
5964 outer0 : Env | Null ):
6065
6166 def outer = outer0.nn
6267
6368 def isOutermost = outer0 == null
6469
6570 /** If an environment is open it tracks free references */
66- def isOpen = ! captured.isAlwaysEmpty && ! isBoxed
71+ def isOpen = ! captured.isAlwaysEmpty && kind != EnvKind . Boxed
6772 end Env
6873
6974 /** Similar normal substParams, but this is an approximating type map that
@@ -226,7 +231,7 @@ class CheckCaptures extends Recheck, SymTransformer:
226231 report.error(em " $header included in allowed capture set ${res.blocking}" , pos)
227232
228233 /** 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 )
230235
231236 private val myCapturedVars : util.EqHashMap [Symbol , CaptureSet ] = EqHashMap ()
232237
@@ -270,7 +275,7 @@ class CheckCaptures extends Recheck, SymTransformer:
270275 if ! cs.isAlwaysEmpty then
271276 forallOuterEnvsUpTo(ctx.owner.topLevelClass): env =>
272277 def isVisibleFromEnv (sym : Symbol ) =
273- (env.nestedInOwner || env.owner != sym)
278+ (env.kind == EnvKind . NestedInOwner || env.owner != sym)
274279 && env.owner.isContainedIn(sym)
275280 val included = cs.filter:
276281 case ref : TermRef => isVisibleFromEnv(ref.symbol.owner)
@@ -512,7 +517,7 @@ class CheckCaptures extends Recheck, SymTransformer:
512517 if ! Synthetics .isExcluded(sym) then
513518 val saved = curEnv
514519 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)
516521 try super .recheckDefDef(tree, sym)
517522 finally
518523 interpolateVarsIn(tree.tpt)
@@ -530,7 +535,7 @@ class CheckCaptures extends Recheck, SymTransformer:
530535 val localSet = capturedVars(cls)
531536 for parent <- impl.parents do // (1)
532537 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)
534539 try
535540 val thisSet = cls.classInfo.selfType.captureSet.withDescription(i " of the self type of $cls" )
536541 checkSubset(localSet, thisSet, tree.srcPos) // (2)
@@ -595,7 +600,7 @@ class CheckCaptures extends Recheck, SymTransformer:
595600
596601 tree match
597602 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)
599604 case _ =>
600605
601606 try super .recheck(tree, pt)
@@ -729,7 +734,7 @@ class CheckCaptures extends Recheck, SymTransformer:
729734 covariant : Boolean , boxed : Boolean ,
730735 reconstruct : (List [Type ], Type ) => Type ): (Type , CaptureSet ) =
731736 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)
733738
734739 try
735740 val (eargs, eres) = expected.dealias.stripCapturing match
@@ -756,7 +761,7 @@ class CheckCaptures extends Recheck, SymTransformer:
756761 covariant : Boolean , boxed : Boolean ,
757762 reconstruct : Type => Type ): (Type , CaptureSet ) =
758763 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)
760765
761766 try
762767 val eres = expected.dealias.stripCapturing match
@@ -888,7 +893,7 @@ class CheckCaptures extends Recheck, SymTransformer:
888893 val actual1 =
889894 val saved = curEnv
890895 try
891- curEnv = Env (clazz, nestedInOwner = true , capturedVars(clazz), isBoxed = false , outer0 = curEnv)
896+ curEnv = Env (clazz, EnvKind . NestedInOwner , capturedVars(clazz), outer0 = curEnv)
892897 val adapted = adaptBoxed(actual, expected1, srcPos, alwaysConst = true )
893898 actual match
894899 case _ : MethodType =>
0 commit comments