Skip to content

Commit 573b083

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

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
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: 39 additions & 8 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,44 @@ 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.
713+
/** Parse the source and index symbols in the compilation unit's untpdTree
714+
* while asserting the `lateCompile` flag. This will cause any old
715+
* top-level symbol with the same fully qualified name as a newly created
716+
* symbol to be replaced.
717+
*
718+
* Will call the callback with an implementation of type checking
719+
* That will set the tpdTree and root tree for the compilation unit.
714720
*/
715-
def lateEnter(tree: Tree)(using Context): Context = {
716-
val saved = lateCompile
717-
lateCompile = true
718-
try index(tree :: Nil) finally lateCompile = saved
719-
}
721+
def lateEnterUnit(typeCheckCB: (() => Unit) => Unit)(using Context) =
722+
val unit = ctx.compilationUnit
723+
724+
/** Index symbols in unit.untpdTree with lateCompile flag = true */
725+
def lateEnter()(using Context): Context =
726+
val saved = lateCompile
727+
lateCompile = true
728+
try index(unit.untpdTree :: Nil) finally lateCompile = saved
729+
730+
/** Set the tpdTree and root tree of the compilation unit */
731+
def lateTypeCheck()(using Context) =
732+
unit.tpdTree = typer.typedExpr(unit.untpdTree)
733+
val phase = new transform.SetRootTree()
734+
phase.run
735+
736+
unit.untpdTree =
737+
if (unit.isJava) new JavaParser(unit.source).parse()
738+
else new Parser(unit.source).parse()
739+
740+
atPhase(Phases.typerPhase) {
741+
inContext(PrepareInlineable.initContext(ctx)) {
742+
// inline body annotations are set in namer, capturing the current context
743+
// we need to prepare the context for inlining.
744+
lateEnter()
745+
typeCheckCB { () =>
746+
lateTypeCheck()
747+
}
748+
}
749+
}
750+
end lateEnterUnit
720751

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

0 commit comments

Comments
 (0)