From df85245c22d2bef711b1287389b2c1800b7a6d30 Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Thu, 6 Feb 2025 10:51:40 +0100 Subject: [PATCH] Support `def`-local `tracked` members Weaken the check for legal tracked members --- compiler/src/dotty/tools/dotc/ast/Desugar.scala | 7 ++++--- tests/neg/abstract-tracked.check | 4 ---- tests/neg/abstract-tracked.scala | 3 --- tests/neg/tracked.check | 4 ---- tests/neg/tracked.scala | 2 +- tests/pos/tracked-val-weaker.scala | 13 +++++++++++++ 6 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 tests/pos/tracked-val-weaker.scala diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index 03573d6f387c..cbda460d2af1 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -1574,9 +1574,10 @@ object desugar { } val legalTracked: Context ?=> MemberDefTest = { - case valdef @ ValDef(_, _, _) => - val sym = valdef.symbol - !ctx.owner.exists || ctx.owner.isClass || ctx.owner.is(Case) || ctx.owner.isConstructor || valdef.mods.is(Param) || valdef.mods.is(ParamAccessor) + case valdef: ValDef if valdef.mods.is(Param) || valdef.mods.is(ParamAccessor) => + !ctx.owner.exists || ctx.owner.isClass || ctx.owner.is(Case) || ctx.owner.isConstructor || ctx.owner.is(Synthetic) || (ctx.owner.isAllOf(Given | Method)) + case valdef: ValDef => + true } def checkOpaqueAlias(tree: MemberDef)(using Context): MemberDef = diff --git a/tests/neg/abstract-tracked.check b/tests/neg/abstract-tracked.check index 70a85e81df85..33bbbd47ba89 100644 --- a/tests/neg/abstract-tracked.check +++ b/tests/neg/abstract-tracked.check @@ -14,7 +14,3 @@ 7 | tracked def f: F // error | ^^^^^^^^^^^^^^^^ | Modifier tracked is not allowed for this definition --- [E156] Syntax Error: tests/neg/abstract-tracked.scala:14:14 --------------------------------------------------------- -14 | tracked val x = 1 // error - | ^^^^^^^^^^^^^^^^^ - | Modifier tracked is not allowed for this definition diff --git a/tests/neg/abstract-tracked.scala b/tests/neg/abstract-tracked.scala index ff4a7ea8174f..7dbf11957ebd 100644 --- a/tests/neg/abstract-tracked.scala +++ b/tests/neg/abstract-tracked.scala @@ -9,6 +9,3 @@ trait G: tracked object O // error tracked class C // error - -def f = - tracked val x = 1 // error diff --git a/tests/neg/tracked.check b/tests/neg/tracked.check index 3494c401a007..eef1c288c3d4 100644 --- a/tests/neg/tracked.check +++ b/tests/neg/tracked.check @@ -14,10 +14,6 @@ 4 |class C2(tracked var x: Int) // error | ^ | mutable variables may not be `tracked` --- [E156] Syntax Error: tests/neg/tracked.scala:8:16 ------------------------------------------------------------------- -8 | tracked val b: Int = 2 // error - | ^^^^^^^^^^^^^^^^^^^^^^ - | Modifier tracked is not allowed for this definition -- [E156] Syntax Error: tests/neg/tracked.scala:11:17 ------------------------------------------------------------------ 11 | tracked object Foo // error | ^^^^^^^^^^^^^^^^^^ diff --git a/tests/neg/tracked.scala b/tests/neg/tracked.scala index 3d6c1a14fc55..2cfb31835117 100644 --- a/tests/neg/tracked.scala +++ b/tests/neg/tracked.scala @@ -5,7 +5,7 @@ class C2(tracked var x: Int) // error object A: def foo(tracked a: Int) = // error - tracked val b: Int = 2 // error + tracked val b: Int = 2 object B: tracked object Foo // error diff --git a/tests/pos/tracked-val-weaker.scala b/tests/pos/tracked-val-weaker.scala new file mode 100644 index 000000000000..60f8aa2e2d85 --- /dev/null +++ b/tests/pos/tracked-val-weaker.scala @@ -0,0 +1,13 @@ +import scala.language.experimental.modularity + +object O: + tracked val x = 1 + +def Test = + tracked val x = 1 + val _: 1 = O.x + val _: 1 = x + tracked val y = x + val _: x.type = y + tracked val y1 = O.x + val _: O.x.type = y1