@@ -358,9 +358,17 @@ class CheckCaptures extends Recheck, SymTransformer:
358
358
def markFree (cs : CaptureSet , pos : SrcPos )(using Context ): Unit =
359
359
if ! cs.isAlwaysEmpty then
360
360
forallOuterEnvsUpTo(ctx.owner.topLevelClass): env =>
361
- def isVisibleFromEnv (sym : Symbol ) =
362
- (env.kind == EnvKind .NestedInOwner || env.owner != sym)
363
- && env.owner.isContainedIn(sym)
361
+ // Whether a symbol is defined inside the owner of the environment?
362
+ inline def isContainedInEnv (sym : Symbol ) =
363
+ if env.kind == EnvKind .NestedInOwner then
364
+ sym.isProperlyContainedIn(env.owner)
365
+ else
366
+ sym.isContainedIn(env.owner)
367
+ // A captured reference with the symbol `sym` is visible from the environment
368
+ // if `sym` is not defined inside the owner of the environment
369
+ inline def isVisibleFromEnv (sym : Symbol ) = ! isContainedInEnv(sym)
370
+ // Only captured references that are visible from the environment
371
+ // should be included.
364
372
val included = cs.filter:
365
373
case ref : TermRef => isVisibleFromEnv(ref.symbol.owner)
366
374
case ref : ThisType => isVisibleFromEnv(ref.cls)
@@ -378,6 +386,7 @@ class CheckCaptures extends Recheck, SymTransformer:
378
386
// there won't be an apply; need to include call captures now
379
387
includeCallCaptures(tree.symbol, tree.srcPos)
380
388
else
389
+ // debugShowEnvs()
381
390
markFree(tree.symbol, tree.srcPos)
382
391
super .recheckIdent(tree, pt)
383
392
@@ -946,6 +955,19 @@ class CheckCaptures extends Recheck, SymTransformer:
946
955
expected
947
956
end addOuterRefs
948
957
958
+ /** A debugging method for showing the envrionments during capture checking. */
959
+ private def debugShowEnvs ()(using Context ): Unit =
960
+ def showEnv (env : Env ): String = i " Env( ${env.owner}, ${env.kind}, ${env.captured}) "
961
+ val sb = StringBuilder ()
962
+ @ annotation.tailrec def walk (env : Env | Null ): Unit =
963
+ if env != null then
964
+ sb ++= showEnv(env)
965
+ sb ++= " \n "
966
+ walk(env.outer0)
967
+ sb ++= " ===== Current Envs ======\n "
968
+ walk(curEnv)
969
+ sb ++= " ===== End ======\n "
970
+ println(sb.result())
949
971
950
972
/** Adapt `actual` type to `expected` type by inserting boxing and unboxing conversions
951
973
*
@@ -1085,6 +1107,7 @@ class CheckCaptures extends Recheck, SymTransformer:
1085
1107
pos)
1086
1108
}
1087
1109
if ! insertBox then // unboxing
1110
+ // debugShowEnvs()
1088
1111
markFree(criticalSet, pos)
1089
1112
adaptedType(! boxed)
1090
1113
else
0 commit comments