Skip to content

Commit 69cf220

Browse files
committed
Put position check into a phase
1 parent f8fa97c commit 69cf220

File tree

4 files changed

+66
-39
lines changed

4 files changed

+66
-39
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Compiler {
3737
/** Phases dealing with the frontend up to trees ready for TASTY pickling */
3838
protected def frontendPhases: List[List[Phase]] =
3939
List(new FrontEnd) :: // Compiler frontend: scanner, parser, namer, typer
40+
List(new YCheckPositions) :: // YCheck positions
4041
List(new Staging) :: // Check PCP, heal quoted types and expand macros
4142
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
4243
List(new PostTyper) :: // Additional checks and cleanups after type checking

compiler/src/dotty/tools/dotc/transform/Staging.scala

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,44 +62,6 @@ class Staging extends MacroTransform {
6262
case _ =>
6363
}
6464
}
65-
if (ctx.phase <= ctx.erasurePhase) {
66-
tree match {
67-
case PackageDef(pid, _) if tree.symbol.owner == defn.RootClass =>
68-
new TreeTraverser {
69-
private[this] var sources: List[SourceFile] = ctx.source :: Nil
70-
def traverse(tree: tpd.Tree)(implicit ctx: Context): Unit = {
71-
assert(ctx.source == sources.head)
72-
if (!tree.isEmpty && !tree.isInstanceOf[untpd.TypedSplice] && ctx.typerState.isGlobalCommittable) {
73-
if (!tree.isType) { // TODO also check types, currently we do not add Inlined(EmptyTree, _, _) for types. We should.
74-
val currentSource = sources.head
75-
assert(tree.source == currentSource, i"wrong source set for $tree # ${tree.uniqueId} of ${tree.getClass}, set to ${tree.source} but context had $currentSource")
76-
}
77-
}
78-
tree match {
79-
case Inlined(EmptyTree, bindings, expansion) =>
80-
assert(bindings.isEmpty)
81-
val old = sources
82-
sources = old.tail
83-
traverse(expansion)(inlineContext(EmptyTree))
84-
sources = old
85-
case Inlined(call, bindings, expansion) =>
86-
bindings.foreach(traverse(_))
87-
sources = call.symbol.topLevelClass.source :: sources
88-
if (
89-
!( // FIXME macro implementations can drop Inlined nodes. We should reinsert them after macro expansion based on the positions of the trees
90-
((ctx.phase <= ctx.typerPhase.next) && call.symbol.is(Macro)) ||
91-
(!(ctx.phase <= ctx.typerPhase.next) && call.symbol.unforcedDecls.exists(_.is(Macro)) || call.symbol.unforcedDecls.toList.exists(_.is(Macro)))
92-
)
93-
) traverse(expansion)(inlineContext(call))
94-
sources = sources.tail
95-
case _ => traverseChildren(tree)
96-
}
97-
}
98-
}.traverse(tree)
99-
case _ =>
100-
}
101-
102-
}
10365
}
10466

10567
override def run(implicit ctx: Context): Unit =
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package dotty.tools.dotc
2+
package transform
3+
4+
import dotty.tools.dotc.ast.Trees._
5+
import dotty.tools.dotc.ast.{tpd, untpd}
6+
import dotty.tools.dotc.core.Contexts._
7+
import dotty.tools.dotc.core.Decorators._
8+
import dotty.tools.dotc.core.Flags._
9+
import dotty.tools.dotc.core.Phases
10+
import dotty.tools.dotc.core.Symbols._
11+
import dotty.tools.dotc.util.{SourceFile, SourcePosition}
12+
13+
/** Ycheck inlined positions */
14+
class YCheckPositions extends Phases.Phase {
15+
import tpd._
16+
17+
def phaseName: String = "inlinedPositions"
18+
19+
override def run(implicit ctx: Context): Unit = () // YCheck only
20+
21+
override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = {
22+
tree match {
23+
case PackageDef(pid, _) if tree.symbol.owner == defn.RootClass =>
24+
new TreeTraverser {
25+
private[this] var sources: List[SourceFile] = ctx.source :: Nil
26+
def traverse(tree: tpd.Tree)(implicit ctx: Context): Unit = {
27+
28+
// Check current context is correct
29+
assert(ctx.source == sources.head)
30+
if (!tree.isEmpty && !tree.isInstanceOf[untpd.TypedSplice] && ctx.typerState.isGlobalCommittable) {
31+
if (!tree.isType) { // TODO also check types, currently we do not add Inlined(EmptyTree, _, _) for types. We should.
32+
val currentSource = sources.head
33+
assert(tree.source == currentSource, i"wrong source set for $tree # ${tree.uniqueId} of ${tree.getClass}, set to ${tree.source} but context had $currentSource")
34+
}
35+
}
36+
37+
// Recursivlely check children while keeping track of current source
38+
tree match {
39+
case Inlined(EmptyTree, bindings, expansion) =>
40+
assert(bindings.isEmpty)
41+
val old = sources
42+
sources = old.tail
43+
traverse(expansion)(inlineContext(EmptyTree))
44+
sources = old
45+
case Inlined(call, bindings, expansion) =>
46+
bindings.foreach(traverse(_))
47+
sources = call.symbol.topLevelClass.source :: sources
48+
if (
49+
!( // FIXME macro implementations can drop Inlined nodes. We should reinsert them after macro expansion based on the positions of the trees
50+
((ctx.phase <= ctx.typerPhase.next) && call.symbol.is(Macro)) ||
51+
(!(ctx.phase <= ctx.typerPhase.next) && call.symbol.unforcedDecls.exists(_.is(Macro)) || call.symbol.unforcedDecls.toList.exists(_.is(Macro)))
52+
)
53+
) traverse(expansion)(inlineContext(call))
54+
sources = sources.tail
55+
case _ => traverseChildren(tree)
56+
}
57+
}
58+
}.traverse(tree)
59+
case _ =>
60+
}
61+
62+
}
63+
64+
}

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
415415
val inlineCtx = inlineContext(call).fresh.setTyper(inlineTyper).setNewScope
416416

417417
def inlinedFromOutside(tree: Tree)(span: Span): Tree =
418-
Inlined(EmptyTree, Nil, tree)(inlinedSourceCtx).withSpan(span)
418+
Inlined(EmptyTree, Nil, tree)(ctx.withSource(inlinedMethod.topLevelClass.source)).withSpan(span)
419419

420420
// A tree type map to prepare the inlined body for typechecked.
421421
// The translation maps references to `this` and parameters to

0 commit comments

Comments
 (0)