Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toolbox.compile can corrupt the internal state of the toolbox #9932

Open
scabug opened this issue Sep 21, 2016 · 1 comment
Open

Toolbox.compile can corrupt the internal state of the toolbox #9932

scabug opened this issue Sep 21, 2016 · 1 comment

Comments

@scabug
Copy link

scabug commented Sep 21, 2016

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.

@scabug
Copy link
Author

scabug commented Sep 21, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9932?orig=1
Reporter: Gabor Gevay (ggevay)
Affected Versions: 2.11.8, 2.12.0-RC1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants