Skip to content

Commit d2ed41e

Browse files
committed
move lateCompile implementation to Namer
1 parent 1f81426 commit d2ed41e

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

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

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import rewrites.Rewrites
2323

2424
import profile.Profiler
2525
import printing.XprintMode
26-
import parsing.Parsers.Parser
27-
import parsing.JavaParsers.JavaParser
2826
import typer.ImplicitRunInfo
2927
import config.Feature
3028
import StdNames.nme
@@ -302,31 +300,16 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
302300
.setCompilationUnit(unit)
303301
.withRootImports
304302

305-
def process()(using Context) = {
306-
307-
def enterTrees()(using Context) =
308-
ctx.typer.lateEnter(unit.untpdTree)
309-
def typeCheckUnit()(using Context) =
310-
unit.tpdTree = ctx.typer.typedExpr(unit.untpdTree)
311-
val phase = new transform.SetRootTree()
312-
phase.run
303+
def process()(using Context) =
304+
ctx.typer.lateEnterUnit(doTypeCheck =>
313305
if typeCheck then
314-
val typerCtx: Context =
315-
// typer phase allows implicits to be searched
316-
ctx.withPhase(Phases.typerPhase)
317-
if compiling then finalizeActions += (() => typeCheckUnit()(using typerCtx))
318-
else typeCheckUnit()(using typerCtx)
319-
320-
unit.untpdTree =
321-
if (unit.isJava) new JavaParser(unit.source).parse()
322-
else new Parser(unit.source).parse()
323-
val namerCtx =
324-
// inline body annotations are set in namer, capturing the current context
325-
// we need to prepare the context for inlining.
326-
if unit.isJava then ctx else PrepareInlineable.initContext(ctx)
327-
enterTrees()(using namerCtx)
306+
if compiling then finalizeActions += doTypeCheck
307+
else doTypeCheck()
308+
)
309+
310+
inContext(unitCtx) {
311+
process()
328312
}
329-
process()(using unitCtx)
330313
}
331314

332315
private sealed trait PrintedTree

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import tpd.tpes
1818
import Variances.alwaysInvariant
1919
import config.{Config, Feature}
2020
import config.Printers.typr
21+
import parsing.JavaParsers.JavaParser
22+
import parsing.Parsers.Parser
2123
import Annotations._
2224
import Inferencing._
2325
import transform.ValueClasses._
@@ -708,15 +710,41 @@ class Namer { typer: Typer =>
708710
ctxWithStats
709711
}
710712

711-
/** Index symbols in `tree` while asserting the `lateCompile` flag.
712-
* This will cause any old top-level symbol with the same fully qualified
713-
* name as a newly created symbol to be replaced.
714-
*/
715-
def lateEnter(tree: Tree)(using Context): Context = {
716-
val saved = lateCompile
717-
lateCompile = true
718-
try index(tree :: Nil) finally lateCompile = saved
719-
}
713+
714+
def lateEnterUnit(typeCheckCB: (() => Unit) => Unit)(using Context) =
715+
val unit = ctx.compilationUnit
716+
717+
/** Index symbols in `tree` while asserting the `lateCompile` flag.
718+
* This will cause any old top-level symbol with the same fully qualified
719+
* name as a newly created symbol to be replaced.
720+
*/
721+
def lateEnter()(using Context): Context = {
722+
val saved = lateCompile
723+
lateCompile = true
724+
try index(unit.untpdTree :: Nil) finally lateCompile = saved
725+
}
726+
727+
/** Set the tpdTree and root tree of the compilation unit
728+
*/
729+
def lateTypeCheck()(using Context) =
730+
unit.tpdTree = typer.typedExpr(unit.untpdTree)
731+
val phase = new transform.SetRootTree()
732+
phase.run
733+
734+
unit.untpdTree =
735+
if (unit.isJava) new JavaParser(unit.source).parse()
736+
else new Parser(unit.source).parse()
737+
738+
inContext(PrepareInlineable.initContext(ctx)) {
739+
// inline body annotations are set in namer, capturing the current context
740+
// we need to prepare the context for inlining.
741+
lateEnter()
742+
typeCheckCB { () =>
743+
atPhase(Phases.typerPhase) {
744+
lateTypeCheck()
745+
}
746+
}
747+
}
720748

721749
/** The type bound on wildcard imports of an import list, with special values
722750
* Nothing if no wildcard imports of this kind exist

0 commit comments

Comments
 (0)