From 3314d76cebc6e27ba4d4ca75cc64fe67fcb90fb6 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Tue, 18 Feb 2014 14:37:51 +0100 Subject: [nomaster] typecheck(q"class C") no longer crashes MemberDefs alone can't be typechecked as is, because namer only names contents of PackageDefs, Templates and Blocks. And, if not named, a tree can't be typed. This commit solves this problem by wrapping typecheckees in a trivial block and then unwrapping the result when it returns back from the typechecker. (cherry picked from commit 609047ba372ceaf06916d3361954bc949a6906ee) --- test/files/run/typecheck.check | 0 test/files/run/typecheck/Macros_1.scala | 17 +++++++++++++++++ test/files/run/typecheck/Test_2.scala | 15 +++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 test/files/run/typecheck.check create mode 100644 test/files/run/typecheck/Macros_1.scala create mode 100644 test/files/run/typecheck/Test_2.scala (limited to 'test') diff --git a/test/files/run/typecheck.check b/test/files/run/typecheck.check new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/files/run/typecheck/Macros_1.scala b/test/files/run/typecheck/Macros_1.scala new file mode 100644 index 0000000000..850a611ab1 --- /dev/null +++ b/test/files/run/typecheck/Macros_1.scala @@ -0,0 +1,17 @@ +import scala.reflect.macros.Context +import scala.language.experimental.macros + +object Macros { + def impl(c: Context) = { + import c.universe._ + val classDef = ClassDef( + Modifiers(), newTypeName("C"), List(), + Template( + List(Select(Ident(newTermName("scala")), newTypeName("AnyRef"))), emptyValDef, + List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(()))))))) + c.typeCheck(classDef) + c.Expr[Any](Literal(Constant(()))) + } + + def foo: Any = macro impl +} \ No newline at end of file diff --git a/test/files/run/typecheck/Test_2.scala b/test/files/run/typecheck/Test_2.scala new file mode 100644 index 0000000000..0a3279e23e --- /dev/null +++ b/test/files/run/typecheck/Test_2.scala @@ -0,0 +1,15 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect.ToolBox + +object Test extends App { + Macros.foo + + val tb = cm.mkToolBox() + val classDef = ClassDef( + Modifiers(), newTypeName("C"), List(), + Template( + List(Select(Ident(newTermName("scala")), newTypeName("AnyRef"))), emptyValDef, + List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(()))))))) + tb.typeCheck(classDef) +} \ No newline at end of file -- cgit v1.2.3