Skip to content

Commit

Permalink
Fix handling tracked param accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Oct 14, 2024
1 parent f1d146d commit d2b6a61
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1481,8 +1481,10 @@ object desugar {
rhsOK(rhs)
}

val legalTracked: MemberDefTest = {
case ValDef(_, _, _) => true
val legalTracked: Context ?=> MemberDefTest = {
case valdef @ ValDef(_, _, _) =>
val sym = valdef.symbol
ctx.owner.exists && (ctx.owner.isClass || ctx.owner.isConstructor)
}

def checkOpaqueAlias(tree: MemberDef)(using Context): MemberDef =
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -877,16 +877,16 @@ class Namer { typer: Typer =>
protected def addAnnotations(sym: Symbol): Unit = original match {
case original: untpd.MemberDef =>
lazy val annotCtx = annotContext(original, sym)
original.setMods:
original.setMods:
original.mods.withAnnotations :
original.mods.annotations.mapConserve: annotTree =>
original.mods.annotations.mapConserve: annotTree =>
val cls = typedAheadAnnotationClass(annotTree)(using annotCtx)
if (cls eq sym)
report.error(em"An annotation class cannot be annotated with iself", annotTree.srcPos)
annotTree
else
val ann =
if cls.is(JavaDefined) then Checking.checkNamedArgumentForJavaAnnotation(annotTree, cls.asClass)
val ann =
if cls.is(JavaDefined) then Checking.checkNamedArgumentForJavaAnnotation(annotTree, cls.asClass)
else annotTree
val ann1 = Annotation.deferred(cls)(typedAheadExpr(ann)(using annotCtx))
sym.addAnnotation(ann1)
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2835,14 +2835,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
excludeDeferredGiven(rhs, sym):
typedExpr(_, tpt1.tpe.widenExpr)
setAbstractTrackedInfo(sym, rhs1, tpt)
val tpt2 = if tpt.isEmpty then TypeTree(rhs1.tpe) else tpt1
val tpt2 = if sym.flags.is(Tracked) && tpt.isEmpty && !sym.flags.is(ParamAccessor) then TypeTree(rhs1.tpe) else tpt1
val vdef2 = assignType(cpy.ValDef(vdef)(name, tpt2, rhs1), sym)
postProcessInfo(vdef2, sym)
vdef2.setDefTree
}

private def setAbstractTrackedInfo(sym: Symbol, rhs: Tree, tpt: untpd.Tree)(using Context): Unit =
if sym.allOverriddenSymbols.exists(_.flags.is(Tracked)) then
if sym.allOverriddenSymbols.exists(_.flags.is(Tracked)) && !sym.flags.is(ParamAccessor) then
sym.setFlag(Tracked)
if tpt.isEmpty then
sym.info = rhs.tpe
Expand Down
21 changes: 18 additions & 3 deletions tests/neg/abstract-tracked.check
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
-- [E156] Syntax Error: tests/neg/abstract-tracked.scala:4:14 ----------------------------------------------------------
4 |tracked trait F: // error
|^
4 |tracked trait F // error
|^^^^^^^^^^^^^^^
|Modifier tracked is not allowed for this definition
5 | val x: Int
-- [E156] Syntax Error: tests/neg/abstract-tracked.scala:9:15 ----------------------------------------------------------
9 |tracked object O // error
|^^^^^^^^^^^^^^^^
|Modifier tracked is not allowed for this definition
-- [E156] Syntax Error: tests/neg/abstract-tracked.scala:11:14 ---------------------------------------------------------
11 |tracked class C // error
|^^^^^^^^^^^^^^^
|Modifier tracked is not allowed for this definition
-- [E156] Syntax Error: tests/neg/abstract-tracked.scala:7:14 ----------------------------------------------------------
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
13 changes: 11 additions & 2 deletions tests/neg/abstract-tracked.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import scala.language.experimental.modularity
import scala.language.future

tracked trait F: // error
val x: Int
tracked trait F // error

trait G:
tracked def f: F // error

tracked object O // error

tracked class C // error

def f =
tracked val x = 1 // error
15 changes: 15 additions & 0 deletions tests/pos/abstract-tracked.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ trait G:
trait H:
tracked val z: Int = 3

trait I extends F

trait J extends F:
val x: Int = 1

class K(tracked val x: Int)

object Test:
// val f : F(1) /*: F { val x: 1 }*/ = new F:
// val x: 1 = 1
Expand All @@ -19,7 +26,15 @@ object Test:
val y: 2 = 2
val h = new H:
override val z = 4
val i = new I:
val x = 5
val j = new J:
override val x = 6
val k = new K(7)

summon[f.x.type <:< 1]
summon[g.y.type <:< 2]
summon[h.z.type <:< 4]
summon[i.x.type <:< 5]
summon[j.x.type <:< 6]
summon[k.x.type <:< 7]

0 comments on commit d2b6a61

Please sign in to comment.