From bb78daed22c0c15fa807ae0298a8a841e6c61e50 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Wed, 5 Feb 2020 23:22:18 -0800 Subject: [PATCH] ParseResult ctx gets a compilation unit Fix #7934 --- .../src/dotty/tools/repl/ParseResult.scala | 18 +++++++++------- .../dotty/tools/repl/ReplCompilerTests.scala | 21 ++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/compiler/src/dotty/tools/repl/ParseResult.scala b/compiler/src/dotty/tools/repl/ParseResult.scala index 8b511cb4c021..e81370fd894f 100644 --- a/compiler/src/dotty/tools/repl/ParseResult.scala +++ b/compiler/src/dotty/tools/repl/ParseResult.scala @@ -1,13 +1,14 @@ package dotty.tools package repl -import dotc.reporting.diagnostic.MessageContainer +import dotc.CompilationUnit +import dotc.ast.untpd import dotc.core.Contexts.Context +import dotc.core.StdNames.str import dotc.parsing.Parsers.Parser import dotc.parsing.Tokens +import dotc.reporting.diagnostic.MessageContainer import dotc.util.SourceFile -import dotc.ast.untpd -import dotty.tools.dotc.core.StdNames.str import scala.annotation.internal.sharable @@ -148,18 +149,21 @@ object ParseResult { def apply(sourceCode: String)(implicit state: State): ParseResult = apply(SourceFile.virtual(str.REPL_SESSION_LINE + (state.objectIndex + 1), sourceCode)) - /** Check if the input is incomplete + /** Check if the input is incomplete. * * This can be used in order to check if a newline can be inserted without - * having to evaluate the expression + * having to evaluate the expression. */ def isIncomplete(sourceCode: String)(implicit ctx: Context): Boolean = sourceCode match { case CommandExtract(_) | "" => false case _ => { val reporter = newStoreReporter - val source = SourceFile.virtual("", sourceCode) - val localCtx = ctx.fresh.setSource(source).setReporter(reporter) + val source = SourceFile.virtual("", sourceCode) + val unit = CompilationUnit(source, mustExist = false) + val localCtx = ctx.fresh + .setCompilationUnit(unit) + .setReporter(reporter) var needsMore = false reporter.withIncompleteHandler((_, _) => needsMore = true) { parseStats(localCtx) diff --git a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala index 5818162121b1..7eca7614c49b 100644 --- a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala +++ b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala @@ -1,6 +1,6 @@ package dotty.tools.repl -import org.junit.Assert._ +import org.junit.Assert.{assertTrue => assert, _} import org.junit.{Ignore, Test} class ReplCompilerTests extends ReplTest { @@ -85,33 +85,33 @@ class ReplCompilerTests extends ReplTest { @Ignore @Test def i3305: Unit = { fromInitialState { implicit s => run("null.toString") - assertTrue(storedOutput().startsWith("java.lang.NullPointerException")) + assert(storedOutput().startsWith("java.lang.NullPointerException")) } fromInitialState { implicit s => run("def foo: Int = 1 + foo; foo") - assertTrue(storedOutput().startsWith("def foo: Int\njava.lang.StackOverflowError")) + assert(storedOutput().startsWith("def foo: Int\njava.lang.StackOverflowError")) } fromInitialState { implicit s => run("""throw new IllegalArgumentException("Hello")""") - assertTrue(storedOutput().startsWith("java.lang.IllegalArgumentException: Hello")) + assert(storedOutput().startsWith("java.lang.IllegalArgumentException: Hello")) } fromInitialState { implicit s => run("val (x, y) = null") - assertTrue(storedOutput().startsWith("scala.MatchError: null")) + assert(storedOutput().startsWith("scala.MatchError: null")) } } @Test def i2789: Unit = fromInitialState { implicit state => run("(x: Int) => println(x)") - assertTrue(storedOutput().startsWith("val res0: Int => Unit =")) + assert(storedOutput().startsWith("val res0: Int => Unit =")) } @Test def byNameParam: Unit = fromInitialState { implicit state => run("def f(g: => Int): Int = g") - assertTrue(storedOutput().startsWith("def f(g: => Int): Int")) + assert(storedOutput().startsWith("def f(g: => Int): Int")) } @Test def i4051 = fromInitialState { implicit state => @@ -163,9 +163,14 @@ class ReplCompilerTests extends ReplTest { storedOutput().trim ) run("IntOrd") - assertTrue(storedOutput().startsWith("val res0: IntOrd.type =")) + assert(storedOutput().startsWith("val res0: IntOrd.type =")) } + @Test def i7934: Unit = fromInitialState { state => + implicit val ctx = state.context + assertFalse(ParseResult.isIncomplete("_ + 1")) // was: assertThrows[NullPointerException] + } + @Test def testSingletonPrint = fromInitialState { implicit state => run("""val a = "hello"; val x: a.type = a""") assertEquals("val a: String = hello\nval x: a.type = hello", storedOutput().trim)