Skip to content

Commit

Permalink
Fix #14701: avoid REPL crash when input is of the form val _ = ???
Browse files Browse the repository at this point in the history
The REPL should not crash when the right hand side of `val _ =`
throws a non-fatal error that is a subclass of java.lang.Error.
  • Loading branch information
griggt committed Mar 16, 2022
1 parent 4f1ad71 commit 0b0f626
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/repl/Rendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,15 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {

/** Force module initialization in the absence of members. */
def forceModule(sym: Symbol)(using Context): Seq[Diagnostic] =
import scala.util.control.NonFatal
def load() =
val objectName = sym.fullName.encode.toString
Class.forName(objectName, true, classLoader())
Nil
try load() catch case e: ExceptionInInitializerError => List(renderError(e, sym.denot))
try load()
catch
case e: ExceptionInInitializerError => List(renderError(e, sym.denot))
case NonFatal(e) => List(renderError(InvocationTargetException(e), sym.denot))

/** Render the stack trace of the underlying exception. */
def renderError(ite: InvocationTargetException | ExceptionInInitializerError, d: Denotation)(using Context): Diagnostic =
Expand Down
13 changes: 13 additions & 0 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,19 @@ class ReplCompilerTests extends ReplTest:
assertEquals("scala.MatchError: hi (of class java.lang.String)", all.head)
}

@Test def i14701 = initially {
val state = run("val _ = ???")
val all = lines()
assertEquals(3, all.length)
assertEquals("scala.NotImplementedError: an implementation is missing", all.head)
state
} andThen {
run("val _ = assert(false)")
val all = lines()
assertEquals(3, all.length)
assertEquals("java.lang.AssertionError: assertion failed", all.head)
}

@Test def i14491 =
initially {
run("import language.experimental.fewerBraces")
Expand Down

0 comments on commit 0b0f626

Please sign in to comment.