From 7142e3d41f849969714911756a68ac73ab46baef Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Fri, 25 Oct 2024 18:54:50 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20some=20pickling=20errors=20=E2=80=A2?= =?UTF-8?q?=E1=B4=97=E2=80=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/dotty/tools/dotc/typer/Namer.scala | 29 ++++++++++++------- .../test/dotc/pos-test-pickling.blacklist | 1 + .../infer-tracked-parent-refinements.scala | 8 +++++ ...fer-tracked-parsercombinators-givens.scala | 3 +- 4 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 tests/pos/infer-tracked-parent-refinements.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index b7797ebe2cd7..8a592583bf68 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -1791,6 +1791,7 @@ class Namer { typer: Typer => sym.owner.typeParams.foreach(_.ensureCompleted()) completeTrailingParamss(constr, sym, indexingCtor = true) if Feature.enabled(modularity) then + // println(i"[indexConstructor] Checking if params of $constr need tracked") constr.termParamss.foreach(_.foreach(setTracked)) /** The signature of a module valdef. @@ -1931,22 +1932,26 @@ class Namer { typer: Typer => def wrapRefinedMethType(restpe: Type): Type = wrapMethType(addParamRefinements(restpe, paramSymss)) + def addTrackedIfNeeded(ddef: DefDef, owningSym: Symbol): Boolean = + var wasSet = false + for params <- ddef.termParamss; param <- params do + val psym = symbolOfTree(param) + if needsTracked(psym, param, owningSym) then + psym.setFlag(Tracked) + wasSet = true + wasSet + + if Feature.enabled(modularity) then addTrackedIfNeeded(ddef, sym.maybeOwner) + if isConstructor then // set result type tree to unit, but take the current class as result type of the symbol typedAheadType(ddef.tpt, defn.UnitType) val mt = wrapMethType(effectiveResultType(sym, paramSymss)) if sym.isPrimaryConstructor then checkCaseClassParamDependencies(mt, sym.owner) mt - else if Feature.enabled(modularity) then - // set every context bound evidence parameter of a given companion method - // to be tracked, provided it has a type that has an abstract type member. - // Add refinements for all tracked parameters to the result type. - for params <- ddef.termParamss; param <- params do - val psym = symbolOfTree(param) - if needsTracked(psym, param, sym) then psym.setFlag(Tracked) - valOrDefDefSig(ddef, sym, paramSymss, wrapRefinedMethType) else - valOrDefDefSig(ddef, sym, paramSymss, wrapMethType) + val paramFn = if Feature.enabled(Feature.modularity) && sym.isAllOf(Given | Method) then wrapRefinedMethType else wrapMethType + valOrDefDefSig(ddef, sym, paramSymss, paramFn) end defDefSig /** Complete the trailing parameters of a DefDef, @@ -1998,6 +2003,7 @@ class Namer { typer: Typer => /** Try to infer if the parameter needs a `tracked` modifier */ def needsTracked(psym: Symbol, param: ValDef, owningSym: Symbol)(using Context) = + // println(i"Checking if $psym needs tracked") lazy val abstractContextBound = isContextBoundWitnessWithAbstractMembers(psym, param, owningSym) lazy val isRefInSignatures = psym.maybeOwner.isPrimaryConstructor @@ -2071,8 +2077,9 @@ class Namer { typer: Typer => def setTracked(param: ValDef)(using Context): Unit = val sym = symbolOfTree(param) sym.maybeOwner.maybeOwner.infoOrCompleter match - 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}") + case info: ClassInfo + if !sym.is(Tracked) && isContextBoundWitnessWithAbstractMembers(sym, param, sym.maybeOwner.maybeOwner) => + typr.println(i"set tracked $param, $sym: ${sym.info}") for acc <- info.decls.lookupAll(sym.name) if acc.is(ParamAccessor) do acc.resetFlag(PrivateLocal) acc.setFlag(Tracked) diff --git a/compiler/test/dotc/pos-test-pickling.blacklist b/compiler/test/dotc/pos-test-pickling.blacklist index 032b53150e49..d8f4fcc10c04 100644 --- a/compiler/test/dotc/pos-test-pickling.blacklist +++ b/compiler/test/dotc/pos-test-pickling.blacklist @@ -135,6 +135,7 @@ parsercombinators-new-syntax.scala hylolib-deferred-given hylolib-cb hylolib +infer-tracked-parsercombinators-givens.scala # typecheckErrors method unpickling i21415.scala diff --git a/tests/pos/infer-tracked-parent-refinements.scala b/tests/pos/infer-tracked-parent-refinements.scala new file mode 100644 index 000000000000..0d71d7cc2897 --- /dev/null +++ b/tests/pos/infer-tracked-parent-refinements.scala @@ -0,0 +1,8 @@ +import scala.language.experimental.modularity +import scala.language.future + +trait WithValue { type Value = Int } + +case class Year(value: Int) extends WithValue { + val x: Value = 2 +} diff --git a/tests/pos/infer-tracked-parsercombinators-givens.scala b/tests/pos/infer-tracked-parsercombinators-givens.scala index 8bb514c8a75a..209bbcc67493 100644 --- a/tests/pos/infer-tracked-parsercombinators-givens.scala +++ b/tests/pos/infer-tracked-parsercombinators-givens.scala @@ -1,6 +1,5 @@ import scala.language.experimental.modularity import scala.language.future - import collection.mutable /// A parser combinator. @@ -19,7 +18,7 @@ end Combinator final case class Apply[C, E](action: C => Option[E]) final case class Combine[A, B](first: A, second: B) -given apply[C, E]: Combinator[Apply[C, E]] with { +given apply: [C, E] => Combinator[Apply[C, E]] { type Context = C type Element = E extension(self: Apply[C, E]) {