Skip to content

Commit d2b6a61

Browse files
committed
Fix handling tracked param accessors
1 parent f1d146d commit d2b6a61

File tree

6 files changed

+54
-13
lines changed

6 files changed

+54
-13
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,8 +1481,10 @@ object desugar {
14811481
rhsOK(rhs)
14821482
}
14831483

1484-
val legalTracked: MemberDefTest = {
1485-
case ValDef(_, _, _) => true
1484+
val legalTracked: Context ?=> MemberDefTest = {
1485+
case valdef @ ValDef(_, _, _) =>
1486+
val sym = valdef.symbol
1487+
ctx.owner.exists && (ctx.owner.isClass || ctx.owner.isConstructor)
14861488
}
14871489

14881490
def checkOpaqueAlias(tree: MemberDef)(using Context): MemberDef =

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -877,16 +877,16 @@ class Namer { typer: Typer =>
877877
protected def addAnnotations(sym: Symbol): Unit = original match {
878878
case original: untpd.MemberDef =>
879879
lazy val annotCtx = annotContext(original, sym)
880-
original.setMods:
880+
original.setMods:
881881
original.mods.withAnnotations :
882-
original.mods.annotations.mapConserve: annotTree =>
882+
original.mods.annotations.mapConserve: annotTree =>
883883
val cls = typedAheadAnnotationClass(annotTree)(using annotCtx)
884884
if (cls eq sym)
885885
report.error(em"An annotation class cannot be annotated with iself", annotTree.srcPos)
886886
annotTree
887887
else
888-
val ann =
889-
if cls.is(JavaDefined) then Checking.checkNamedArgumentForJavaAnnotation(annotTree, cls.asClass)
888+
val ann =
889+
if cls.is(JavaDefined) then Checking.checkNamedArgumentForJavaAnnotation(annotTree, cls.asClass)
890890
else annotTree
891891
val ann1 = Annotation.deferred(cls)(typedAheadExpr(ann)(using annotCtx))
892892
sym.addAnnotation(ann1)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,14 +2835,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
28352835
excludeDeferredGiven(rhs, sym):
28362836
typedExpr(_, tpt1.tpe.widenExpr)
28372837
setAbstractTrackedInfo(sym, rhs1, tpt)
2838-
val tpt2 = if tpt.isEmpty then TypeTree(rhs1.tpe) else tpt1
2838+
val tpt2 = if sym.flags.is(Tracked) && tpt.isEmpty && !sym.flags.is(ParamAccessor) then TypeTree(rhs1.tpe) else tpt1
28392839
val vdef2 = assignType(cpy.ValDef(vdef)(name, tpt2, rhs1), sym)
28402840
postProcessInfo(vdef2, sym)
28412841
vdef2.setDefTree
28422842
}
28432843

28442844
private def setAbstractTrackedInfo(sym: Symbol, rhs: Tree, tpt: untpd.Tree)(using Context): Unit =
2845-
if sym.allOverriddenSymbols.exists(_.flags.is(Tracked)) then
2845+
if sym.allOverriddenSymbols.exists(_.flags.is(Tracked)) && !sym.flags.is(ParamAccessor) then
28462846
sym.setFlag(Tracked)
28472847
if tpt.isEmpty then
28482848
sym.info = rhs.tpe

tests/neg/abstract-tracked.check

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
-- [E156] Syntax Error: tests/neg/abstract-tracked.scala:4:14 ----------------------------------------------------------
2-
4 |tracked trait F: // error
3-
|^
2+
4 |tracked trait F // error
3+
|^^^^^^^^^^^^^^^
44
|Modifier tracked is not allowed for this definition
5-
5 | val x: Int
5+
-- [E156] Syntax Error: tests/neg/abstract-tracked.scala:9:15 ----------------------------------------------------------
6+
9 |tracked object O // error
7+
|^^^^^^^^^^^^^^^^
8+
|Modifier tracked is not allowed for this definition
9+
-- [E156] Syntax Error: tests/neg/abstract-tracked.scala:11:14 ---------------------------------------------------------
10+
11 |tracked class C // error
11+
|^^^^^^^^^^^^^^^
12+
|Modifier tracked is not allowed for this definition
13+
-- [E156] Syntax Error: tests/neg/abstract-tracked.scala:7:14 ----------------------------------------------------------
14+
7 | tracked def f: F // error
15+
| ^^^^^^^^^^^^^^^^
16+
| Modifier tracked is not allowed for this definition
17+
-- [E156] Syntax Error: tests/neg/abstract-tracked.scala:14:14 ---------------------------------------------------------
18+
14 | tracked val x = 1 // error
19+
| ^^^^^^^^^^^^^^^^^
20+
| Modifier tracked is not allowed for this definition

tests/neg/abstract-tracked.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import scala.language.experimental.modularity
22
import scala.language.future
33

4-
tracked trait F: // error
5-
val x: Int
4+
tracked trait F // error
5+
6+
trait G:
7+
tracked def f: F // error
8+
9+
tracked object O // error
10+
11+
tracked class C // error
12+
13+
def f =
14+
tracked val x = 1 // error

tests/pos/abstract-tracked.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ trait G:
1010
trait H:
1111
tracked val z: Int = 3
1212

13+
trait I extends F
14+
15+
trait J extends F:
16+
val x: Int = 1
17+
18+
class K(tracked val x: Int)
19+
1320
object Test:
1421
// val f : F(1) /*: F { val x: 1 }*/ = new F:
1522
// val x: 1 = 1
@@ -19,7 +26,15 @@ object Test:
1926
val y: 2 = 2
2027
val h = new H:
2128
override val z = 4
29+
val i = new I:
30+
val x = 5
31+
val j = new J:
32+
override val x = 6
33+
val k = new K(7)
2234

2335
summon[f.x.type <:< 1]
2436
summon[g.y.type <:< 2]
2537
summon[h.z.type <:< 4]
38+
summon[i.x.type <:< 5]
39+
summon[j.x.type <:< 6]
40+
summon[k.x.type <:< 7]

0 commit comments

Comments
 (0)