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

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

Closed
scabug opened this issue Nov 26, 2011 · 7 comments
Closed
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Nov 26, 2011

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)
@scabug
Copy link
Author

scabug commented Nov 26, 2011

Imported From: https://issues.scala-lang.org/browse/SI-5230?orig=1
Reporter: @xeno-by
Affected Versions: 2.10.0

@scabug
Copy link
Author

scabug commented Nov 28, 2011

@xeno-by said:
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")))))

@scabug
Copy link
Author

scabug commented Nov 29, 2011

Commit Message Bot (anonymous) said:
(odersky in r26085) Attempt to fix #5230. Review by burmako.

@scabug
Copy link
Author

scabug commented Nov 29, 2011

@xeno-by said:
The fix does not work

@scabug
Copy link
Author

scabug commented Nov 29, 2011

Commit Message Bot (anonymous) said:
(burmako in r26086) Test for #5230

@scabug
Copy link
Author

scabug commented Nov 29, 2011

Commit Message Bot (anonymous) said:
(burmako in r26088) Moved the test for #5230 from files to pending

@scabug
Copy link
Author

scabug commented Dec 6, 2011

@xeno-by said:
Fixed by scala/scala@9393efc

@scabug scabug closed this as completed Dec 6, 2011
@scabug scabug added this to the 2.10.0 milestone Apr 7, 2017
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