From 4a273659e0e25ccfe7ea9d4eafa4a9c87ee2fc82 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Sat, 10 Aug 2013 15:16:09 +0200 Subject: kills introduceTopLevel As we've figured out from the practice, introduceTopLevel is seductively useful but unfortunately not robust, potentially bringing compilation order problems. Therefore, as discussed, I'm removing it from the public macro API. Alternatives are either: 1) delving into internals, or 2) using macro paradise and experimenting with macro annotations: http://docs.scala-lang.org/overviews/macros/annotations.html. --- test/files/run/macro-expand-unapply-b.check | 2 - test/files/run/macro-expand-unapply-b.flags | 1 - .../macro-expand-unapply-b/Impls_Macros_1.scala | 37 ---------------- test/files/run/macro-expand-unapply-b/Test_2.scala | 8 ---- test/files/run/macro-toplevel-companion-a.check | 0 test/files/run/macro-toplevel-companion-a.flags | 1 - .../Impls_Macros_1.scala | 14 ------ .../run/macro-toplevel-companion-a/Test_2.scala | 8 ---- test/files/run/macro-toplevel-companion-b.check | 4 -- test/files/run/macro-toplevel-companion-b.flags | 1 - .../Impls_Macros_1.scala | 15 ------- .../run/macro-toplevel-companion-b/Test_2.scala | 11 ----- test/files/run/macro-toplevel-companion-c.check | 3 -- test/files/run/macro-toplevel-companion-c.flags | 1 - test/files/run/macro-toplevel-companion-c.scala | 51 ---------------------- test/files/run/macro-toplevel.check | 2 - test/files/run/macro-toplevel/Macros_1.scala | 15 ------- test/files/run/macro-toplevel/Test_2.scala | 6 --- 18 files changed, 180 deletions(-) delete mode 100644 test/files/run/macro-expand-unapply-b.check delete mode 100644 test/files/run/macro-expand-unapply-b.flags delete mode 100644 test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala delete mode 100644 test/files/run/macro-expand-unapply-b/Test_2.scala delete mode 100644 test/files/run/macro-toplevel-companion-a.check delete mode 100644 test/files/run/macro-toplevel-companion-a.flags delete mode 100644 test/files/run/macro-toplevel-companion-a/Impls_Macros_1.scala delete mode 100644 test/files/run/macro-toplevel-companion-a/Test_2.scala delete mode 100644 test/files/run/macro-toplevel-companion-b.check delete mode 100644 test/files/run/macro-toplevel-companion-b.flags delete mode 100644 test/files/run/macro-toplevel-companion-b/Impls_Macros_1.scala delete mode 100644 test/files/run/macro-toplevel-companion-b/Test_2.scala delete mode 100644 test/files/run/macro-toplevel-companion-c.check delete mode 100644 test/files/run/macro-toplevel-companion-c.flags delete mode 100644 test/files/run/macro-toplevel-companion-c.scala delete mode 100644 test/files/run/macro-toplevel.check delete mode 100644 test/files/run/macro-toplevel/Macros_1.scala delete mode 100644 test/files/run/macro-toplevel/Test_2.scala (limited to 'test/files/run') diff --git a/test/files/run/macro-expand-unapply-b.check b/test/files/run/macro-expand-unapply-b.check deleted file mode 100644 index 5272f0d00a..0000000000 --- a/test/files/run/macro-expand-unapply-b.check +++ /dev/null @@ -1,2 +0,0 @@ -(1,List(2)) -List(1) diff --git a/test/files/run/macro-expand-unapply-b.flags b/test/files/run/macro-expand-unapply-b.flags deleted file mode 100644 index cd66464f2f..0000000000 --- a/test/files/run/macro-expand-unapply-b.flags +++ /dev/null @@ -1 +0,0 @@ --language:experimental.macros \ No newline at end of file diff --git a/test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala b/test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala deleted file mode 100644 index d0300bdf7e..0000000000 --- a/test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala +++ /dev/null @@ -1,37 +0,0 @@ -import language.experimental.macros -import scala.reflect.macros.Context - -object Macros { - implicit class ContextExtensions(c: StringContext) { - object q { - def unapply(x: Any): Option[Any] = macro impl - } - } - - def impl(c: Context)(x: c.Expr[Any]): c.Expr[Option[Any]] = { - import c.universe._ - import Flag._ - - // parts here will be string literals - static parts of the string interpolation - // e.g. for q"$x, $y" parts will be Literal(Constant("")), Literal(Constant(", ")) and Literal(Constant("")) - val Apply(Select(Select(Apply(_, List(Apply(_, parts))), _), _), _) = c.macroApplication - val nresults = parts.length - 1 - - def results() = - ((1 to (nresults - 1)).toList map (i => Literal(Constant(i)))) :+ // (n - 1) results of type Int - Apply(Ident(TermName("List")), List(Literal(Constant(nresults)))) // and also one result of a different type - def extractorBody() = - if (nresults == 0) Literal(Constant(true)) - else if (nresults == 1) Apply(Ident(TermName("Some")), results()) - else Apply(Ident(TermName("Some")), List(Apply(Ident(TermName("Tuple" + nresults)), results()))) - - val name = TermName(java.util.UUID.randomUUID().toString.replace("-", "")) - val mdef = ModuleDef(NoMods, name, Template(List(Select(Ident(TermName("scala")), TypeName("AnyRef"))), emptyValDef, List( - DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), - Block(List(pendingSuperCall), Literal(Constant(())))), - DefDef(Modifiers(), TermName("unapply"), List(), List(List(ValDef(Modifiers(PARAM), TermName("x"), Ident(TypeName("Any")), EmptyTree))), TypeTree(), - extractorBody())))) - c.introduceTopLevel(nme.EMPTY_PACKAGE_NAME.toString, mdef) - c.Expr[Option[Any]](Apply(Select(Ident(name), TermName("unapply")), List(x.tree))) - } -} \ No newline at end of file diff --git a/test/files/run/macro-expand-unapply-b/Test_2.scala b/test/files/run/macro-expand-unapply-b/Test_2.scala deleted file mode 100644 index 5352160dfe..0000000000 --- a/test/files/run/macro-expand-unapply-b/Test_2.scala +++ /dev/null @@ -1,8 +0,0 @@ -object Test extends App { - import Macros._ - def whatever() = null - val q"$x1, $y1" = whatever() - println(x1, y1) - val q"$x2" = whatever() - println(x2) -} diff --git a/test/files/run/macro-toplevel-companion-a.check b/test/files/run/macro-toplevel-companion-a.check deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/test/files/run/macro-toplevel-companion-a.flags b/test/files/run/macro-toplevel-companion-a.flags deleted file mode 100644 index cd66464f2f..0000000000 --- a/test/files/run/macro-toplevel-companion-a.flags +++ /dev/null @@ -1 +0,0 @@ --language:experimental.macros \ No newline at end of file diff --git a/test/files/run/macro-toplevel-companion-a/Impls_Macros_1.scala b/test/files/run/macro-toplevel-companion-a/Impls_Macros_1.scala deleted file mode 100644 index 23e8694ddc..0000000000 --- a/test/files/run/macro-toplevel-companion-a/Impls_Macros_1.scala +++ /dev/null @@ -1,14 +0,0 @@ -import scala.reflect.macros.Context -import language.experimental.macros - -object Macros { - def impl(c: Context) = { - import c.universe._ - val synthetic = reify{ class C { override def toString = "C" }; object C { implicit val c = new C } }.tree - val defs = synthetic.asInstanceOf[Block].stats.asInstanceOf[List[ImplDef]] - if (c.topLevelRef(TypeName("C")).isEmpty) c.introduceTopLevel(nme.EMPTY_PACKAGE_NAME.toString, defs: _*) - c.literalUnit - } - - def foo = macro impl -} \ No newline at end of file diff --git a/test/files/run/macro-toplevel-companion-a/Test_2.scala b/test/files/run/macro-toplevel-companion-a/Test_2.scala deleted file mode 100644 index 78b65b5b1f..0000000000 --- a/test/files/run/macro-toplevel-companion-a/Test_2.scala +++ /dev/null @@ -1,8 +0,0 @@ -import Macros._ - -object Test extends App { - foo; - implicitly[C]; - foo; - implicitly[C]; -} \ No newline at end of file diff --git a/test/files/run/macro-toplevel-companion-b.check b/test/files/run/macro-toplevel-companion-b.check deleted file mode 100644 index bd30dc75d3..0000000000 --- a/test/files/run/macro-toplevel-companion-b.check +++ /dev/null @@ -1,4 +0,0 @@ -reflective compilation has failed: - -Companions 'class C' and 'object C' must be defined in same file: - Found in and diff --git a/test/files/run/macro-toplevel-companion-b.flags b/test/files/run/macro-toplevel-companion-b.flags deleted file mode 100644 index cd66464f2f..0000000000 --- a/test/files/run/macro-toplevel-companion-b.flags +++ /dev/null @@ -1 +0,0 @@ --language:experimental.macros \ No newline at end of file diff --git a/test/files/run/macro-toplevel-companion-b/Impls_Macros_1.scala b/test/files/run/macro-toplevel-companion-b/Impls_Macros_1.scala deleted file mode 100644 index f30adc2965..0000000000 --- a/test/files/run/macro-toplevel-companion-b/Impls_Macros_1.scala +++ /dev/null @@ -1,15 +0,0 @@ -import scala.reflect.macros.Context -import language.experimental.macros - -object Macros { - def impl(c: Context) = { - import c.universe._ - val Block(List(cdef: ClassDef), _) = reify{ class C }.tree - val classRef = c.topLevelRef(TypeName("C")) orElse c.introduceTopLevel(nme.EMPTY_PACKAGE_NAME.toString, cdef) - val Block(List(mdef: ModuleDef), _) = reify{ object C }.tree - val moduleRef = c.topLevelRef(TermName("C")) orElse c.introduceTopLevel(nme.EMPTY_PACKAGE_NAME.toString, mdef) - c.literalUnit - } - - def foo = macro impl -} \ No newline at end of file diff --git a/test/files/run/macro-toplevel-companion-b/Test_2.scala b/test/files/run/macro-toplevel-companion-b/Test_2.scala deleted file mode 100644 index 4e766bde89..0000000000 --- a/test/files/run/macro-toplevel-companion-b/Test_2.scala +++ /dev/null @@ -1,11 +0,0 @@ -import scala.reflect.runtime.universe._ -import scala.reflect.runtime.{universe => ru} -import scala.reflect.runtime.{currentMirror => cm} -import scala.tools.reflect.{ToolBox, ToolBoxError} -import Macros._ - -object Test extends App { - val tb = cm.mkToolBox() - try tb.compile(Select(Ident(TermName("Macros")), TermName("foo"))) - catch { case ToolBoxError(message, _) => println("""(Found in|and) .*?compileLateSynthetic-.*?\.scala""".r.replaceAllIn(message, m => m.group(1) + " ")) } -} \ No newline at end of file diff --git a/test/files/run/macro-toplevel-companion-c.check b/test/files/run/macro-toplevel-companion-c.check deleted file mode 100644 index 4052c472f8..0000000000 --- a/test/files/run/macro-toplevel-companion-c.check +++ /dev/null @@ -1,3 +0,0 @@ -error: Companions 'class C' and 'object C' must be defined in same file: - Found in and newSource1.scala - diff --git a/test/files/run/macro-toplevel-companion-c.flags b/test/files/run/macro-toplevel-companion-c.flags deleted file mode 100644 index cd66464f2f..0000000000 --- a/test/files/run/macro-toplevel-companion-c.flags +++ /dev/null @@ -1 +0,0 @@ --language:experimental.macros \ No newline at end of file diff --git a/test/files/run/macro-toplevel-companion-c.scala b/test/files/run/macro-toplevel-companion-c.scala deleted file mode 100644 index c315f8b942..0000000000 --- a/test/files/run/macro-toplevel-companion-c.scala +++ /dev/null @@ -1,51 +0,0 @@ -import scala.tools.partest._ -import java.io._ - -object Test extends DirectTest { - def code = ??? - - def macros_1 = """ - package test - - import scala.reflect.macros.Context - import language.experimental.macros - - object Macros { - def impl(c: Context) = { - import c.universe._ - val Block(List(cdef: ClassDef), _) = reify{ class C }.tree - val ref = c.topLevelRef(TypeName("test.C")) orElse c.introduceTopLevel("test", cdef) - c.literalUnit - } - - def foo = macro impl - } - """ - def compileMacros() = { - val classpath = List(sys.props("partest.lib"), sys.props("partest.reflect")) mkString sys.props("path.separator") - compileString(newCompiler("-language:experimental.macros", "-cp", classpath, "-d", testOutput.path))(macros_1) - } - - def test_2 = """ - package test - object C { Macros.foo } - """ - def compileTest() = { - val classpath = List(sys.props("partest.lib"), testOutput.path) mkString sys.props("path.separator") - compileString(newCompiler("-cp", classpath, "-d", testOutput.path))(test_2) - } - - def show(): Unit = { - // redirect err to string, for logging - val prevErr = System.err - val baos = new ByteArrayOutputStream() - System.setErr(new PrintStream(baos)) - log("Compiling Macros_1...") - if (compileMacros()) { - log("Compiling Test_2...") - if (compileTest()) log("Success!") else log("Failed...") - } - println("""(Found in|and) .*?compileLateSynthetic-.*?\.scala""".r.replaceAllIn(baos.toString, m => m.group(1) + " ")) - System.setErr(prevErr) - } -} \ No newline at end of file diff --git a/test/files/run/macro-toplevel.check b/test/files/run/macro-toplevel.check deleted file mode 100644 index 257c3764fd..0000000000 --- a/test/files/run/macro-toplevel.check +++ /dev/null @@ -1,2 +0,0 @@ -I've been created from Macros.foo -I've been created from Macros.foo diff --git a/test/files/run/macro-toplevel/Macros_1.scala b/test/files/run/macro-toplevel/Macros_1.scala deleted file mode 100644 index f681c86735..0000000000 --- a/test/files/run/macro-toplevel/Macros_1.scala +++ /dev/null @@ -1,15 +0,0 @@ -import scala.reflect.macros.Context -import language.experimental.macros - -object Macros { - def impl(c: Context) = { - import c.universe._ - val msg = "I've been created from " + c.macroApplication - val Block(List(synthetic: ClassDef), _) = reify{ class SomeUniqueName { def hello = c.literal(msg).splice } }.tree - val ref = c.topLevelRef(synthetic.name) orElse c.introduceTopLevel(nme.EMPTY_PACKAGE_NAME.toString, synthetic) - c.Expr[String](Select(Apply(Select(New(ref), nme.CONSTRUCTOR), List()), TermName("hello"))) - } - - def foo = macro impl - def foo2 = macro impl -} diff --git a/test/files/run/macro-toplevel/Test_2.scala b/test/files/run/macro-toplevel/Test_2.scala deleted file mode 100644 index eee2d6ae13..0000000000 --- a/test/files/run/macro-toplevel/Test_2.scala +++ /dev/null @@ -1,6 +0,0 @@ -import Macros._ - -object Test extends App { - println(Macros.foo) - println(Macros.foo2) -} \ No newline at end of file -- cgit v1.2.3