Skip to content

Commit ccab4eb

Browse files
liufengyunEnzeXing
authored andcommitted
Synchronize Symbol.defTree before analysis
1 parent fef4ddf commit ccab4eb

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class Compiler {
6464
new CheckStatic, // Check restrictions that apply to @static members
6565
new BetaReduce, // Reduce closure applications
6666
new InlineVals, // Check right hand-sides of an `inline val`s
67-
new ExpandSAMs, // Expand single abstract method closures to anonymous classes
68-
new init.Checker) :: // Check initialization of objects
67+
new ExpandSAMs) :: // Expand single abstract method closures to anonymous classes
68+
List(new init.Checker) :: // Check initialization of objects
6969
List(new ElimRepeated, // Rewrite vararg parameters and arguments
7070
new ProtectedAccessors, // Add accessors for protected members
7171
new ExtensionMethods, // Expand methods of value classes with extension methods

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ object Phases {
290290
/** If set, implicit search is enabled */
291291
def allowsImplicitSearch: Boolean = false
292292

293-
/** List of names of phases that should precede this phase */
293+
/** List of names of phases that should precede this phase */
294294
def runsAfter: Set[String] = Set.empty
295295

296296
/** @pre `isRunnable` returns true */

compiler/src/dotty/tools/dotc/transform/init/Checker.scala

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import dotty.tools.dotc.core._
1010
import Contexts._
1111
import Types._
1212
import Symbols._
13+
import StdNames._
1314

1415
import dotty.tools.dotc.transform._
15-
import MegaPhase._
16+
import Phases._
1617

1718

1819
import scala.collection.mutable
1920

2021

21-
class Checker extends MiniPhase {
22+
class Checker extends Phase {
2223
import tpd._
2324

2425
val phaseName = "initChecker"
@@ -30,9 +31,34 @@ class Checker extends MiniPhase {
3031
override def isEnabled(using Context): Boolean =
3132
super.isEnabled && ctx.settings.YcheckInit.value
3233

33-
override def transformTypeDef(tree: TypeDef)(using Context): tpd.Tree = {
34-
if (!tree.isClassDef) return tree
34+
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
35+
units.foreach { unit => traverser.traverse(unit.tpdTree) }
36+
super.runOn(units)
37+
38+
val traverser = new TreeTraverser {
39+
override def traverse(tree: Tree)(using Context): Unit =
40+
tree match {
41+
case tdef: MemberDef =>
42+
// self-type annotation ValDef has no symbol
43+
if tdef.name != nme.WILDCARD then
44+
tdef.symbol.defTree = tree
45+
case _ =>
46+
traverseChildren(tree)
47+
}
48+
}
49+
50+
override def run(using Context): Unit = {
51+
val unit = ctx.compilationUnit
52+
unit.tpdTree.foreachSubTree {
53+
case tdef: TypeDef if tdef.isClassDef =>
54+
transformTypeDef(tdef)
55+
56+
case _ =>
57+
}
58+
}
59+
3560

61+
private def transformTypeDef(tree: TypeDef)(using Context): tpd.Tree = {
3662
val cls = tree.symbol.asClass
3763
val instantiable: Boolean =
3864
cls.is(Flags.Module) ||

0 commit comments

Comments
 (0)