Skip to content

Commit b15153e

Browse files
Merge pull request #53 from dotty-staging/mb/abstract-tracked-namer
Move tracked inference logic to `inferredResultType`
2 parents 567b2c1 + 9ff1af7 commit b15153e

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ object Flags {
480480
*/
481481
val AfterLoadFlags: FlagSet = commonFlags(
482482
FromStartFlags, AccessFlags, Final, AccessorOrSealed,
483-
Abstract, LazyOrTrait, SelfName, JavaDefined, JavaAnnotation, Transparent, Tracked)
483+
Abstract, LazyOrTrait, SelfName, JavaDefined, JavaAnnotation, Transparent)
484484

485485
/** A value that's unstable unless complemented with a Stable flag */
486486
val UnstableValueFlags: FlagSet = Mutable | Method

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,6 +2016,11 @@ class Namer { typer: Typer =>
20162016
paramFn: Type => Type,
20172017
fallbackProto: Type
20182018
)(using Context): Type =
2019+
/** Is this member tracked? This is true if it is marked as `tracked` or if
2020+
* it overrides a `tracked` member. To account for the later, `isTracked`
2021+
* is overriden to `true` as a side-effect of computing `inherited`.
2022+
*/
2023+
var isTracked: Boolean = sym.is(Tracked)
20192024

20202025
/** A type for this definition that might be inherited from elsewhere:
20212026
* If this is a setter parameter, the corresponding getter type.
@@ -2051,8 +2056,10 @@ class Namer { typer: Typer =>
20512056
if paramss.isEmpty then info.widenExpr
20522057
else NoType
20532058

2054-
val iRawInfo =
2055-
cls.info.nonPrivateDecl(sym.name).matchingDenotation(site, schema, sym.targetName).info
2059+
val iDenot = cls.info.nonPrivateDecl(sym.name).matchingDenotation(site, schema, sym.targetName)
2060+
val iSym = iDenot.symbol
2061+
if iSym.is(Tracked) then isTracked = true
2062+
val iRawInfo = iDenot.info
20562063
val iResType = instantiatedResType(iRawInfo, paramss).asSeenFrom(site, cls)
20572064
if (iResType.exists)
20582065
typr.println(i"using inherited type for ${mdef.name}; raw: $iRawInfo, inherited: $iResType")
@@ -2141,6 +2148,7 @@ class Namer { typer: Typer =>
21412148
if defaultTp.exists then TypeOps.SimplifyKeepUnchecked() else null)
21422149
match
21432150
case ctp: ConstantType if sym.isInlineVal => ctp
2151+
case tp if isTracked => tp
21442152
case tp => TypeComparer.widenInferred(tp, pt, Widen.Unions)
21452153

21462154
// Replace aliases to Unit by Unit itself. If we leave the alias in
@@ -2151,7 +2159,7 @@ class Namer { typer: Typer =>
21512159
def lhsType = fullyDefinedType(cookedRhsType, "right-hand side", mdef.srcPos)
21522160
//if (sym.name.toString == "y") println(i"rhs = $rhsType, cooked = $cookedRhsType")
21532161
if (inherited.exists)
2154-
if sym.isInlineVal then lhsType else inherited
2162+
if sym.isInlineVal || isTracked then lhsType else inherited
21552163
else {
21562164
if (sym.is(Implicit))
21572165
mdef match {

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,20 +2834,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
28342834
case rhs =>
28352835
excludeDeferredGiven(rhs, sym):
28362836
typedExpr(_, tpt1.tpe.widenExpr)
2837-
setAbstractTrackedInfo(sym, rhs1, tpt)
2838-
val tpt2 = if sym.flags.is(Tracked) && tpt.isEmpty && !sym.flags.is(ParamAccessor) && !sym.flags.is(Param) then TypeTree(rhs1.tpe) else tpt1
2839-
val vdef2 = assignType(cpy.ValDef(vdef)(name, tpt2, rhs1), sym)
2840-
postProcessInfo(vdef2, sym)
2841-
vdef2.setDefTree
2837+
val vdef1 = assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
2838+
postProcessInfo(vdef1, sym)
2839+
vdef1.setDefTree
28422840
}
2843-
2844-
private def setAbstractTrackedInfo(sym: Symbol, rhs: Tree, tpt: untpd.Tree)(using Context): Unit =
2845-
if !sym.flags.is(ParamAccessor) && !sym.flags.is(Param) then
2846-
if sym.allOverriddenSymbols.exists(_.flags.is(Tracked)) then
2847-
sym.setFlag(Tracked)
2848-
if sym.flags.is(Tracked) && tpt.isEmpty then
2849-
sym.info = rhs.tpe
2850-
28512841
private def retractDefDef(sym: Symbol)(using Context): Tree =
28522842
// it's a discarded method (synthetic case class method or synthetic java record constructor or overridden member), drop it
28532843
val canBeInvalidated: Boolean =

tests/pos/abstract-tracked-2.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.language.experimental.modularity
2+
import scala.language.future
3+
4+
abstract class Vec:
5+
tracked val size: Int
6+
7+
@main def main =
8+
val v = new Vec:
9+
val size0: size.type = 10
10+
val size = 10
11+
val size1: size.type = 10

0 commit comments

Comments
 (0)