Skip to content

Commit 3348b8f

Browse files
committed
Experiment with simpler inference for tracked
1 parent 0cd43fe commit 3348b8f

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

compiler/src/dotty/tools/dotc/config/Feature.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ object Feature:
105105
* feature is defined.
106106
*/
107107
def enabled(feature: TermName)(using Context): Boolean =
108-
enabledBySetting(feature) || enabledByImport(feature)
108+
enabledBySetting(feature) || enabledByImport(feature) || feature == modularity
109109

110110
/** Is auto-tupling enabled? */
111111
def autoTuplingEnabled(using Context): Boolean = !enabled(nme.noAutoTupling)

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,12 +2020,10 @@ class Namer { typer: Typer =>
20202020
lazy val isRefInSignatures =
20212021
psym.maybeOwner.isPrimaryConstructor
20222022
&& isReferencedInPublicSignatures(psym)
2023+
lazy val needsTrackedSimp = needsTrackedSimple(psym, param, owningSym)
20232024
!psym.is(Tracked)
20242025
&& psym.isTerm
2025-
&& (
2026-
abstractContextBound
2027-
|| isRefInSignatures
2028-
)
2026+
&& needsTrackedSimp
20292027

20302028
/** Under x.modularity, we add `tracked` to context bound witnesses and
20312029
* explicit evidence parameters that have abstract type members
@@ -2036,6 +2034,13 @@ class Namer { typer: Typer =>
20362034
&& (param.hasAttachment(ContextBoundParam) || (psym.isOneOf(GivenOrImplicit) && !accessorSyms.forall(_.isOneOf(PrivateLocal))))
20372035
&& psym.info.memberNames(abstractTypeNameFilter).nonEmpty
20382036

2037+
private def needsTrackedSimple(psym: Symbol, param: ValDef, owningSym: Symbol)(using Context): Boolean =
2038+
val accessorSyms = maybeParamAccessors(owningSym, psym)
2039+
(owningSym.isClass || owningSym.isAllOf(Given | Method))
2040+
&& !accessorSyms.exists(_.is(Mutable))
2041+
&& (param.hasAttachment(ContextBoundParam) || !accessorSyms.forall(_.isOneOf(PrivateLocal)))
2042+
&& psym.info.memberNames(abstractTypeNameFilter).nonEmpty
2043+
20392044
extension (sym: Symbol)
20402045
private def infoWithForceNonInferingCompleter(using Context): Type = sym.infoOrCompleter match
20412046
case tpe: LazyType if tpe.isExplicit => sym.info
@@ -2083,8 +2088,7 @@ class Namer { typer: Typer =>
20832088
def setTrackedConstrParam(param: ValDef)(using Context): Unit =
20842089
val sym = symbolOfTree(param)
20852090
sym.maybeOwner.maybeOwner.infoOrCompleter match
2086-
case info: ClassInfo
2087-
if !sym.is(Tracked) && isContextBoundWitnessWithAbstractMembers(sym, param, sym.maybeOwner.maybeOwner) =>
2091+
case info: ClassInfo if needsTracked(sym, param, sym.maybeOwner.maybeOwner) =>
20882092
typr.println(i"set tracked $param, $sym: ${sym.info} containing ${sym.info.memberNames(abstractTypeNameFilter).toList}")
20892093
setParamTrackedWithAccessors(sym, info)
20902094
case _ =>

tests/pos/tracked.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.language.experimental.modularity
2+
3+
trait T:
4+
type X
5+
6+
class C(var t: T)

0 commit comments

Comments
 (0)