scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

Previous error in script engine fails compilation on first try #12833

Open som-snytt opened 11 months ago

som-snytt commented 11 months ago

Reproduction steps

Scala version: 2.13.11

scala> val c = intp.compile("""s"hey $bar"""", res11)
javax.script.ScriptException: compile-time error
  at scala.tools.nsc.interpreter.shell.Scripted.$anonfun$compile$1(Scripted.scala:154)
  at scala.tools.nsc.interpreter.shell.Scripted.withCompileContext(Scripted.scala:126)
  at scala.tools.nsc.interpreter.shell.Scripted.compile(Scripted.scala:138)
  ... 30 elided

scala> val c = intp.compile("""s"hey $bar"""", res11)
val c: javax.script.CompiledScript = scala.tools.nsc.interpreter.shell.Scripted$WrappedRequest@dc4aba1

scala> c.eval()
val res15: Object = hey 42

scala>

Problem

The compile worked only the second time. bar is an attribute in ENGINE_SCOPE.

It works this way:

➜  ~ scala -nobootcp
Welcome to Scala 2.13.11 (OpenJDK 64-Bit Server VM, Java 20.0.1).
Type in expressions for evaluation. Or try :help.

scala> val intp = scala.tools.nsc.interpreter.shell.Scripted()
val intp: scala.tools.nsc.interpreter.shell.Scripted = scala.tools.nsc.interpreter.shell.Scripted@8dc3019

scala> val bnd = intp.createBindings
val bnd: javax.script.Bindings = javax.script.SimpleBindings@232be28d

scala> bnd.put("foo", 42)
val res0: Object = null

scala> intp.eval("""s"hey $foo"""", bnd)
val res1: Object = hey 42

scala> val context = intp.getContext
val context: javax.script.ScriptContext = javax.script.SimpleScriptContext@9670873

scala> context.setAttribute("bar", 42, javax.script.ScriptContext.ENGINE_SCOPE)

scala> val c = intp.compile("""s"hey $bar"""", context)
val c: javax.script.CompiledScript = scala.tools.nsc.interpreter.shell.Scripted$WrappedRequest@2d4e6bb2

scala> c.eval()
val res3: Object = hey 42

scala>

It looks like previous errors cause the symptom:

scala> context.setAttribute("baz", 42, 0)
java.lang.IllegalArgumentException: Illegal scope value.
  at java.scripting/javax.script.SimpleScriptContext.setAttribute(SimpleScriptContext.java:256)
  ... 30 elided

scala> val c = intp.compile("""s"hey $baz"""", context)
javax.script.ScriptException: not found: value baz in s"hey $baz" at line number 1 at column number 8
  at scala.tools.nsc.interpreter.shell.Scripted.$anonfun$compile$2(Scripted.scala:153)
  at scala.tools.nsc.interpreter.shell.Scripted.$anonfun$compile$1(Scripted.scala:152)
  at scala.tools.nsc.interpreter.shell.Scripted.withCompileContext(Scripted.scala:126)
  at scala.tools.nsc.interpreter.shell.Scripted.compile(Scripted.scala:138)
  ... 30 elided

scala> context.setAttribute("baz", 42, javax.script.ScriptContext.ENGINE_SCOPE)

scala> val c = intp.compile("""s"hey $baz"""", context)
javax.script.ScriptException: compile-time error
  at scala.tools.nsc.interpreter.shell.Scripted.$anonfun$compile$1(Scripted.scala:154)
  at scala.tools.nsc.interpreter.shell.Scripted.withCompileContext(Scripted.scala:126)
  at scala.tools.nsc.interpreter.shell.Scripted.compile(Scripted.scala:138)
  ... 30 elided

scala> val c = intp.compile("""s"hey $baz"""", context)
val c: javax.script.CompiledScript = scala.tools.nsc.interpreter.shell.Scripted$WrappedRequest@51ddc08e

scala>