Skip to content

Commit 774eaa1

Browse files
committed
Refine test of vals with inferred types contributing fresh
1 parent 498752e commit 774eaa1

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,10 +1239,9 @@ class CheckCaptures extends Recheck, SymTransformer:
12391239
|must conform to this type."""
12401240

12411241
tree.tpt match
1242-
case tpt: InferredTypeTree if !isExemptFromChecks =>
1243-
if !sym.isLocalToCompilationUnit
1244-
// Symbols that can't be seen outside the compilation unit can have inferred types
1245-
// except for the else clause below.
1242+
case tpt: InferredTypeTree =>
1243+
if !sym.isLocalToCompilationUnit && !isExemptFromChecks
1244+
// Symbols that can't be seen outside the compilation unit can have inferred types
12461245
then
12471246
val expected = tpt.tpe.dropAllRetains
12481247
todoAtPostCheck += { () =>
@@ -1251,18 +1250,19 @@ class CheckCaptures extends Recheck, SymTransformer:
12511250
// The check that inferred <: expected is done after recheck so that it
12521251
// does not interfere with normal rechecking by constraining capture set variables.
12531252
}
1254-
else if sym.is(Private)
1255-
&& !sym.isLocalToCompilationUnitIgnoringPrivate
1256-
&& tree.tpt.nuType.spanCaptureSet.containsTerminalCapability
1253+
if !sym.isLocalToCompilationUnitIgnoringPrivate
12571254
&& contributesFreshToClass(sym)
12581255
// Private symbols capturing a root capability need explicit types
12591256
// so that we can compute field constributions to class instance
12601257
// capture sets across compilation units.
12611258
then
1262-
report.error(
1263-
em"""$sym needs an explicit type because it captures a root capability in its type ${tree.tpt.nuType}.
1264-
|Fields of publicily accessible classes that capture a root capability need to be given an explicit type.""",
1265-
tpt.srcPos)
1259+
todoAtPostCheck += { () =>
1260+
if sym.info.spanCaptureSet.containsTerminalCapability then
1261+
report.error(
1262+
em"""$sym needs an explicit type because it captures a root capability in its type ${tree.tpt.nuType}.
1263+
|Fields of publicily accessible classes that capture a root capability need to be given an explicit type.""",
1264+
tpt.srcPos)
1265+
}
12661266
case _ =>
12671267
tp
12681268
end checkInferredResult

tests/neg-custom-args/captures/i24335.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@ class C(val io: IO):
66
val l1 = () => c.io.write()
77
val _: () -> Unit = l1 // error
88

9+
class Ref extends caps.Mutable:
10+
var x: Int = 0
11+
12+
class D:
13+
val r = Ref() // error
14+
15+
def test =
16+
val d = D()
17+
val _: D^{} = d
918

0 commit comments

Comments
 (0)