@@ -191,7 +191,7 @@ class CheckCaptures extends Recheck, SymTransformer:
191191 if Feature .ccEnabled then
192192 super .run
193193
194- val ccState = new CCState
194+ val ccState1 = new CCState // Dotty problem: Rename to ccState ==> Crash in ExplicitOuter
195195
196196 class CaptureChecker (ictx : Context ) extends Rechecker (ictx):
197197
@@ -311,7 +311,7 @@ class CheckCaptures extends Recheck, SymTransformer:
311311 def capturedVars (sym : Symbol )(using Context ): CaptureSet =
312312 myCapturedVars.getOrElseUpdate(sym,
313313 if sym.ownersIterator.exists(_.isTerm)
314- then CaptureSet .Var (sym.owner)
314+ then CaptureSet .Var (sym.owner, level = ccState.level(sym) )
315315 else CaptureSet .empty)
316316
317317 /** For all nested environments up to `limit` or a closed environment perform `op`,
@@ -585,6 +585,9 @@ class CheckCaptures extends Recheck, SymTransformer:
585585 tree.srcPos)
586586 super .recheckTypeApply(tree, pt)
587587
588+ override def recheckBlock (tree : Block , pt : Type )(using Context ): Type =
589+ ccState.inNestedLevel(super .recheckBlock(tree, pt))
590+
588591 override def recheckClosure (tree : Closure , pt : Type , forceDependent : Boolean )(using Context ): Type =
589592 val cs = capturedVars(tree.meth.symbol)
590593 capt.println(i " typing closure $tree with cvs $cs" )
@@ -688,13 +691,14 @@ class CheckCaptures extends Recheck, SymTransformer:
688691 val localSet = capturedVars(sym)
689692 if ! localSet.isAlwaysEmpty then
690693 curEnv = Env (sym, EnvKind .Regular , localSet, curEnv)
691- try checkInferredResult(super .recheckDefDef(tree, sym), tree)
692- finally
693- if ! sym.isAnonymousFunction then
694- // Anonymous functions propagate their type to the enclosing environment
695- // so it is not in general sound to interpolate their types.
696- interpolateVarsIn(tree.tpt)
697- curEnv = saved
694+ ccState.inNestedLevel:
695+ try checkInferredResult(super .recheckDefDef(tree, sym), tree)
696+ finally
697+ if ! sym.isAnonymousFunction then
698+ // Anonymous functions propagate their type to the enclosing environment
699+ // so it is not in general sound to interpolate their types.
700+ interpolateVarsIn(tree.tpt)
701+ curEnv = saved
698702
699703 override def recheckRHS (rhs : Tree , pt : Type )(using Context ): Type =
700704 def avoidMap = new TypeOps .AvoidMap :
@@ -824,9 +828,9 @@ class CheckCaptures extends Recheck, SymTransformer:
824828 val saved = curEnv
825829 tree match
826830 case _ : RefTree | closureDef(_) if pt.isBoxedCapturing =>
827- curEnv = Env (curEnv.owner, EnvKind .Boxed , CaptureSet .Var (curEnv.owner), curEnv)
831+ curEnv = Env (curEnv.owner, EnvKind .Boxed , CaptureSet .Var (curEnv.owner, level = ccState.currentLevel ), curEnv)
828832 case _ if tree.hasAttachment(ClosureBodyValue ) =>
829- curEnv = Env (curEnv.owner, EnvKind .ClosureResult , CaptureSet .Var (curEnv.owner), curEnv)
833+ curEnv = Env (curEnv.owner, EnvKind .ClosureResult , CaptureSet .Var (curEnv.owner, level = ccState.currentLevel ), curEnv)
830834 case _ =>
831835 val res =
832836 try
@@ -990,7 +994,11 @@ class CheckCaptures extends Recheck, SymTransformer:
990994
991995 inline def inNestedEnv [T ](boxed : Boolean )(op : => T ): T =
992996 val saved = curEnv
993- curEnv = Env (curEnv.owner, EnvKind .NestedInOwner , CaptureSet .Var (curEnv.owner), if boxed then null else curEnv)
997+ curEnv = Env (
998+ curEnv.owner,
999+ EnvKind .NestedInOwner ,
1000+ CaptureSet .Var (curEnv.owner, level = ccState.currentLevel),
1001+ if boxed then null else curEnv)
9941002 try op
9951003 finally curEnv = saved
9961004
0 commit comments