@@ -390,14 +390,7 @@ class CheckCaptures extends Recheck, SymTransformer:
390390 markFree(sym, sym.termRef, pos)
391391
392392 def markFree (sym : Symbol , ref : TermRef , pos : SrcPos )(using Context ): Unit =
393- if sym.exists && ref.isTracked then
394- def recur (env : Env ): Unit =
395- if env.isOpen && env.owner != sym.enclosure then
396- capt.println(i " Mark $sym with cs ${ref.captureSet} free in ${env.owner}" )
397- checkElem(ref, env.captured, pos, provenance(env))
398- if ! isOfNestedMethod(env) then
399- recur(nextEnvToCharge(env, _.owner != sym.enclosure))
400- recur(curEnv)
393+ if sym.exists && ref.isTracked then markFree(ref.captureSet, pos)
401394
402395 /** Make sure (projected) `cs` is a subset of the capture sets of all enclosing
403396 * environments. At each stage, only include references from `cs` that are outside
@@ -434,7 +427,24 @@ class CheckCaptures extends Recheck, SymTransformer:
434427 case ref =>
435428 false
436429 if ! isVisible then
437- c match
430+ // println(i"out of scope: $c")
431+ if ccConfig.deferredReaches then // avoid all locally bound capabilities
432+ if c.isParamPath then
433+ c match
434+ case ReachCapability (_) | _ : TypeRef =>
435+ checkUseDeclared(c, env, lastEnv)
436+ case _ =>
437+ else
438+ val underlying = c match
439+ case ReachCapability (c1) =>
440+ CaptureSet .ofTypeDeeply(c1.widen)
441+ case _ =>
442+ CaptureSet .ofType(c.widen, followResult = false )
443+ capt.println(i " Widen reach $c to $underlying in ${env.owner}" )
444+ underlying.disallowRootCapability: () =>
445+ report.error(em " Local capability $c in ${env.ownerString} cannot have `cap` as underlying capture set " , pos)
446+ recur(underlying, env, lastEnv)
447+ else c match // avoid only reach capabilities and capture sets
438448 case ReachCapability (c1) =>
439449 if c1.isParamPath then
440450 checkUseDeclared(c, env, lastEnv)
0 commit comments