Skip to content

Commit 602ede3

Browse files
oderskyHarrisL2
authored andcommitted
Issue a warning when a global value is declared with a capture set
1 parent 3e657c7 commit 602ede3

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,9 +1019,20 @@ class CheckCaptures extends Recheck, SymTransformer:
10191019
// function is compiled since we do not propagate expected types into blocks.
10201020
interpolateIfInferred(tree.tpt, sym)
10211021

1022+
def declaredCaptures = tree.tpt.nuType.captureSet
10221023
if runInConstructor && savedEnv.owner.isClass then
10231024
curEnv = savedEnv
1024-
markFree(tree.tpt.nuType.captureSet, tree, addUseInfo = false)
1025+
markFree(declaredCaptures, tree, addUseInfo = false)
1026+
1027+
if sym.owner.isStaticOwner && !declaredCaptures.elems.isEmpty then
1028+
def where =
1029+
if sym.effectiveOwner.is(Package) then "top-level definition"
1030+
else i"member of static ${sym.owner}"
1031+
report.warning(
1032+
em"""$sym has a non-empty capture set but will not be added as
1033+
|a capability to computed capture sets since it is globally accessible
1034+
|as a $where. Global values cannot be capabilities.""",
1035+
tree.namePos)
10251036
end recheckValDef
10261037

10271038
/** Recheck method definitions:

tests/neg-custom-args/captures/class-caps.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@
2020
30 | a + b
2121
|
2222
| longer explanation available when compiling with `-explain`
23+
-- Warning: tests/neg-custom-args/captures/class-caps.scala:35:6 -------------------------------------------------------
24+
35 | val console: Console^ = Console() // provide capability locally
25+
| ^^^^^^^
26+
| value console has a non-empty capture set but will not be added as
27+
| a capability to computed capture sets since it is globally accessible
28+
| as a member of static object Test2. Global values cannot be capabilities.

tests/neg-custom-args/captures/nonsensical-for-pure.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22
1 |val x: Int^ = 2 // error
33
| ^^^^
44
| Int is a pure type, it makes no sense to add a capture set to it
5+
-- Warning: tests/neg-custom-args/captures/nonsensical-for-pure.scala:1:4 ----------------------------------------------
6+
1 |val x: Int^ = 2 // error
7+
| ^
8+
| value x has a non-empty capture set but will not be added as
9+
| a capability to computed capture sets since it is globally accessible
10+
| as a top-level definition. Global values cannot be capabilities.

tests/pos-custom-args/captures/i23421.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ object Collection:
1212
trait Foo:
1313
val thunks: Collection[() => Unit] // that's fine
1414

15-
object FooImpl1 extends Foo:
15+
class FooImpl1 extends Foo:
1616
val thunks: Collection[() => Unit] = Collection.empty // was error, now ok

0 commit comments

Comments
 (0)