Scala Programming Language
  1. Scala Programming Language
  2. SI-5334

Cannot reify code that returns an object of a locally defined type

    Details

      Description

      import scala.tools.nsc.reporters._
      import scala.tools.nsc.Settings
      import reflect.runtime.Mirror.ToolBox
      
      object Test extends App {
        val code = scala.reflect.Code.lift{
          class C
          new C
        };
      
        val reporter = new ConsoleReporter(new Settings)
        val toolbox = new ToolBox(reporter)
        val ttree = toolbox.typeCheck(code.tree)
        toolbox.runExpr(ttree)
      }
      
      java.lang.ClassNotFoundException: Test.code $C
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$findClass(ScalaClassLoader.scala:159)
        at scala.tools.nsc.util.ScalaClassLoader$class.findClass(ScalaClassLoader.scala:57)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.findClass(ScalaClassLoader.scala:159)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$loadClass(ScalaClassLoader.scala:159)
        at scala.tools.nsc.util.ScalaClassLoader$class.loadClass(ScalaClassLoader.scala:63)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.loadClass(ScalaClassLoader.scala:159)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        at Test$delayedInit$body.apply(reify_anonymous.scala:6)
        at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
        at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
        at scala.App$$anonfun$main$1.apply(App.scala:61)
        at scala.App$$anonfun$main$1.apply(App.scala:61)
        at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
        at scala.collection.immutable.List.foreach(List.scala:76)
        at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:31)
        at scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:44)
        at scala.App$class.main(App.scala:61)
        at Test$.main(reify_anonymous.scala:5)
        at Test.main(reify_anonymous.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:90)
        at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:38)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:159)
        at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:90)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:159)
        at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:28)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:45)
        at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:35)
        at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:45)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:70)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:92)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:101)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
      

        Activity

        Hide
        Eugene Burmako added a comment -
        def codify(tree: Tree): Tree = debugTrace("codified " + tree + " -> ") {
          val targetType = definitions.CodeClass.primaryConstructor.info.paramTypes.head
          val reifier = new Reifier()
          val arg = gen.mkAsInstanceOf(reifier.reifyTopLevel(tree), targetType, wrapInApply = false)
          val treetpe = // this really should use packedType(tree.tpe, context.owner) 
                        // where packedType is defined in Typers. But we can do that only if liftCode is moved to Typers.
            if (tree.tpe.typeSymbol.isAnonymousClass) tree.tpe.typeSymbol.classBound
            else tree.tpe
          New(TypeTree(appliedType(definitions.CodeClass.typeConstructor, List(treetpe.widen))),
            List(List(arg)))
        }
        

        The comment says it all. Bugfix is postponed until liftcode becomes a macro (and, consequently, gets moved to Typers).

        Show
        Eugene Burmako added a comment - def codify(tree: Tree): Tree = debugTrace("codified " + tree + " -> ") { val targetType = definitions.CodeClass.primaryConstructor.info.paramTypes.head val reifier = new Reifier() val arg = gen.mkAsInstanceOf(reifier.reifyTopLevel(tree), targetType, wrapInApply = false) val treetpe = // this really should use packedType(tree.tpe, context.owner) // where packedType is defined in Typers. But we can do that only if liftCode is moved to Typers. if (tree.tpe.typeSymbol.isAnonymousClass) tree.tpe.typeSymbol.classBound else tree.tpe New(TypeTree(appliedType(definitions.CodeClass.typeConstructor, List(treetpe.widen))), List(List(arg))) } The comment says it all. Bugfix is postponed until liftcode becomes a macro (and, consequently, gets moved to Typers).
        Hide
        Eugene Burmako added a comment -

        Tests have been submitted as a pull request to scala/scala: https://github.com/scala/scala/pull/74

        Show
        Eugene Burmako added a comment - Tests have been submitted as a pull request to scala/scala: https://github.com/scala/scala/pull/74
        Show
        Eugene Burmako added a comment - Fixed by https://github.com/scala/scala/commit/75696bc3d310a53594d97853f38bb0dbfef42390

          People

          • Assignee:
            Eugene Burmako
            Reporter:
            Eugene Burmako
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development