Skip to content

Commit db959dc

Browse files
Fix #5895: REPL autocompletion crushes in certain cases
The crashes happen due to the fact that in certain cases, certain trees get mispositioned during code completion. E.g. this happens with `opaque type T = Int` or `object Foo { type T = Int`. The tree spans are set incorrectly because repl doesn't set the sources of these trees correctly. Precisely, when typechecking on code completion, a virtual source is used. However, when parsing for autocompletion, a line source is used which is created by the parsing logic. This commit makes sure that REPL is consistent in its source choices when computing code completions.
1 parent cab020d commit db959dc

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ object ParseResult {
115115
stats
116116
}
117117

118-
/** Extract a `ParseResult` from the string `sourceCode` */
119-
def apply(sourceCode: String)(implicit state: State): ParseResult =
118+
def apply(source: SourceFile)(implicit state: State): ParseResult = {
119+
val sourceCode = source.content().mkString
120120
sourceCode match {
121121
case "" => Newline
122122
case CommandExtract(cmd, arg) => cmd match {
@@ -132,8 +132,6 @@ object ParseResult {
132132
case _ =>
133133
implicit val ctx: Context = state.context
134134

135-
val source = SourceFile.virtual(str.REPL_SESSION_LINE + (state.objectIndex + 1), sourceCode)
136-
137135
val reporter = newStoreReporter
138136
val stats = parseStats(sourceCode)(state.context.fresh.setReporter(reporter).withSource(source))
139137

@@ -145,6 +143,10 @@ object ParseResult {
145143
else
146144
Parsed(source, stats)
147145
}
146+
}
147+
148+
def apply(sourceCode: String)(implicit state: State): ParseResult =
149+
apply(SourceFile.virtual(str.REPL_SESSION_LINE + (state.objectIndex + 1), sourceCode))
148150

149151
/** Check if the input is incomplete
150152
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class ReplCompiler extends Compiler {
241241
PackageDef(Ident(nme.EMPTY_PACKAGE), List(wrapper))
242242
}
243243

244-
ParseResult(expr)(state) match {
244+
ParseResult(sourceFile)(state) match {
245245
case Parsed(_, trees) =>
246246
wrap(trees).result
247247
case SyntaxErrors(_, reported, trees) =>

compiler/test/dotty/tools/repl/TabcompleteTests.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,8 @@ class TabcompleteTests extends ReplTest {
124124
val comp = tabComplete("???.")
125125
assertEquals(Nil, comp)
126126
}
127+
128+
@Test def moduleCompletion = fromInitialState { implicit s =>
129+
assertEquals(List("Predef"), tabComplete("object Foo { type T = Pre"))
130+
}
127131
}

0 commit comments

Comments
 (0)