Skip to content

Commit adf6a25

Browse files
committed
Refactoring: Use common code for all markFree variants
1 parent 96d1610 commit adf6a25

File tree

11 files changed

+39
-28
lines changed

11 files changed

+39
-28
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ extension (tp: Type)
290290
case _: ThisType | NoPrefix =>
291291
tp1.symbol.is(Param) || tp1.symbol.is(ParamAccessor)
292292
case prefix => prefix.isParamPath
293+
case _: ParamRef => true
293294
case _ => false
294295

295296
/** If this is a unboxed capturing type with nonempty capture set, its boxed version.

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

tests/neg-custom-args/captures/capt1.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
-- Error: tests/neg-custom-args/captures/capt1.scala:6:11 --------------------------------------------------------------
22
6 | () => if x == null then y else y // error
33
| ^
4-
| (x : C^) cannot be referenced here; it is not included in the allowed capture set {}
4+
| reference (x : C^) is not included in the allowed capture set {}
55
| of an enclosing function literal with expected type () -> C
66
-- Error: tests/neg-custom-args/captures/capt1.scala:9:11 --------------------------------------------------------------
77
9 | () => if x == null then y else y // error
88
| ^
9-
| (x : C^) cannot be referenced here; it is not included in the allowed capture set {}
9+
| reference (x : C^) is not included in the allowed capture set {}
1010
| of an enclosing function literal with expected type Matchable
1111
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:16:2 -----------------------------------------
1212
16 | def f(y: Int) = if x == null then y else y // error
@@ -41,8 +41,8 @@
4141
-- Error: tests/neg-custom-args/captures/capt1.scala:34:30 -------------------------------------------------------------
4242
34 | val z2 = h[() -> Cap](() => x) // error // error
4343
| ^
44-
| (x : C^) cannot be referenced here; it is not included in the allowed capture set {}
45-
| of an enclosing function literal with expected type () -> box C^
44+
| reference (x : C^) is not included in the allowed capture set {}
45+
| of an enclosing function literal with expected type () -> box C^
4646
-- Error: tests/neg-custom-args/captures/capt1.scala:36:13 -------------------------------------------------------------
4747
36 | val z3 = h[(() -> Cap) @retains(x)](() => x)(() => C()) // error
4848
| ^^^^^^^^^^^^^^^^^^^^^^^

tests/neg-custom-args/captures/cc-this5.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Error: tests/neg-custom-args/captures/cc-this5.scala:16:20 ----------------------------------------------------------
22
16 | def f = println(c) // error
33
| ^
4-
| (c : Cap^) cannot be referenced here; it is not included in the allowed capture set {}
4+
| reference (c : Cap^) is not included in the allowed capture set {}
55
| of the enclosing class A
66
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/cc-this5.scala:21:15 -------------------------------------
77
21 | val x: A = this // error

tests/neg-custom-args/captures/eta.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
-- Error: tests/neg-custom-args/captures/eta.scala:6:20 ----------------------------------------------------------------
99
6 | bar( () => f ) // error
1010
| ^
11-
| (f : Proc^) cannot be referenced here; it is not included in the allowed capture set {}
11+
| reference (f : Proc^) is not included in the allowed capture set {}
1212
| of an enclosing function literal with expected type () -> box () ->? Unit

tests/neg-custom-args/captures/exception-definitions.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
-- Error: tests/neg-custom-args/captures/exception-definitions.scala:7:12 ----------------------------------------------
66
7 | val x = c // error
77
| ^
8-
|(c : Any^) cannot be referenced here; it is not included in the allowed capture set {} of the self type of class Err2
8+
| reference (c : Any^) is not included in the allowed capture set {} of the self type of class Err2
99
-- Error: tests/neg-custom-args/captures/exception-definitions.scala:8:13 ----------------------------------------------
1010
8 | class Err3(c: Any^) extends Exception // error
1111
| ^

tests/neg-custom-args/captures/i15772.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Error: tests/neg-custom-args/captures/i15772.scala:19:26 ------------------------------------------------------------
22
19 | val c : C^{x} = new C(x) // error
33
| ^
4-
| (x : C^) cannot be referenced here; it is not included in the allowed capture set {}
4+
| reference (x : C^) is not included in the allowed capture set {}
55
| of an enclosing function literal with expected type () -> Int
66
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:20:46 ---------------------------------------
77
20 | val boxed1 : ((C^) => Unit) -> Unit = box1(c) // error
@@ -13,7 +13,7 @@
1313
-- Error: tests/neg-custom-args/captures/i15772.scala:26:26 ------------------------------------------------------------
1414
26 | val c : C^{x} = new C(x) // error
1515
| ^
16-
| (x : C^) cannot be referenced here; it is not included in the allowed capture set {}
16+
| reference (x : C^) is not included in the allowed capture set {}
1717
| of an enclosing function literal with expected type () -> Int
1818
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:27:35 ---------------------------------------
1919
27 | val boxed2 : Observe[C^] = box2(c) // error

tests/neg-custom-args/captures/i16114.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-- Error: tests/neg-custom-args/captures/i16114.scala:20:8 -------------------------------------------------------------
77
20 | fs // error (limitation)
88
| ^^
9-
| (fs : Cap^) cannot be referenced here; it is not included in the allowed capture set {io}
9+
| reference (fs : Cap^) is not included in the allowed capture set {io}
1010
| of an enclosing function literal with expected type Unit ->{io} Unit
1111
-- Error: tests/neg-custom-args/captures/i16114.scala:24:13 ------------------------------------------------------------
1212
24 | expect[Cap^] { // error
@@ -16,7 +16,7 @@
1616
-- Error: tests/neg-custom-args/captures/i16114.scala:26:8 -------------------------------------------------------------
1717
26 | io // error (limitation)
1818
| ^^
19-
| (io : Cap^) cannot be referenced here; it is not included in the allowed capture set {fs}
19+
| reference (io : Cap^) is not included in the allowed capture set {fs}
2020
| of an enclosing function literal with expected type Unit ->{fs} Unit
2121
-- Error: tests/neg-custom-args/captures/i16114.scala:30:13 ------------------------------------------------------------
2222
30 | expect[Cap^] { // error
@@ -36,10 +36,10 @@
3636
-- Error: tests/neg-custom-args/captures/i16114.scala:40:8 -------------------------------------------------------------
3737
40 | io.use() // error
3838
| ^^
39-
| (io : Cap^) cannot be referenced here; it is not included in the allowed capture set {}
39+
| reference (io : Cap^) is not included in the allowed capture set {}
4040
| of an enclosing function literal with expected type Unit -> Unit
4141
-- Error: tests/neg-custom-args/captures/i16114.scala:41:8 -------------------------------------------------------------
4242
41 | io // error
4343
| ^^
44-
| (io : Cap^) cannot be referenced here; it is not included in the allowed capture set {}
44+
| reference (io : Cap^) is not included in the allowed capture set {}
4545
| of an enclosing function literal with expected type Unit -> Unit

tests/neg-custom-args/captures/lazylists2.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
-- Error: tests/neg-custom-args/captures/lazylists2.scala:40:20 --------------------------------------------------------
2626
40 | def head: B = f(xs.head) // error
2727
| ^
28-
|(f : A => B) cannot be referenced here; it is not included in the allowed capture set {xs} of the self type of class Mapped
28+
| reference (f : A => B) is not included in the allowed capture set {xs} of the self type of class Mapped
2929
-- Error: tests/neg-custom-args/captures/lazylists2.scala:41:48 --------------------------------------------------------
3030
41 | def tail: LazyList[B]^{this}= xs.tail.map(f) // error
3131
| ^
32-
|(f : A => B) cannot be referenced here; it is not included in the allowed capture set {xs} of the self type of class Mapped
32+
| reference (f : A => B) is not included in the allowed capture set {xs} of the self type of class Mapped
3333
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylists2.scala:45:4 ------------------------------------
3434
45 | final class Mapped extends LazyList[B]: // error
3535
| ^
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
-- Error: tests/neg-custom-args/captures/leaked-curried.scala:14:20 ----------------------------------------------------
22
14 | () => () => io // error
33
| ^^
4-
| (io : Cap^) cannot be referenced here; it is not included in the allowed capture set {}
4+
| reference (io : Cap^) is not included in the allowed capture set {}
55
| of an enclosing function literal with expected type () -> () ->{io} (ex$7: caps.Exists) -> Cap^{ex$7}
66
-- Error: tests/neg-custom-args/captures/leaked-curried.scala:17:20 ----------------------------------------------------
77
17 | () => () => io // error
88
| ^^
9-
| (io : Cap^) cannot be referenced here; it is not included in the allowed capture set {}
9+
| reference (io : Cap^) is not included in the allowed capture set {}
1010
| of an enclosing function literal with expected type () -> () ->{io} (ex$15: caps.Exists) -> Cap^{ex$15}

0 commit comments

Comments
 (0)