From 9393efc68a2c057a9b6e7d1c148355024d46a8aa Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Sun, 4 Dec 2011 20:27:06 +0100 Subject: Reification of classes now produces trees that can be compiled and run. Multiple minor fixes to Martin's implementation of reflection infrastructure. Dominating theme is allowing for the fact that compilation via reflection involves numerous exports/imports between various reflection universes. Fixes SI-5230. Review by @odersky. --- src/compiler/scala/reflect/internal/Importers.scala | 2 ++ src/compiler/scala/reflect/internal/Types.scala | 10 +++++++++- src/compiler/scala/tools/nsc/ast/Trees.scala | 4 ++++ src/compiler/scala/tools/nsc/transform/LiftCode.scala | 2 +- test/files/run/t5230.check | 2 ++ test/files/run/t5230.scala | 19 +++++++++++++++++++ test/pending/run/t5230.check | 1 - test/pending/run/t5230.scala | 19 ------------------- 8 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 test/files/run/t5230.check create mode 100644 test/files/run/t5230.scala delete mode 100644 test/pending/run/t5230.check delete mode 100644 test/pending/run/t5230.scala diff --git a/src/compiler/scala/reflect/internal/Importers.scala b/src/compiler/scala/reflect/internal/Importers.scala index 6d672d9263..60b353a7c4 100644 --- a/src/compiler/scala/reflect/internal/Importers.scala +++ b/src/compiler/scala/reflect/internal/Importers.scala @@ -231,6 +231,8 @@ trait Importers { self: SymbolTable => new PackageDef(importRefTree(pid), stats map importTree) case from.ModuleDef(mods, name, impl) => new ModuleDef(importModifiers(mods), importName(name).toTermName, importTemplate(impl)) + case from.emptyValDef => + emptyValDef case from.ValDef(mods, name, tpt, rhs) => new ValDef(importModifiers(mods), importName(name).toTermName, importTree(tpt), importTree(rhs)) case from.DefDef(mods, name, tparams, vparamss, tpt, rhs) => diff --git a/src/compiler/scala/reflect/internal/Types.scala b/src/compiler/scala/reflect/internal/Types.scala index 320fb949ff..265261f594 100644 --- a/src/compiler/scala/reflect/internal/Types.scala +++ b/src/compiler/scala/reflect/internal/Types.scala @@ -4174,8 +4174,16 @@ A type's typeSymbol should never be inspected directly. private def adaptToNewRun(pre: Type, sym: Symbol): Symbol = { if (phase.flatClasses) { sym + } else if (sym == definitions.RootClass) { + definitions.RootClass + } else if (sym == definitions.RootPackage) { + definitions.RootPackage } else if (sym.isModuleClass) { - adaptToNewRun(pre, sym.sourceModule).moduleClass + val sourceModule1 = adaptToNewRun(pre, sym.sourceModule) + val result = sourceModule1.moduleClass + val msg = "sym = %s, sourceModule = %s, sourceModule.moduleClass = %s => sourceModule1 = %s, sourceModule1.moduleClass = %s" + assert(result != NoSymbol, msg.format(sym, sym.sourceModule, sym.sourceModule.moduleClass, sourceModule1, sourceModule1.moduleClass)) + result } else if ((pre eq NoPrefix) || (pre eq NoType) || sym.isPackageClass) { sym } else { diff --git a/src/compiler/scala/tools/nsc/ast/Trees.scala b/src/compiler/scala/tools/nsc/ast/Trees.scala index 9668debbbb..85849cfad4 100644 --- a/src/compiler/scala/tools/nsc/ast/Trees.scala +++ b/src/compiler/scala/tools/nsc/ast/Trees.scala @@ -257,6 +257,10 @@ trait Trees extends reflect.internal.Trees { self: Global => case _: DefTree | Function(_, _) | Template(_, _, _) => resetDef(tree) tree.tpe = null + tree match { + case tree: DefDef => tree.tpt.tpe = null + case _ => () + } case tpt: TypeTree => if (tpt.wasEmpty) tree.tpe = null case This(_) if tree.symbol != null && tree.symbol.isPackageClass => diff --git a/src/compiler/scala/tools/nsc/transform/LiftCode.scala b/src/compiler/scala/tools/nsc/transform/LiftCode.scala index 7a64fc9b5e..68a53e57a1 100644 --- a/src/compiler/scala/tools/nsc/transform/LiftCode.scala +++ b/src/compiler/scala/tools/nsc/transform/LiftCode.scala @@ -475,7 +475,7 @@ abstract class LiftCode extends Transform with TypingTransformers { case tt: TypeTree if (tt.tpe != null) => if (!(boundSyms exists (tt.tpe contains _))) mirrorCall("TypeTree", reifyType(tt.tpe)) else if (tt.original != null) reify(tt.original) - else TypeTree() + else mirrorCall("TypeTree") case global.emptyValDef => mirrorSelect("emptyValDef") case _ => diff --git a/test/files/run/t5230.check b/test/files/run/t5230.check new file mode 100644 index 0000000000..5db6ec9b38 --- /dev/null +++ b/test/files/run/t5230.check @@ -0,0 +1,2 @@ +2 +evaluated = null diff --git a/test/files/run/t5230.scala b/test/files/run/t5230.scala new file mode 100644 index 0000000000..5aab8f9290 --- /dev/null +++ b/test/files/run/t5230.scala @@ -0,0 +1,19 @@ +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 { + val x = 2 + } + + println(new C().x) + }; + + val reporter = new ConsoleReporter(new Settings) + val toolbox = new ToolBox(reporter) + val ttree = toolbox.typeCheck(code.tree) + val evaluated = toolbox.runExpr(ttree) + println("evaluated = " + evaluated) +} diff --git a/test/pending/run/t5230.check b/test/pending/run/t5230.check deleted file mode 100644 index 5ef4ff4d04..0000000000 --- a/test/pending/run/t5230.check +++ /dev/null @@ -1 +0,0 @@ -evaluated = 2 diff --git a/test/pending/run/t5230.scala b/test/pending/run/t5230.scala deleted file mode 100644 index 5aab8f9290..0000000000 --- a/test/pending/run/t5230.scala +++ /dev/null @@ -1,19 +0,0 @@ -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 { - val x = 2 - } - - println(new C().x) - }; - - val reporter = new ConsoleReporter(new Settings) - val toolbox = new ToolBox(reporter) - val ttree = toolbox.typeCheck(code.tree) - val evaluated = toolbox.runExpr(ttree) - println("evaluated = " + evaluated) -} -- cgit v1.2.3