@@ -1213,43 +1213,41 @@ class CheckCaptures extends Recheck, SymTransformer:
12131213 val savedEnv = curEnv
12141214 val runInConstructor = ! sym.isOneOf(Param | ParamAccessor | Lazy | NonMember )
12151215 try
1216- if sym.is(Module ) then sym.info // Modules are checked by checking the module class
1217- else
1218- if sym.is(Mutable ) then
1219- if ! sym.hasAnnotation(defn.UncheckedCapturesAnnot ) then
1220- val addendum = setup.capturedBy.get(sym) match
1221- case Some (encl) =>
1222- val enclStr =
1223- if encl.isAnonymousFunction then
1224- val location = setup.anonFunCallee.get(encl) match
1225- case Some (meth) if meth.exists => i " argument in a call to $meth"
1226- case _ => " "
1227- s " an anonymous function $location"
1228- else encl.show
1229- i " \n\n Note that $sym does not count as local since it is captured by $enclStr"
1230- case _ =>
1231- " "
1232- disallowBadRootsIn(
1233- tree.tpt.nuType, NoSymbol , i " Mutable $sym" , " have type" , addendum, sym.srcPos)
1234- if ccConfig.strictMutability
1235- && sym.owner.isClass
1236- && ! sym.owner.derivesFrom(defn.Caps_Stateful )
1237- && ! sym.hasAnnotation(defn.UntrackedCapturesAnnot ) then
1238- report.error(
1239- em """ Mutable $sym is defined in a class that does not extend `Stateful`.
1240- |The variable needs to be annotated with `untrackedCaptures` to allow this. """ ,
1241- tree.namePos)
1242-
1243- // Lazy vals need their own environment to track captures from their RHS,
1244- // similar to how methods work
1245- if sym.is(Lazy ) then
1246- val localSet = capturedVars(sym)
1247- if localSet ne CaptureSet .empty then
1248- curEnv = Env (sym, EnvKind .Regular , localSet, curEnv, nestedClosure = NoSymbol )
1249- else if runInConstructor then
1250- pushConstructorEnv()
1251-
1252- checkInferredResult(super .recheckValDef(tree, sym), tree)
1216+ if sym.is(Mutable ) then
1217+ if ! sym.hasAnnotation(defn.UncheckedCapturesAnnot ) then
1218+ val addendum = setup.capturedBy.get(sym) match
1219+ case Some (encl) =>
1220+ val enclStr =
1221+ if encl.isAnonymousFunction then
1222+ val location = setup.anonFunCallee.get(encl) match
1223+ case Some (meth) if meth.exists => i " argument in a call to $meth"
1224+ case _ => " "
1225+ s " an anonymous function $location"
1226+ else encl.show
1227+ i " \n\n Note that $sym does not count as local since it is captured by $enclStr"
1228+ case _ =>
1229+ " "
1230+ disallowBadRootsIn(
1231+ tree.tpt.nuType, NoSymbol , i " Mutable $sym" , " have type" , addendum, sym.srcPos)
1232+ if ccConfig.strictMutability
1233+ && sym.owner.isClass
1234+ && ! sym.owner.derivesFrom(defn.Caps_Stateful )
1235+ && ! sym.hasAnnotation(defn.UntrackedCapturesAnnot ) then
1236+ report.error(
1237+ em """ Mutable $sym is defined in a class that does not extend `Stateful`.
1238+ |The variable needs to be annotated with `untrackedCaptures` to allow this. """ ,
1239+ tree.namePos)
1240+
1241+ // Lazy vals need their own environment to track captures from their RHS,
1242+ // similar to how methods work
1243+ if sym.is(Lazy ) then
1244+ val localSet = capturedVars(sym)
1245+ if localSet ne CaptureSet .empty then
1246+ curEnv = Env (sym, EnvKind .Regular , localSet, curEnv, nestedClosure = NoSymbol )
1247+ else if runInConstructor then
1248+ pushConstructorEnv()
1249+
1250+ checkInferredResult(super .recheckValDef(tree, sym), tree)
12531251 finally
12541252 if ! sym.is(Param ) then
12551253 // Parameters with inferred types belong to anonymous methods. We need to wait
@@ -1264,7 +1262,13 @@ class CheckCaptures extends Recheck, SymTransformer:
12641262 if runInConstructor && savedEnv.owner.isClass then
12651263 markFree(declaredCaptures, tree, addUseInfo = false )
12661264
1267- if sym.owner.isStaticOwner && ! declaredCaptures.elems.isEmpty && sym != defn.captureRoot then
1265+ if sym.owner.isStaticOwner
1266+ && ! declaredCaptures.elems.isEmpty
1267+ && sym != defn.captureRoot
1268+ && ! (sym.is(ModuleVal ) && sym.owner.is(Package ))
1269+ // global modules that don't derive from capability can have captures
1270+ // only if their fields have them, and then the field was already reported.
1271+ then
12681272 def where =
12691273 if sym.effectiveOwner.is(Package ) then " top-level definition"
12701274 else i " member of static ${sym.owner}"
0 commit comments