Skip to content

Commit ef74663

Browse files
committed
Refactoring: Add EnvKind to replace boolean fields in Env
1 parent 89aa1dd commit ef74663

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -549,7 +554,7 @@ class CheckCaptures extends Recheck, SymTransformer:
549554
if !Synthetics.isExcluded(sym) then
550555
val saved = curEnv
551556
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)
553558
try super.recheckDefDef(tree, sym)
554559
finally
555560
interpolateVarsIn(tree.tpt)
@@ -567,7 +572,7 @@ class CheckCaptures extends Recheck, SymTransformer:
567572
val localSet = capturedVars(cls)
568573
for parent <- impl.parents do // (1)
569574
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)
571576
try
572577
val thisSet = cls.classInfo.selfType.captureSet.withDescription(i"of the self type of $cls")
573578
checkSubset(localSet, thisSet, tree.srcPos) // (2)
@@ -632,7 +637,7 @@ class CheckCaptures extends Recheck, SymTransformer:
632637

633638
tree match
634639
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)
636641
case _ =>
637642

638643
try super.recheck(tree, pt)
@@ -768,7 +773,7 @@ class CheckCaptures extends Recheck, SymTransformer:
768773
covariant: Boolean, boxed: Boolean,
769774
reconstruct: (List[Type], Type) => Type): (Type, CaptureSet) =
770775
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)
772777

773778
try
774779
val (eargs, eres) = expected.dealias.stripCapturing match
@@ -795,7 +800,7 @@ class CheckCaptures extends Recheck, SymTransformer:
795800
covariant: Boolean, boxed: Boolean,
796801
reconstruct: Type => Type): (Type, CaptureSet) =
797802
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)
799804

800805
try
801806
val eres = expected.dealias.stripCapturing match
@@ -927,7 +932,7 @@ class CheckCaptures extends Recheck, SymTransformer:
927932
val actual1 =
928933
val saved = curEnv
929934
try
930-
curEnv = Env(clazz, nestedInOwner = true, capturedVars(clazz), isBoxed = false, outer0 = curEnv)
935+
curEnv = Env(clazz, EnvKind.NestedInOwner, capturedVars(clazz), outer0 = curEnv)
931936
val adapted = adaptBoxed(actual, expected1, srcPos, alwaysConst = true)
932937
actual match
933938
case _: MethodType =>

0 commit comments

Comments
 (0)