From c2287a25baa3470fdc0a42198062dbabf97aedb2 Mon Sep 17 00:00:00 2001 From: Jan Chyb Date: Fri, 6 Sep 2024 18:04:35 +0200 Subject: [PATCH] Check if symbol was moved to a companion when bringing forward denotation While bringing forward the denotation to a new run, check if the symbol was moved from its owner to a companion object. If so, return NoDenotation, as that denotation seems to be a leftover from pre-MoveStatics phases in a previous run. --- compiler/src/dotty/tools/dotc/core/Denotations.scala | 7 ++++++- .../src/dotty/tools/dotc/core/SymDenotations.scala | 4 ++++ tests/pos-macros/i21271/Macro.scala | 12 ++++++++++++ tests/pos-macros/i21271/Test.scala | 1 + 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/pos-macros/i21271/Macro.scala create mode 100644 tests/pos-macros/i21271/Test.scala diff --git a/compiler/src/dotty/tools/dotc/core/Denotations.scala b/compiler/src/dotty/tools/dotc/core/Denotations.scala index 2418aba1978b..60a7555456bf 100644 --- a/compiler/src/dotty/tools/dotc/core/Denotations.scala +++ b/compiler/src/dotty/tools/dotc/core/Denotations.scala @@ -2,7 +2,7 @@ package dotty.tools package dotc package core -import SymDenotations.{ SymDenotation, ClassDenotation, NoDenotation, LazyType, stillValid, acceptStale, traceInvalid } +import SymDenotations.{ SymDenotation, ClassDenotation, NoDenotation, LazyType, stillValid, movedToCompanionClass, acceptStale, traceInvalid } import Contexts.* import Names.* import NameKinds.* @@ -755,6 +755,11 @@ object Denotations { } if (!symbol.exists) return updateValidity() if (!coveredInterval.containsPhaseId(ctx.phaseId)) return NoDenotation + // Moved to a companion class, likely at a later phase (in MoveStatics) + this match { + case symd: SymDenotation if movedToCompanionClass(symd) => return NoDenotation + case _ => + } if (ctx.debug) traceInvalid(this) staleSymbolError } diff --git a/compiler/src/dotty/tools/dotc/core/SymDenotations.scala b/compiler/src/dotty/tools/dotc/core/SymDenotations.scala index 3904228756a0..906e74735097 100644 --- a/compiler/src/dotty/tools/dotc/core/SymDenotations.scala +++ b/compiler/src/dotty/tools/dotc/core/SymDenotations.scala @@ -2680,6 +2680,10 @@ object SymDenotations { stillValidInOwner(denot) } + def movedToCompanionClass(denot: SymDenotation)(using Context): Boolean = + val ownerCompanion = denot.maybeOwner.companionClass + stillValid(ownerCompanion) && ownerCompanion.unforcedDecls.contains(denot.name, denot.symbol) + private[SymDenotations] def stillValidInOwner(denot: SymDenotation)(using Context): Boolean = try val owner = denot.maybeOwner.denot stillValid(owner) diff --git a/tests/pos-macros/i21271/Macro.scala b/tests/pos-macros/i21271/Macro.scala new file mode 100644 index 000000000000..09d29ecc65af --- /dev/null +++ b/tests/pos-macros/i21271/Macro.scala @@ -0,0 +1,12 @@ +import scala.quoted.* + +trait Schema +object Schema: + lazy val sampleDate: String = "" // lazy val requried to reproduce + + inline def derived: Schema = + annotations + new Schema {} + +inline def annotations: Int = ${ annotationsImpl } +def annotationsImpl(using Quotes): Expr[Int] = Expr(1) diff --git a/tests/pos-macros/i21271/Test.scala b/tests/pos-macros/i21271/Test.scala new file mode 100644 index 000000000000..c0ba38212b09 --- /dev/null +++ b/tests/pos-macros/i21271/Test.scala @@ -0,0 +1 @@ +val inputValueSchema = Schema.derived