1

I have found that scala compiler report compile error differently depends on whether it is used in compile-time or run-time.

E.g. for a simple implicit not found case, in compile time the report is:

newSource1.scala:6: error: type mismatch;
  L|R
  f(new L)
    ^

In runtime, when the same code was evaluated and the error message was captured directly:


  class TestCase(code: String, extra: String) {


  def toolbox(extra: String): ToolBox[universe.type] = {
    ToolBox(cm).mkToolBox(options = s"$opts $extra")
  }

    def compile(): Any = {
      import SpecHelpers._

      val tb = toolbox(extra)
      tb.eval(tb.parse(code))
    }

    def compileError(): String =
      Try(compile()) match {
        case Failure(ee) =>
          ee match {
            case te: ToolBoxError =>
              te.message.linesIterator.toList.drop(2).mkString("\n")
            case t =>
              throw t
          }
        case Success(_) =>
          sys.error("compiling succeeded")
      }
  }

the line number and location cursor is omitted:;

implicit error;
!I e: F[Arg]

Is there a way to add the missing part back?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
tribbloid
  • 4,026
  • 14
  • 64
  • 103

1 Answers1

4

You can specify frontEnd for ToolBox (default frontEnd is mkSilentFrontEnd())

val tb = cm.mkToolBox(
  frontEnd = new FrontEnd {
    override def display(info: Info): Unit = println(info)
  },
  options = "-Xlog-implicits -Vimplicits -Vimplicits-verbose-tree -d out" 
  // with `-d out` toolbox keeps `.class` files in directory `out` 
  // rather than in memory, so you can decompile `.class` files 
  // if you want to investigate auto-generated code
)

tb.compile(tb.parse(
  """implicit val i: Int = 1
    |implicitly[String]""".stripMargin))

//Info(source-<toolbox>,line-2,offset=34,implicit error;
//!I e: String,ERROR)
//Exception in thread "main" java.lang.ExceptionInInitializerError
//  at App.main(App.scala)
//Caused by: scala.tools.reflect.ToolBoxError: reflective compilation has failed:
//
//implicit error;
//!I e: String
//  at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.throwIfErrors(ToolBoxFactory.scala:332)
//  at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.wrapInPackageAndCompile(ToolBoxFactory.scala:214)
//  at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.compile(ToolBoxFactory.scala:268)
//  at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.$anonfun$compile$13(ToolBoxFactory.scala:445)
//  at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.apply(ToolBoxFactory.scala:371)
//  at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.compile(ToolBoxFactory.scala:438)
//  at App$.<clinit>(App.scala:20)
//  ... 1 more
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66