Is there a way to get proper report of runtime compilation errors in scala 2.10?
Asked Answered
W

1

8

In scala 2.9.x I used tools.nsc.Global directly to compile certain string into a class and execute it.

In scala 2.10, it is possible to replace it with something like the following:

import scala.reflect.runtime._;
val cm = universe.runtimeMirror(getClass.getClassLoader)
import scala.tools.reflect.ToolBox;
val tb = cm.mkToolBox()
tb.eval(tb.parse("class C; new C"))

And it works flawlessly. The only problem is that with old (deprecated) approach, I could get extremely pretty summary of all compilation failures using StoreReporter (with error messages, line numbers), and new approach just throws an exception on compilation error.

Is there some way to reify that?

Wilbert answered 14/1, 2013 at 12:48 Comment(0)
A
2
scala> import scala.reflect.runtime._
import scala.reflect.runtime._

scala> val cm = universe.runtimeMirror(getClass.getClassLoader)
cm: reflect.runtime.universe.Mirror = JavaMirror with ...

scala> import scala.tools.reflect.ToolBox
import scala.tools.reflect.ToolBox

scala> val tb = cm.mkToolBox()
tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@712fe0c0

scala> tb.eval(tb.parse("class C; new D"))
scala.tools.reflect.ToolBoxError: reflective compilation has failed: 

not found: type D
  at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.throwIfErrors(ToolBoxFactory.scala:312)
  at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.compile(ToolBoxFactory.scala:248)
  at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.compile(ToolBoxFactory.scala:407)
  at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.eval(ToolBoxFactory.scala:410)
  ...

scala> tb.frontEnd.infos
res1: ... = Set(Info(NoPosition,not found: type D,ERROR))
Albania answered 14/1, 2013 at 22:26 Comment(5)
But, as you see, it gives NoPosition for all errors. And if I would be compiling big chunks of code with, I would want to be able to report at least line positions to the user.Wilbert
For the reference, here's how it is done currently: github.com/Rogach/miltamm/blob/master/src/main/scala/…Wilbert
Could you please open a ticket about this NoPosition thing?Albania
Oh sorry there's already a ticket for that: issues.scala-lang.org/browse/SI-6489.Albania
Thanks! I'll close this question, then.Wilbert

© 2022 - 2024 — McMap. All rights reserved.