Open
Description
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox
//case class Foo() // Works if Foo is defined here instead
object HelloWorld {
case class Foo()
def main(args: Array[String]) {
val tb = runtimeMirror(getClass.getClassLoader).mkToolBox()
tb.compile(tb.parse("()")) // It works, if we comment this out
//tb.typecheck(reify{()}.tree) // It works, if we uncomment this
val foo = Foo()
val exp = reify {foo: Foo}
tb.typecheck(exp.tree)
}
}
The typecheck in the last line fails with
Exception in thread "main" scala.tools.reflect.ToolBoxError: reflective typecheck has failed: type mismatch;
found : HelloWorld.(some other)Foo(in object HelloWorld)
required: HelloWorld.Foo(in object HelloWorld)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.$anonfun$typecheck$1(ToolBoxFactory.scala:182)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.$anonfun$transformDuringTyper$4(ToolBoxFactory.scala:150)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.$anonfun$transformDuringTyper$3(ToolBoxFactory.scala:150)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.$anonfun$transformDuringTyper$1(ToolBoxFactory.scala:149)
at scala.reflect.internal.Trees.wrappingIntoTerm(Trees.scala:1710)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.withWrapping$1(ToolBoxFactory.scala:119)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.transformDuringTyper(ToolBoxFactory.scala:120)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.typecheck(ToolBoxFactory.scala:174)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.$anonfun$typecheck$6(ToolBoxFactory.scala:379)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.apply(ToolBoxFactory.scala:359)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.typecheck(ToolBoxFactory.scala:371)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.typecheck(ToolBoxFactory.scala:26)
at HelloWorld$.main(HelloWorld.scala:18)
at HelloWorld.main(HelloWorld.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
A workaround is to call tb.typecheck
on a very simple tree after every call to tb.compile
.
Note that this is a similar bug to #8637, but it is different.