Skip to content

Commit 2e1ed26

Browse files
committed
A first attempt at level checking for classes
Check that instantiated root variable of a method inside a class is nested in the instance variable of the class. For the moment I was not able to construct a counter example where this makes a difference.
1 parent 7c34545 commit 2e1ed26

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class CheckCaptures extends Recheck, SymTransformer:
319319
includeCallCaptures(tree.symbol, tree.srcPos)
320320
else
321321
markFree(tree.symbol, tree.srcPos)
322-
instantiateLocalRoots(tree.symbol, NoPrefix, pt):
322+
instantiateLocalRoots(tree.symbol, NoPrefix, pt, tree.srcPos):
323323
super.recheckIdent(tree, pt)
324324

325325
/** A specialized implementation of the selection rule.
@@ -349,7 +349,7 @@ class CheckCaptures extends Recheck, SymTransformer:
349349

350350
val selType = recheckSelection(tree, qualType, name, disambiguate)
351351
val selCs = selType.widen.captureSet
352-
instantiateLocalRoots(tree.symbol, qualType, pt):
352+
instantiateLocalRoots(tree.symbol, qualType, pt, tree.srcPos):
353353
if selCs.isAlwaysEmpty || selType.widen.isBoxedCapturing || qualType.isBoxedCapturing then
354354
selType
355355
else
@@ -370,7 +370,7 @@ class CheckCaptures extends Recheck, SymTransformer:
370370
* - `tp` is the type of a function that gets applied, either as a method
371371
* or as a function value that gets applied.
372372
*/
373-
def instantiateLocalRoots(sym: Symbol, pre: Type, pt: Type)(tp: Type)(using Context): Type =
373+
def instantiateLocalRoots(sym: Symbol, pre: Type, pt: Type, pos: SrcPos)(tp: Type)(using Context): Type =
374374
def canInstantiate =
375375
sym.is(Method, butNot = Accessor)
376376
|| sym.isTerm && defn.isFunctionType(sym.info) && pt == AnySelectionProto
@@ -379,6 +379,16 @@ class CheckCaptures extends Recheck, SymTransformer:
379379
var tp1 = tpw
380380
val rootVar = CaptureRoot.Var(ctx.owner, sym)
381381
if sym.isLevelOwner then
382+
val outerOwner = sym.skipConstructor.owner.levelOwner
383+
if outerOwner.isClass then
384+
val outerRoot = outerOwner.localRoot.termRef
385+
outerRoot.asSeenFrom(pre, sym.owner) match
386+
case outerLimit: CaptureRoot if outerLimit ne outerRoot =>
387+
capt.println(i"constraining $rootVar of $sym by $outerLimit")
388+
if !outerLimit.encloses(rootVar) then
389+
// Should this be an assertion failure instead?
390+
report.error(em"outer instance $outerLimit does not enclose local root $rootVar", pos)
391+
case _ =>
382392
tp1 = mapRoots(sym.localRoot.termRef, rootVar)(tp1)
383393
if tp1 ne tpw then
384394
ccSetup.println(i"INST local $sym: $tp, ${sym.localRoot} = $tp1")

tests/pos-custom-args/captures/nested-classes-2.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ def test2(x1: (() => Unit), x2: (() => Unit) => Unit) =
2020
def test3(y1: (() => Unit), y2: (() => Unit) => Unit) =
2121
val cc1/*: C1^{cap[test3]}*/ = C1(y1, y2) // error (but should be OK)
2222
val cc2 = cc1.c2(x1, x2) // error (but should be OK)
23-
()
2423
//val cc3: cc1.C2^{cap[test2]} = cc2
2524

0 commit comments

Comments
 (0)