Skip to content

Commit b293dca

Browse files
committed
Fix error message for leaks
1 parent e30a3c8 commit b293dca

File tree

9 files changed

+24
-19
lines changed

9 files changed

+24
-19
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,16 +542,21 @@ class CheckCaptures extends Recheck, SymTransformer:
542542
useInfos += ((tree, cs, curEnv))
543543
end markFree
544544

545-
/** If capability `c` refers to a parameter that is not @use declared, report an error.
545+
/** If capability `c` refers to a parameter that is not implicitly or explicitly
546+
* @use declared, report an error.
546547
*/
547548
def checkUseDeclared(c: Capability, pos: SrcPos)(using Context): Unit =
548549
c.paramPathRoot match
549550
case ref: NamedType if !ref.symbol.isUseParam =>
550551
val what = if ref.isType then "Capture set parameter" else "Local reach capability"
551-
val owner = ref.symbol.owner
552+
def mitigation =
553+
if ccConfig.allowUse
554+
then i"\nTo allow this, the ${ref.symbol} should be declared with a @use annotation."
555+
else if !ref.isType then i"\nYou could try to abstract the capabilities referred to by $c in a capset variable."
556+
else ""
552557
report.error(
553-
em"""$what $c leaks into capture scope${owner.qualString("of")}.
554-
|To allow this, the ${ref.symbol} should be declared with a @use annotation""", pos)
558+
em"$what $c leaks into capture scope${owner.qualString("of")}.$mitigation",
559+
pos)
555560
case _ =>
556561

557562
/** Include references captured by the called method in the current environment stack */
@@ -1751,7 +1756,7 @@ class CheckCaptures extends Recheck, SymTransformer:
17511756

17521757
override def checkInheritedTraitParameters: Boolean = false
17531758

1754-
/** Check that overrides don't change the @use or @consume status of their parameters */
1759+
/** Check that overrides don't change the @use, @consume, or @reserve status of their parameters */
17551760
override def additionalChecks(member: Symbol, other: Symbol)(using Context): Unit =
17561761
for
17571762
(params1, params2) <- member.rawParamss.lazyZip(other.rawParamss)

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ class PlainPrinter(_ctx: Context) extends Printer {
379379
finally elideCapabilityCaps = saved
380380

381381
/** Print the annotation that are meant to be on the parameter symbol but was moved
382-
* to parameter types. Examples are `@use` and `@consume`. */
382+
* to parameter types. Examples are `@use` and `@consume`.
383+
*/
383384
protected def specialAnnotText(sym: ClassSymbol, tp: Type): Text =
384385
Str(s"@${sym.name} ").provided(tp.hasAnnotation(sym))
385386

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
11 | ops.foreach: op => // error
1010
| ^
1111
| Local reach capability ops* leaks into capture scope of method runOpsAlt.
12-
| To allow this, the parameter ops should be declared with a @use annotation
12+
| You could try to abstract the capabilities referred to by ops* in a capset variable.
1313
12 | op()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
10 | val io = x.unbox // error: local reach capability {x*} leaks
33
| ^^^^^^^
44
| Local reach capability x.unbox* leaks into capture scope of method foo.
5-
| To allow this, the parameter x should be declared with a @use annotation
5+
| You could try to abstract the capabilities referred to by x.unbox* in a capset variable.
66
-- Error: tests/neg-custom-args/captures/i21442.scala:18:14 ------------------------------------------------------------
77
18 | val io = x1.unbox // error
88
| ^^^^^^^^
99
| Local reach capability x* leaks into capture scope of method bar.
10-
| To allow this, the parameter x should be declared with a @use annotation
10+
| You could try to abstract the capabilities referred to by x* in a capset variable.
1111
-- Error: tests/neg-custom-args/captures/i21442.scala:17:10 ------------------------------------------------------------
1212
17 | val x1: Boxed[IO^] = x // error
1313
| ^^^^^^^^^^
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
-- Error: tests/neg-custom-args/captures/i23303.scala:6:36 -------------------------------------------------------------
22
6 | def execute: Unit = ops.foreach(f => f()) // error
33
| ^^^^^^^^
4-
| Local reach capability Runner.this.ops* leaks into capture scope of class Runner.
5-
| To allow this, the value ops should be declared with a @use annotation
4+
| Local reach capability Runner.this.ops* leaks into capture scope of class Runner.
5+
| You could try to abstract the capabilities referred to by Runner.this.ops* in a capset variable.
66
-- Error: tests/neg-custom-args/captures/i23303.scala:9:22 -------------------------------------------------------------
77
9 | () => ops.foreach(f => f()) // error
88
| ^^^^^^^^
99
| Local reach capability ops* leaks into capture scope of method Runner2.
10-
| To allow this, the parameter ops should be declared with a @use annotation
10+
| You could try to abstract the capabilities referred to by ops* in a capset variable.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
24 | var x: () ->{xs*} Unit = ys.head // error
33
| ^^^^^^^
44
| Local reach capability ops* leaks into capture scope of method localReach3.
5-
| To allow this, the parameter ops should be declared with a @use annotation
5+
| You could try to abstract the capabilities referred to by ops* in a capset variable.
66
-- Error: tests/neg-custom-args/captures/localReaches.scala:27:11 ------------------------------------------------------
77
27 | x = ys.head // error
88
| ^^^^^^^
99
| Local reach capability ops* leaks into capture scope of method localReach3.
10-
| To allow this, the parameter ops should be declared with a @use annotation
10+
| You could try to abstract the capabilities referred to by ops* in a capset variable.
1111
-- Error: tests/neg-custom-args/captures/localReaches.scala:14:10 ------------------------------------------------------
1212
14 | val xs: List[() => Unit] = op :: Nil // error
1313
| ^^^^^^^^^^^^^^^^

tests/neg-custom-args/captures/unsound-reach-6.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
7 | println(xs.head) // error
1919
| ^^^^^^^
2020
| Local reach capability xs* leaks into capture scope of method f.
21-
| To allow this, the parameter xs should be declared with a @use annotation
21+
| You could try to abstract the capabilities referred to by xs* in a capset variable.
2222
-- Error: tests/neg-custom-args/captures/unsound-reach-6.scala:11:14 ---------------------------------------------------
2323
11 | val z = f(ys) // error @consume failure
2424
| ^^
2525
| Local reach capability ys* leaks into capture scope of method test.
26-
| To allow this, the parameter ys should be declared with a @use annotation
26+
| You could try to abstract the capabilities referred to by ys* in a capset variable.
2727
-- Error: tests/neg-custom-args/captures/unsound-reach-6.scala:19:14 ---------------------------------------------------
2828
19 | val z = f(ys) // error @consume failure
2929
| ^^

tests/neg-custom-args/captures/use-capset.check

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@
1818
5 |private def g[C^] = (xs: List[Object^{C}]) => xs.head // error
1919
| ^^^^^^^
2020
| Capture set parameter C leaks into capture scope of method g.
21-
| To allow this, the type C should be declared with a @use annotation

tests/neg/localReaches.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
24 | var x: () ->{xs*} Unit = ys.head // error
33
| ^^^^^^^
44
| Local reach capability ops* leaks into capture scope of method localReach3.
5-
| To allow this, the parameter ops should be declared with a @use annotation
5+
| To allow this, the parameter ops should be declared with a @use annotation.
66
-- Error: tests/neg/localReaches.scala:27:11 ---------------------------------------------------------------------------
77
27 | x = ys.head // error
88
| ^^^^^^^
99
| Local reach capability ops* leaks into capture scope of method localReach3.
10-
| To allow this, the parameter ops should be declared with a @use annotation
10+
| To allow this, the parameter ops should be declared with a @use annotation.

0 commit comments

Comments
 (0)