From 3348b8f79644e0d3177959817f5836597da3c362 Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Fri, 11 Apr 2025 11:45:19 +0200 Subject: [PATCH] Experiment with simpler inference for tracked --- .../src/dotty/tools/dotc/config/Feature.scala | 2 +- compiler/src/dotty/tools/dotc/typer/Namer.scala | 16 ++++++++++------ tests/pos/tracked.scala | 6 ++++++ 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 tests/pos/tracked.scala diff --git a/compiler/src/dotty/tools/dotc/config/Feature.scala b/compiler/src/dotty/tools/dotc/config/Feature.scala index 6df190f3147e..860df16cb566 100644 --- a/compiler/src/dotty/tools/dotc/config/Feature.scala +++ b/compiler/src/dotty/tools/dotc/config/Feature.scala @@ -105,7 +105,7 @@ object Feature: * feature is defined. */ def enabled(feature: TermName)(using Context): Boolean = - enabledBySetting(feature) || enabledByImport(feature) + enabledBySetting(feature) || enabledByImport(feature) || feature == modularity /** Is auto-tupling enabled? */ def autoTuplingEnabled(using Context): Boolean = !enabled(nme.noAutoTupling) diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index 89dc4cf53472..471c95a28fab 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -2020,12 +2020,10 @@ class Namer { typer: Typer => lazy val isRefInSignatures = psym.maybeOwner.isPrimaryConstructor && isReferencedInPublicSignatures(psym) + lazy val needsTrackedSimp = needsTrackedSimple(psym, param, owningSym) !psym.is(Tracked) && psym.isTerm - && ( - abstractContextBound - || isRefInSignatures - ) + && needsTrackedSimp /** Under x.modularity, we add `tracked` to context bound witnesses and * explicit evidence parameters that have abstract type members @@ -2036,6 +2034,13 @@ class Namer { typer: Typer => && (param.hasAttachment(ContextBoundParam) || (psym.isOneOf(GivenOrImplicit) && !accessorSyms.forall(_.isOneOf(PrivateLocal)))) && psym.info.memberNames(abstractTypeNameFilter).nonEmpty + private def needsTrackedSimple(psym: Symbol, param: ValDef, owningSym: Symbol)(using Context): Boolean = + val accessorSyms = maybeParamAccessors(owningSym, psym) + (owningSym.isClass || owningSym.isAllOf(Given | Method)) + && !accessorSyms.exists(_.is(Mutable)) + && (param.hasAttachment(ContextBoundParam) || !accessorSyms.forall(_.isOneOf(PrivateLocal))) + && psym.info.memberNames(abstractTypeNameFilter).nonEmpty + extension (sym: Symbol) private def infoWithForceNonInferingCompleter(using Context): Type = sym.infoOrCompleter match case tpe: LazyType if tpe.isExplicit => sym.info @@ -2083,8 +2088,7 @@ class Namer { typer: Typer => def setTrackedConstrParam(param: ValDef)(using Context): Unit = val sym = symbolOfTree(param) sym.maybeOwner.maybeOwner.infoOrCompleter match - case info: ClassInfo - if !sym.is(Tracked) && isContextBoundWitnessWithAbstractMembers(sym, param, sym.maybeOwner.maybeOwner) => + case info: ClassInfo if needsTracked(sym, param, sym.maybeOwner.maybeOwner) => typr.println(i"set tracked $param, $sym: ${sym.info} containing ${sym.info.memberNames(abstractTypeNameFilter).toList}") setParamTrackedWithAccessors(sym, info) case _ => diff --git a/tests/pos/tracked.scala b/tests/pos/tracked.scala new file mode 100644 index 000000000000..0933593a98d8 --- /dev/null +++ b/tests/pos/tracked.scala @@ -0,0 +1,6 @@ +import scala.language.experimental.modularity + +trait T: + type X + +class C(var t: T)