Skip to content

ParseResult ctx gets a compilation unit #8221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions compiler/src/dotty/tools/repl/ParseResult.scala
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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("<incomplete-handler>", sourceCode)
val localCtx = ctx.fresh.setSource(source).setReporter(reporter)
val source = SourceFile.virtual("<incomplete-handler>", sourceCode)
val unit = CompilationUnit(source, mustExist = false)
val localCtx = ctx.fresh
.setCompilationUnit(unit)
.setReporter(reporter)
var needsMore = false
reporter.withIncompleteHandler((_, _) => needsMore = true) {
parseStats(localCtx)
Expand Down
21 changes: 13 additions & 8 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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)
Expand Down