Skip to content

Commit 7d9771d

Browse files
authored
Do not bring forward symbols created in transform and backend phases (#21865)
In the i21844 issue minimization, first the `Macro.scala` and `SuperClassWIthLazyVal.scala` compilation units are compiled, then in the next run `SubClass.scala` is compiled. While compiling `SuperClassWIthLazyVal.scala`, in the `LazyVals` transform phase, the lzyINIT initialization fields are created. In the next run, while compiling `SubClass.scala`, in the `GenBCode` phase, the compiler would try to access the lzyINIT symbol, leading to a stale symbols crash. While that symbol was a part of the SuperClass, it by design is not generated for the Subclass - if we were to completely split those files into 2 separate compilations, that symbol would be created only for the classfile, but it would not be included in tasty, so the second compilation would not try to access it. This observation inspires the proposed fix, where if the symbol was first created in or after the first transform phase (so after the pickler phases), we do not try to bring forward this denotation, instead returning NoDenotation. In this PR, we also remove the fix proposed in i21559, as it is no longer necessary with the newly added condition. There, since one of the problematic Symbols created in `LazyVals` was moved elsewhere in `MoveStatics`, we checked if that symbol could be found in a companion object. I was not able to create any reproduction where a user defined static would produce this problem, so I believe it’s safe to remove that.
1 parent eb575a8 commit 7d9771d

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

compiler/src/dotty/tools/dotc/core/Denotations.scala

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools
22
package dotc
33
package core
44

5-
import SymDenotations.{ SymDenotation, ClassDenotation, NoDenotation, LazyType, stillValid, movedToCompanionClass, acceptStale, traceInvalid }
5+
import SymDenotations.{ SymDenotation, ClassDenotation, NoDenotation, LazyType, stillValid, acceptStale, traceInvalid }
66
import Contexts.*
77
import Names.*
88
import NameKinds.*
@@ -742,6 +742,8 @@ object Denotations {
742742
* the old version otherwise.
743743
* - If the symbol did not have a denotation that was defined at the current phase
744744
* return a NoDenotation instead.
745+
* - If the symbol was first defined in one of the transform phases (after pickling), it should not
746+
* be visible in new runs, so also return a NoDenotation.
745747
*/
746748
private def bringForward()(using Context): SingleDenotation = {
747749
this match {
@@ -755,11 +757,7 @@ object Denotations {
755757
}
756758
if (!symbol.exists) return updateValidity()
757759
if (!coveredInterval.containsPhaseId(ctx.phaseId)) return NoDenotation
758-
// Moved to a companion class, likely at a later phase (in MoveStatics)
759-
this match {
760-
case symd: SymDenotation if movedToCompanionClass(symd) => return NoDenotation
761-
case _ =>
762-
}
760+
if (coveredInterval.firstPhaseId >= Phases.firstTransformPhase.id) return NoDenotation
763761
if (ctx.debug) traceInvalid(this)
764762
staleSymbolError
765763
}

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

-4
Original file line numberDiff line numberDiff line change
@@ -2680,10 +2680,6 @@ object SymDenotations {
26802680
stillValidInOwner(denot)
26812681
}
26822682

2683-
def movedToCompanionClass(denot: SymDenotation)(using Context): Boolean =
2684-
val ownerCompanion = denot.maybeOwner.companionClass
2685-
stillValid(ownerCompanion) && ownerCompanion.unforcedDecls.contains(denot.name, denot.symbol)
2686-
26872683
private[SymDenotations] def stillValidInOwner(denot: SymDenotation)(using Context): Boolean = try
26882684
val owner = denot.maybeOwner.denot
26892685
stillValid(owner)

tests/pos-macros/i21844/Macro.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.quoted.*
2+
3+
object Macro:
4+
inline def foo = ${ fooImpl }
5+
def fooImpl(using Quotes): Expr[Int] =
6+
'{ 123 }
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class SubClass extends SuperClass
2+
object SubClass:
3+
val foo: Int = Macro.foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class SuperClass:
2+
lazy val xyz: Int = 123

0 commit comments

Comments
 (0)