Skip to content

Commit 6bb218a

Browse files
Merge pull request #8221 from som-snytt/issue/7934
ParseResult ctx gets a compilation unit
2 parents 3f919dd + bb78dae commit 6bb218a

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package dotty.tools
22
package repl
33

4-
import dotc.reporting.diagnostic.MessageContainer
4+
import dotc.CompilationUnit
5+
import dotc.ast.untpd
56
import dotc.core.Contexts.Context
7+
import dotc.core.StdNames.str
68
import dotc.parsing.Parsers.Parser
79
import dotc.parsing.Tokens
10+
import dotc.reporting.diagnostic.MessageContainer
811
import dotc.util.SourceFile
9-
import dotc.ast.untpd
10-
import dotty.tools.dotc.core.StdNames.str
1112

1213
import scala.annotation.internal.sharable
1314

@@ -148,18 +149,21 @@ object ParseResult {
148149
def apply(sourceCode: String)(implicit state: State): ParseResult =
149150
apply(SourceFile.virtual(str.REPL_SESSION_LINE + (state.objectIndex + 1), sourceCode))
150151

151-
/** Check if the input is incomplete
152+
/** Check if the input is incomplete.
152153
*
153154
* This can be used in order to check if a newline can be inserted without
154-
* having to evaluate the expression
155+
* having to evaluate the expression.
155156
*/
156157
def isIncomplete(sourceCode: String)(implicit ctx: Context): Boolean =
157158
sourceCode match {
158159
case CommandExtract(_) | "" => false
159160
case _ => {
160161
val reporter = newStoreReporter
161-
val source = SourceFile.virtual("<incomplete-handler>", sourceCode)
162-
val localCtx = ctx.fresh.setSource(source).setReporter(reporter)
162+
val source = SourceFile.virtual("<incomplete-handler>", sourceCode)
163+
val unit = CompilationUnit(source, mustExist = false)
164+
val localCtx = ctx.fresh
165+
.setCompilationUnit(unit)
166+
.setReporter(reporter)
163167
var needsMore = false
164168
reporter.withIncompleteHandler((_, _) => needsMore = true) {
165169
parseStats(localCtx)

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.repl
22

3-
import org.junit.Assert._
3+
import org.junit.Assert.{assertTrue => assert, _}
44
import org.junit.{Ignore, Test}
55

66
class ReplCompilerTests extends ReplTest {
@@ -85,33 +85,33 @@ class ReplCompilerTests extends ReplTest {
8585
@Ignore @Test def i3305: Unit = {
8686
fromInitialState { implicit s =>
8787
run("null.toString")
88-
assertTrue(storedOutput().startsWith("java.lang.NullPointerException"))
88+
assert(storedOutput().startsWith("java.lang.NullPointerException"))
8989
}
9090

9191
fromInitialState { implicit s =>
9292
run("def foo: Int = 1 + foo; foo")
93-
assertTrue(storedOutput().startsWith("def foo: Int\njava.lang.StackOverflowError"))
93+
assert(storedOutput().startsWith("def foo: Int\njava.lang.StackOverflowError"))
9494
}
9595

9696
fromInitialState { implicit s =>
9797
run("""throw new IllegalArgumentException("Hello")""")
98-
assertTrue(storedOutput().startsWith("java.lang.IllegalArgumentException: Hello"))
98+
assert(storedOutput().startsWith("java.lang.IllegalArgumentException: Hello"))
9999
}
100100

101101
fromInitialState { implicit s =>
102102
run("val (x, y) = null")
103-
assertTrue(storedOutput().startsWith("scala.MatchError: null"))
103+
assert(storedOutput().startsWith("scala.MatchError: null"))
104104
}
105105
}
106106

107107
@Test def i2789: Unit = fromInitialState { implicit state =>
108108
run("(x: Int) => println(x)")
109-
assertTrue(storedOutput().startsWith("val res0: Int => Unit ="))
109+
assert(storedOutput().startsWith("val res0: Int => Unit ="))
110110
}
111111

112112
@Test def byNameParam: Unit = fromInitialState { implicit state =>
113113
run("def f(g: => Int): Int = g")
114-
assertTrue(storedOutput().startsWith("def f(g: => Int): Int"))
114+
assert(storedOutput().startsWith("def f(g: => Int): Int"))
115115
}
116116

117117
@Test def i4051 = fromInitialState { implicit state =>
@@ -163,9 +163,14 @@ class ReplCompilerTests extends ReplTest {
163163
storedOutput().trim
164164
)
165165
run("IntOrd")
166-
assertTrue(storedOutput().startsWith("val res0: IntOrd.type ="))
166+
assert(storedOutput().startsWith("val res0: IntOrd.type ="))
167167
}
168168

169+
@Test def i7934: Unit = fromInitialState { state =>
170+
implicit val ctx = state.context
171+
assertFalse(ParseResult.isIncomplete("_ + 1")) // was: assertThrows[NullPointerException]
172+
}
173+
169174
@Test def testSingletonPrint = fromInitialState { implicit state =>
170175
run("""val a = "hello"; val x: a.type = a""")
171176
assertEquals("val a: String = hello\nval x: a.type = hello", storedOutput().trim)

0 commit comments

Comments
 (0)