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

Reify generates invalid trees when creating a class and referencing its field

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.0
    • Fix Version/s: Scala 2.10.0
    • Component/s: Reflection
    • Labels:
      None

      Description

      import scala.tools.nsc.reporters._
      import scala.tools.nsc.Settings
      import reflect.runtime.Mirror.ToolBox
      
      object Foo extends App {
        val code = scala.reflect.Code.lift{
          class C {
            val x = 2
          }
      
          println(new C().x)
        };
      
        val reporter = new ConsoleReporter(new Settings)
        val toolbox = new ToolBox(reporter)
        val ttree = toolbox.typeCheck(code.tree)
        println("result = " + toolbox.showAttributed(ttree))
      
        val evaluated = toolbox.runExpr(ttree)
        println("evaluated = " + evaluated)
      }
      
      D:\Dropbox\Scratchpad\Scala>scala Foo
      scala.reflect.internal.Types$TypeError: value x is not a member of C
              at scala.tools.nsc.typechecker.Contexts$Context.error(Contexts.scala:283)
              at scala.tools.nsc.typechecker.TypeDiagnostics$class.notAMemberError(TypeDiagnostics.scala:135)
              at scala.tools.nsc.Global$$anon$1.notAMemberError(Global.scala:405)
              at scala.tools.nsc.typechecker.Typers$Typer.typedSelect$1(Typers.scala:3669)
              at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:4217)
              at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4311)
              at scala.tools.nsc.typechecker.Typers$Typer.typedArg(Typers.scala:2162)
              at scala.tools.nsc.typechecker.Typers$Typer.loop$1(Typers.scala:2181)
              at scala.tools.nsc.typechecker.Typers$Typer.typedArgs(Typers.scala:2186)
              at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:2415)
              at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:3431)
              at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:4156)
              at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4311)
              at scala.tools.nsc.typechecker.Typers$Typer.typedBlock(Typers.scala:1875)
              at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:4022)
              at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4311)
              at scala.reflect.runtime.ToolBoxes$ToolBox.importAndTypeCheck(ToolBoxes.scala:132)
              at scala.reflect.runtime.ToolBoxes$ToolBox.typeCheck(ToolBoxes.scala:138)
              at scala.reflect.runtime.ToolBoxes$ToolBox.typeCheck(ToolBoxes.scala:143)
              at Foo$delayedInit$body.apply(Foo.scala:19)
              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:77)
              at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:31)
              at scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:42)
              at scala.App$class.main(App.scala:61)
              at Foo$.main(Foo.scala:8)
              at Foo.main(Foo.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 -

        Tree produced by the codifier is as follows:

        val $local0: Symbol = selectTerm(staticModule("Foo").moduleClass, "code ").newNestedSymbol(NoPosition, newTypeName("C"));
        val $local1: Symbol = $local0.newNestedSymbol(NoPosition, newTermName("<init>")).setInternalFlags(flagsOfModifiers(List()));
        val $local2: Symbol = $local0.newNestedSymbol(NoPosition, newTermName("x")).setInternalFlags(flagsOfModifiers(List()));
        val $local3: Symbol = $local0.newNestedSymbol(NoPosition, newTermName("x ")).setInternalFlags(flagsOfModifiers(List(`private`, local)));
        $local0.setTypeSig(ClassInfoType(List(staticClass("java.lang.Object").asTypeConstructor, staticClass("scala.ScalaObject").asTypeConstructor), newScopeWith($local1, $local2, $local3), $local0));
        $local1.setTypeSig(MethodType(List(), TypeRef(NoPrefix, $local0, List())));
        $local2.setTypeSig(NullaryMethodType(staticClass("scala.Int").asTypeConstructor));
        $local3.setTypeSig(staticClass("scala.Int").asTypeConstructor);
        
        Block(
          List(
            ClassDef(
              Modifiers(), 
              newTypeName("C"), 
              List(), 
              Template(
                List(TypeTree(staticClass("java.lang.Object").asTypeConstructor), TypeTree(staticClass("scala.ScalaObject").asTypeConstructor)), 
                ValDef(Modifiers(Set(`private`), newTypeName(""), List()), newTermName("_"), TypeTree(NoType), EmptyTree),
                List(
                  DefDef(Modifiers(), newTermName("<init>"), List(), List(List()), TypeTree(TypeRef(NoPrefix, $local0, List())), Block(List(Apply(Select(Super(This(newTypeName("")), newTypeName("")), newTermName("<init>")), List())), Literal(Constant(())))), 
                  ValDef(Modifiers(Set(`private`, local), newTypeName(""), List()), newTermName("x "), TypeTree(staticClass("scala.Int").asTypeConstructor), Literal(Constant(2))), 
                  DefDef(Modifiers(Set(), newTypeName(""), List()), newTermName("x"), List(), List(), TypeTree(staticClass("scala.Int").asTypeConstructor), Select(This(newTypeName("C")), newTermName("x "))))))), 
        
          Apply(
            Select(Select(Ident(staticModule("scala").moduleClass), newTermName("Predef")), newTermName("println")), 
            List(
              Select(
                Apply(Select(New(Ident(newTypeName("C"))), newTermName("<init>")), List()), 
                newTermName("x")))))
        
        Show
        Eugene Burmako added a comment - Tree produced by the codifier is as follows: val $local0: Symbol = selectTerm(staticModule("Foo").moduleClass, "code ").newNestedSymbol(NoPosition, newTypeName("C")); val $local1: Symbol = $local0.newNestedSymbol(NoPosition, newTermName("<init>")).setInternalFlags(flagsOfModifiers(List())); val $local2: Symbol = $local0.newNestedSymbol(NoPosition, newTermName("x")).setInternalFlags(flagsOfModifiers(List())); val $local3: Symbol = $local0.newNestedSymbol(NoPosition, newTermName("x ")).setInternalFlags(flagsOfModifiers(List(`private`, local))); $local0.setTypeSig(ClassInfoType(List(staticClass("java.lang.Object").asTypeConstructor, staticClass("scala.ScalaObject").asTypeConstructor), newScopeWith($local1, $local2, $local3), $local0)); $local1.setTypeSig(MethodType(List(), TypeRef(NoPrefix, $local0, List()))); $local2.setTypeSig(NullaryMethodType(staticClass("scala.Int").asTypeConstructor)); $local3.setTypeSig(staticClass("scala.Int").asTypeConstructor); Block( List( ClassDef( Modifiers(), newTypeName("C"), List(), Template( List(TypeTree(staticClass("java.lang.Object").asTypeConstructor), TypeTree(staticClass("scala.ScalaObject").asTypeConstructor)), ValDef(Modifiers(Set(`private`), newTypeName(""), List()), newTermName("_"), TypeTree(NoType), EmptyTree), List( DefDef(Modifiers(), newTermName("<init>"), List(), List(List()), TypeTree(TypeRef(NoPrefix, $local0, List())), Block(List(Apply(Select(Super(This(newTypeName("")), newTypeName("")), newTermName("<init>")), List())), Literal(Constant(())))), ValDef(Modifiers(Set(`private`, local), newTypeName(""), List()), newTermName("x "), TypeTree(staticClass("scala.Int").asTypeConstructor), Literal(Constant(2))), DefDef(Modifiers(Set(), newTypeName(""), List()), newTermName("x"), List(), List(), TypeTree(staticClass("scala.Int").asTypeConstructor), Select(This(newTypeName("C")), newTermName("x "))))))), Apply( Select(Select(Ident(staticModule("scala").moduleClass), newTermName("Predef")), newTermName("println")), List( Select( Apply(Select(New(Ident(newTypeName("C"))), newTermName("<init>")), List()), newTermName("x")))))
        Hide
        Commit Message Bot added a comment -

        (odersky in r26085) Attempt to fix #5230. Review by burmako.

        Show
        Commit Message Bot added a comment - (odersky in r26085 ) Attempt to fix #5230. Review by burmako.
        Hide
        Eugene Burmako added a comment -

        The fix does not work

        Show
        Eugene Burmako added a comment - The fix does not work
        Hide
        Commit Message Bot added a comment -

        (burmako in r26086) Test for SI-5230

        Show
        Commit Message Bot added a comment - (burmako in r26086 ) Test for SI-5230
        Hide
        Commit Message Bot added a comment -

        (burmako in r26088) Moved the test for SI-5230 from files to pending

        Show
        Commit Message Bot added a comment - (burmako in r26088 ) Moved the test for SI-5230 from files to pending
        Show
        Eugene Burmako added a comment - Fixed by https://github.com/scala/scala/commit/9393efc68a2c057a9b6e7d1c148355024d46a8aa

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development