Skip to content

Commit c606153

Browse files
committed
Use the same virtual file for parsing and compiling in REPL
1 parent 90d2cb0 commit c606153

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

compiler/src/dotty/tools/dotc/ast/untpd.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
2525
*/
2626
abstract case class TypedSplice(splice: tpd.Tree)(val owner: Symbol)(implicit @transientParam src: SourceFile) extends ProxyTree {
2727
def forwardTo: tpd.Tree = splice
28-
span = splice.span
2928
}
3029

3130
object TypedSplice {

compiler/src/dotty/tools/repl/ParseResult.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import dotc.parsing.Parsers.Parser
77
import dotc.parsing.Tokens
88
import dotc.util.SourceFile
99
import dotc.ast.untpd
10-
10+
import dotty.tools.dotc.core.StdNames.str
1111

1212
import scala.annotation.internal.sharable
1313

1414
/** A parsing result from string input */
1515
sealed trait ParseResult
1616

1717
/** An error free parsing resulting in a list of untyped trees */
18-
case class Parsed(sourceCode: String, trees: List[untpd.Tree]) extends ParseResult
18+
case class Parsed(source: SourceFile, trees: List[untpd.Tree]) extends ParseResult
1919

2020
/** A parsing result containing syntax `errors` */
2121
case class SyntaxErrors(sourceCode: String,
@@ -109,13 +109,14 @@ object ParseResult {
109109
@sharable private[this] val CommandExtract = """(:[\S]+)\s*(.*)""".r
110110

111111
private def parseStats(sourceCode: String)(implicit ctx: Context): List[untpd.Tree] = {
112-
val source = SourceFile.virtual("<console>", sourceCode)
113-
val parser = new Parser(source)(ctx.withSource(source))
112+
val parser = new Parser(ctx.source)
114113
val stats = parser.blockStatSeq()
115114
parser.accept(Tokens.EOF)
116115
stats
117116
}
118117

118+
private[this] var replSessionNumber = 0
119+
119120
/** Extract a `ParseResult` from the string `sourceCode` */
120121
def apply(sourceCode: String)(implicit ctx: Context): ParseResult =
121122
sourceCode match {
@@ -131,16 +132,19 @@ object ParseResult {
131132
case _ => UnknownCommand(cmd)
132133
}
133134
case _ =>
135+
replSessionNumber += 1
136+
val source = SourceFile.virtual(str.REPL_SESSION_LINE + replSessionNumber, sourceCode)
137+
134138
val reporter = newStoreReporter
135-
val stats = parseStats(sourceCode)(ctx.fresh.setReporter(reporter))
139+
val stats = parseStats(sourceCode)(ctx.fresh.setReporter(reporter).withSource(source))
136140

137141
if (reporter.hasErrors)
138142
SyntaxErrors(
139143
sourceCode,
140144
reporter.removeBufferedMessages,
141145
stats)
142146
else
143-
Parsed(sourceCode, stats)
147+
Parsed(source, stats)
144148
}
145149

146150
/** Check if the input is incomplete

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ class ReplCompiler extends Compiler {
6464
}
6565

6666
private[this] val objectNames = mutable.Map.empty[Int, TermName]
67-
private def objectName(state: State) =
68-
objectNames.getOrElseUpdate(state.objectIndex,
69-
(str.REPL_SESSION_LINE + state.objectIndex).toTermName)
7067

7168
private case class Definitions(stats: List[untpd.Tree], state: State)
7269

@@ -126,23 +123,28 @@ class ReplCompiler extends Compiler {
126123
* }
127124
* ```
128125
*/
129-
private def wrapped(defs: Definitions): untpd.PackageDef = {
126+
private def wrapped(defs: Definitions, objectTermName: TermName): untpd.PackageDef = {
130127
import untpd._
131128

132129
assert(defs.stats.nonEmpty)
133130

134131
implicit val ctx: Context = defs.state.context
135132

136133
val tmpl = Template(emptyConstructor, Nil, EmptyValDef, defs.stats)
137-
val module = ModuleDef(objectName(defs.state), tmpl)
134+
val module = ModuleDef(objectTermName, tmpl)
138135
.withSpan(Span(0, defs.stats.last.span.end))
139136

140137
PackageDef(Ident(nme.EMPTY_PACKAGE), List(module))
141138
}
142139

143-
private def createUnit(defs: Definitions, sourceCode: String)(implicit ctx: Context): CompilationUnit = {
144-
val unit = CompilationUnit(SourceFile.virtual(objectName(defs.state).toString, sourceCode))
145-
unit.untpdTree = wrapped(defs)
140+
private def createUnit(defs: Definitions)(implicit ctx: Context): CompilationUnit = {
141+
val objectName = ctx.source.file.toString
142+
assert(objectName.startsWith(str.REPL_SESSION_LINE))
143+
val objectTermName = ctx.source.file.toString.toTermName
144+
objectNames.update(defs.state.objectIndex, objectTermName)
145+
146+
val unit = CompilationUnit(ctx.source)
147+
unit.untpdTree = wrapped(defs, objectTermName)
146148
unit
147149
}
148150

@@ -156,7 +158,7 @@ class ReplCompiler extends Compiler {
156158

157159
final def compile(parsed: Parsed)(implicit state: State): Result[(CompilationUnit, State)] = {
158160
val defs = definitions(parsed.trees, state)
159-
val unit = createUnit(defs, parsed.sourceCode)(state.context)
161+
val unit = createUnit(defs)(state.context)
160162
runCompilationUnit(unit, defs.state)
161163
}
162164

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ class ReplDriver(settings: Array[String],
208208
def extractTopLevelImports(ctx: Context): List[tpd.Import] =
209209
ctx.phases.collectFirst { case phase: CollectTopLevelImports => phase.imports }.get
210210

211-
implicit val state = newRun(istate)
211+
implicit val state = {
212+
val state0 = newRun(istate)
213+
state0.copy(context = state0.context.withSource(parsed.source))
214+
}
212215
compiler
213216
.compile(parsed)
214217
.fold(

0 commit comments

Comments
 (0)