Skip to content

Commit c9e4b03

Browse files
jchybtgodzik
authored andcommitted
Do not bring forward symbols created in transform and backend phases (scala#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 fb2c793 commit c9e4b03

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

Diff for: compiler/src/dotty/tools/dotc/core/Denotations.scala

+3
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,8 @@ object Denotations {
736736
* the old version otherwise.
737737
* - If the symbol did not have a denotation that was defined at the current phase
738738
* return a NoDenotation instead.
739+
* - If the symbol was first defined in one of the transform phases (after pickling), it should not
740+
* be visible in new runs, so also return a NoDenotation.
739741
*/
740742
private def bringForward()(using Context): SingleDenotation = {
741743
this match {
@@ -749,6 +751,7 @@ object Denotations {
749751
}
750752
if (!symbol.exists) return updateValidity()
751753
if (!coveredInterval.containsPhaseId(ctx.phaseId)) return NoDenotation
754+
if (coveredInterval.firstPhaseId >= Phases.firstTransformPhase.id) return NoDenotation
752755
if (ctx.debug) traceInvalid(this)
753756
staleSymbolError
754757
}

Diff for: 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 }

Diff for: tests/pos-macros/i21844/SubClass.scala

+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

Diff for: tests/pos-macros/i21844/SuperClassWithLazyVal.scala

+2
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)