diff --git a/src/compiler/scala/tools/nsc/typechecker/Macros.scala b/src/compiler/scala/tools/nsc/typechecker/Macros.scala index afefc39b0ecd..5a67feee69ac 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Macros.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Macros.scala @@ -639,8 +639,8 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers { typecheck("blackbox typecheck #2", expanded1, outerPt) } else { val expanded1 = expanded0 - val expanded2 = typecheck("whitebox typecheck #1", expanded1, innerPt) - typecheck("whitebox typecheck #2", expanded2, outerPt) + val expanded2 = typecheck("whitebox typecheck #1", expanded1, outerPt) + typecheck("whitebox typecheck #2", expanded2, innerPt) } } override def onDelayed(delayed: Tree) = { diff --git a/test/files/run/t6992.check b/test/files/run/t6992.check new file mode 100644 index 000000000000..1a0684c9956e --- /dev/null +++ b/test/files/run/t6992.check @@ -0,0 +1,3 @@ +Int +42 +42 diff --git a/test/files/run/t6992/Macros_1.scala b/test/files/run/t6992/Macros_1.scala new file mode 100644 index 000000000000..25566dddbf89 --- /dev/null +++ b/test/files/run/t6992/Macros_1.scala @@ -0,0 +1,75 @@ +import scala.language.experimental.macros +import scala.reflect.macros.Context + +object Macros { + def foo(name: String): Any = macro foo_impl + def foo_impl(c: Context)(name: c.Expr[String]) = { + import c.universe._ + + val Literal(Constant(lit: String)) = name.tree + val anon = newTypeName(c.fresh) + + c.Expr(Block( + ClassDef( + Modifiers(Flag.FINAL), anon, Nil, Template( + Nil, noSelfType, List( + DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(pendingSuperCall), Literal(Constant(())))), + TypeDef(Modifiers(), TypeName(lit), Nil, TypeTree(typeOf[Int])) + ) + ) + ), + Apply(Select(New(Ident(anon)), nme.CONSTRUCTOR), Nil) + )) + } + + def bar(name: String): Any = macro bar_impl + def bar_impl(c: Context)(name: c.Expr[String]) = { + import c.universe._ + + val Literal(Constant(lit: String)) = name.tree + val anon = newTypeName(c.fresh) + + c.Expr(Block( + ClassDef( + Modifiers(Flag.FINAL), anon, Nil, Template( + Nil, noSelfType, List( + DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(pendingSuperCall), Literal(Constant(())))), + DefDef( + Modifiers(), TermName(lit), Nil, Nil, TypeTree(), + c.literal(42).tree + ) + ) + ) + ), + Apply(Select(New(Ident(anon)), nme.CONSTRUCTOR), Nil) + )) + } + + def baz(name: String): Any = macro baz_impl + def baz_impl(c: Context)(name: c.Expr[String]) = { + import c.universe._ + + val Literal(Constant(lit: String)) = name.tree + val anon = newTypeName(c.fresh) + val wrapper = newTypeName(c.fresh) + + c.Expr(Block( + ClassDef( + Modifiers(), anon, Nil, Template( + Nil, emptyValDef, List( + DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(pendingSuperCall), Literal(Constant(())))), + DefDef( + Modifiers(), TermName(lit), Nil, Nil, TypeTree(), + c.literal(42).tree + ) + ) + ) + ), + ClassDef( + Modifiers(Flag.FINAL), wrapper, Nil, + Template(Ident(anon) :: Nil, noSelfType, DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(pendingSuperCall), Literal(Constant(())))) :: Nil) + ), + Apply(Select(New(Ident(wrapper)), nme.CONSTRUCTOR), Nil) + )) + } +} \ No newline at end of file diff --git a/test/files/run/t6992/Test_2.scala b/test/files/run/t6992/Test_2.scala new file mode 100644 index 000000000000..05282d6f5b20 --- /dev/null +++ b/test/files/run/t6992/Test_2.scala @@ -0,0 +1,12 @@ +import scala.language.reflectiveCalls + +object Test extends App { + val foo = Macros.foo("T") + println(scala.reflect.runtime.universe.weakTypeOf[foo.T].typeSymbol.typeSignature) + + val bar = Macros.bar("test") + println(bar.test) + + val baz = Macros.baz("test") + println(baz.test) +} \ No newline at end of file diff --git a/test/files/run/t8048a.check b/test/files/run/t8048a.check new file mode 100644 index 000000000000..8fb9e26e8437 --- /dev/null +++ b/test/files/run/t8048a.check @@ -0,0 +1 @@ +Some(2) diff --git a/test/files/run/t8048a/Macros_1.scala b/test/files/run/t8048a/Macros_1.scala new file mode 100644 index 000000000000..f48e84f1ded0 --- /dev/null +++ b/test/files/run/t8048a/Macros_1.scala @@ -0,0 +1,11 @@ +import scala.reflect.macros.WhiteboxContext +import scala.language.experimental.macros + +object Macros { + def impl(c: WhiteboxContext) = { + import c.universe._ + q"if (true) Some(2) else None" + } + + def foo: Any = macro impl +} \ No newline at end of file diff --git a/test/files/run/t8048a/Test_2.scala b/test/files/run/t8048a/Test_2.scala new file mode 100644 index 000000000000..4e1c8b16b09d --- /dev/null +++ b/test/files/run/t8048a/Test_2.scala @@ -0,0 +1,4 @@ +object Test extends App { + val x: Option[Int] = Macros.foo + println(x) +} \ No newline at end of file diff --git a/test/files/run/t8048b.check b/test/files/run/t8048b.check new file mode 100644 index 000000000000..083edaac2489 --- /dev/null +++ b/test/files/run/t8048b.check @@ -0,0 +1,3 @@ +2 +2 +2 diff --git a/test/files/run/t8048b/Macros_1.scala b/test/files/run/t8048b/Macros_1.scala new file mode 100644 index 000000000000..b113af86eaf2 --- /dev/null +++ b/test/files/run/t8048b/Macros_1.scala @@ -0,0 +1,37 @@ +// see the following discussions to understand what's being tested here: +// * https://issues.scala-lang.org/browse/SI-6992 +// * https://issues.scala-lang.org/browse/SI-8048 +// * http://stackoverflow.com/questions/14370842/getting-a-structural-type-with-an-anonymous-classs-methods-from-a-macro +// * http://stackoverflow.com/questions/18480707/method-cannot-be-accessed-in-macro-generated-class/18485004#18485004 +// * https://groups.google.com/forum/#!topic/scala-internals/eXQt-BPm4i8 + +import scala.language.experimental.macros +import scala.reflect.macros.WhiteboxContext + +object Macros { + def impl1(c: WhiteboxContext) = { + import c.universe._ + q""" + trait Foo { def x = 2 } + new Foo {} + """ + } + def foo1: Any = macro impl1 + + def impl2(c: WhiteboxContext) = { + import c.universe._ + q""" + class Foo { def x = 2 } + new Foo + """ + } + def foo2: Any = macro impl2 + + def impl3(c: WhiteboxContext) = { + import c.universe._ + q""" + new { def x = 2 } + """ + } + def foo3: Any = macro impl3 +} \ No newline at end of file diff --git a/test/files/run/t8048b/Test_2.scala b/test/files/run/t8048b/Test_2.scala new file mode 100644 index 000000000000..fb410dab7ae5 --- /dev/null +++ b/test/files/run/t8048b/Test_2.scala @@ -0,0 +1,5 @@ +object Test extends App { + println(Macros.foo1.x) + println(Macros.foo2.x) + println(Macros.foo3.x) +} \ No newline at end of file