Skip to content

Fix stale symbols connected to lazy vals in macros #21559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Denotations.scala
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions tests/pos-macros/i21271/Macro.scala
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tests/pos-macros/i21271/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val inputValueSchema = Schema.derived