Skip to content

Commit 023303d

Browse files
committed
Fix #3309: Crash in REPL Auto-completion
When auto-completing in the REPL, we sometime tried to manipulate a tree with no position. Not anymore
1 parent b8d1e41 commit 023303d

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package repl
44
import dotc.reporting.diagnostic.MessageContainer
55
import dotc.core.Contexts.Context
66
import dotc.parsing.Parsers.Parser
7+
import dotc.parsing.Tokens
78
import dotc.util.SourceFile
89
import dotc.ast.untpd
910
import dotc.reporting._
@@ -97,6 +98,12 @@ object ParseResult {
9798

9899
@sharable private[this] val CommandExtract = """(:[\S]+)\s*(.*)""".r
99100

101+
private def parseStats(parser: Parser): List[untpd.Tree] = {
102+
val stats = parser.blockStatSeq()
103+
parser.accept(Tokens.EOF)
104+
stats
105+
}
106+
100107
/** Extract a `ParseResult` from the string `sourceCode` */
101108
def apply(sourceCode: String)(implicit ctx: Context): ParseResult =
102109
sourceCode match {
@@ -114,7 +121,7 @@ object ParseResult {
114121
val source = new SourceFile("<console>", sourceCode.toCharArray)
115122
val parser = new Parser(source)
116123

117-
val (_, stats) = parser.templateStatSeq()
124+
val stats = parseStats(parser)
118125

119126
if (ctx.reporter.hasErrors) {
120127
SyntaxErrors(sourceCode,
@@ -140,7 +147,7 @@ object ParseResult {
140147
reporter.withIncompleteHandler(_ => _ => needsMore = true) {
141148
val source = new SourceFile("<console>", sourceCode.toCharArray)
142149
val parser = new Parser(source)(ctx.fresh.setReporter(reporter))
143-
parser.templateStatSeq()
150+
parseStats(parser)
144151
!reporter.hasErrors && needsMore
145152
}
146153
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
238238
PackageDef(Ident(nme.EMPTY_PACKAGE),
239239
TypeDef("EvaluateExpr".toTypeName, tmpl)
240240
.withMods(new Modifiers(Final))
241-
.withPos(Position(trees.head.pos.start, trees.last.pos.end)) :: Nil
241+
.withPos(Position(0, expr.length)) :: Nil
242242
)
243243
}
244244

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ class TabcompleteTests extends ReplTest {
5252
assert(tabComplete(src2).suggestions.nonEmpty)
5353
}
5454
}
55+
56+
@Test def i3309: Unit =
57+
fromInitialState { implicit s =>
58+
List("\"", "#", ")", "=", "'", "¨", "£", ".", ":", ",", ";", "@", "}", "[", "]")
59+
.foreach(src => assertTrue(tabComplete(src).suggestions.isEmpty))
60+
}
5561
}

0 commit comments

Comments
 (0)