Skip to content

Commit e6f678b

Browse files
committed
Move typechecking to throwaway class
1 parent 38e2649 commit e6f678b

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

repl/src/dotty/tools/repl/ReplCompiler.scala

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dotc.util.SourceFile
1111
import dotc.typer.FrontEnd
1212
import backend.jvm.GenBCode
1313
import dotc.core.Contexts.Context
14+
import dotc.util.Positions._
1415
import dotc.reporting._
1516
import io._
1617

@@ -57,35 +58,36 @@ class ReplCompiler(ictx: Context) extends Compiler {
5758
Phases.replace(classOf[GenBCode], _ => new REPLGenBCode :: Nil, replPhases)
5859
}
5960

60-
def wrapped(trees: Seq[untpd.Tree], currentRes: Int)(implicit ctx: Context): (NextRes, untpd.ModuleDef) = {
61+
def freeToAssigned(trees: Seq[untpd.Tree], currentRes: Int)
62+
(implicit ctx: Context): (NextRes, Seq[untpd.Tree]) = {
6163
import untpd._
64+
6265
def freeExpression(t: Tree) =
6366
t.isTerm && !t.isInstanceOf[Assign]
6467

6568
val (exps, other) = trees.partition(freeExpression)
66-
6769
val resX = exps.zipWithIndex.map { (exp, i) =>
6870
ValDef(s"res${i + currentRes}".toTermName, TypeTree(), exp)
71+
.withPos(exp.pos)
6972
}
7073

71-
val moduleIndex = resX.length + currentRes
72-
val module = {
73-
val stats = if (trees.isEmpty) List(EmptyTree) else resX ++ other
74-
val tmpl = Template(emptyConstructor, Nil, EmptyValDef, stats)
75-
val moduleName = s"EncapsulatedRes$moduleIndex".toTermName
74+
(currentRes + resX.length, resX ++ other)
75+
}
7676

77-
ModuleDef(moduleName, tmpl).withFlags(Module)
78-
}
77+
def wrapped(trees: Seq[untpd.Tree])(implicit ctx: Context): untpd.TypeDef = {
78+
import untpd._
7979

80-
(moduleIndex + 1, module)
80+
val tmpl = Template(emptyConstructor, Nil, EmptyValDef, trees)
81+
TypeDef("TypeCheckingContext".toTypeName, tmpl)
82+
.withPos(Position(trees.head.pos.start, trees.last.pos.end))
8183
}
8284

8385
def compile(parsed: Parsed, currentRes: Int)(implicit ctx: Context): NextRes = {
8486
val reporter = new StoreReporter(null) with UniqueMessagePositions with HideNonSensicalMessages
8587

8688
val unit = new CompilationUnit(new SourceFile(s"repl-run-$currentRes", parsed.sourceCode))
87-
val (newRes, tree) = wrapped(parsed.trees, currentRes)
88-
unit.untpdTree = tree
89+
val (newRes, trees) = freeToAssigned(parsed.trees, currentRes)
90+
unit.untpdTree = wrapped(trees)
8991

9092
val run = newRun(ctx.fresh.setReporter(reporter))
9193
run.compileUnits(unit :: Nil)

0 commit comments

Comments
 (0)