From ce37ae45e22463a3f1a2d659d6699f2977b26c6b Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Wed, 2 Oct 2013 16:47:11 +0200 Subject: blackbox and whitebox macros This is the first commit in the series. This commit only: 1) Splits Context into BlackboxContext and WhiteboxContext 2) Splits Macro into BlackboxMacro and WhiteboxMacro 3) Introduces the isBundle property in the macro impl binding Here we just teach the compiler that macros can now be blackbox and whitebox, without actually imposing any restrictions on blackbox macros. These restrictions will come in subsequent commits. For description and documentation of the blackbox/whitebox separation see the official macro guide at the scaladoc website: http://docs.scala-lang.org/overviews/macros/blackbox-whitebox.html Some infrastructure work to make evolving macros easier: compile partest-extras with quick so they can use latest library/reflect/... --- test/files/pos/annotated-original/M_1.scala | 4 ++-- test/files/pos/annotated-treecopy/Impls_Macros_1.scala | 4 ++-- .../files/pos/attachments-typed-another-ident/Impls_1.scala | 4 ++-- test/files/pos/attachments-typed-ident/Impls_1.scala | 4 ++-- test/files/pos/t5692a/Macros_1.scala | 4 ++-- test/files/pos/t5692b/Macros_1.scala | 4 ++-- test/files/pos/t5706.scala | 13 +++++++++---- test/files/pos/t5744/Macros_1.scala | 6 +++--- test/files/pos/t6047.scala | 4 ++-- test/files/pos/t6447.scala | 8 ++++---- test/files/pos/t6485a/Macros_1.scala | 4 ++-- test/files/pos/t6485b/Test.scala | 4 ++-- test/files/pos/t6516.scala | 6 +++--- test/files/pos/t7377/Macro_1.scala | 4 ++-- test/files/pos/t7461/Macros_1.scala | 4 ++-- test/files/pos/t7649.scala | 2 +- 16 files changed, 42 insertions(+), 37 deletions(-) (limited to 'test/files/pos') diff --git a/test/files/pos/annotated-original/M_1.scala b/test/files/pos/annotated-original/M_1.scala index 01654e02cf..089a3a13c5 100644 --- a/test/files/pos/annotated-original/M_1.scala +++ b/test/files/pos/annotated-original/M_1.scala @@ -1,7 +1,7 @@ import language.experimental.macros -import reflect.macros.Context +import reflect.macros.BlackboxContext object M { - def impl(c: Context)(a: c.Expr[Any]) = c.Expr[Any](c.resetLocalAttrs(a.tree)) + def impl(c: BlackboxContext)(a: c.Expr[Any]) = c.Expr[Any](c.resetLocalAttrs(a.tree)) def m(a: Any) = macro impl } diff --git a/test/files/pos/annotated-treecopy/Impls_Macros_1.scala b/test/files/pos/annotated-treecopy/Impls_Macros_1.scala index 9b7af0c3b8..50c671707d 100644 --- a/test/files/pos/annotated-treecopy/Impls_Macros_1.scala +++ b/test/files/pos/annotated-treecopy/Impls_Macros_1.scala @@ -1,5 +1,5 @@ import scala.language.experimental.macros -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext import collection.mutable.ListBuffer import collection.mutable.Stack @@ -12,7 +12,7 @@ object Macros { def tree[T,U](f:Function1[T,U]): Function1[T,U] = macro tree_impl[T,U] - def tree_impl[T:c.WeakTypeTag,U:c.WeakTypeTag](c: Context) + def tree_impl[T:c.WeakTypeTag,U:c.WeakTypeTag](c: BlackboxContext) (f:c.Expr[Function1[T,U]]): c.Expr[Function1[T,U]] = { import c.universe._ val ttag = c.weakTypeTag[U] diff --git a/test/files/pos/attachments-typed-another-ident/Impls_1.scala b/test/files/pos/attachments-typed-another-ident/Impls_1.scala index c3f541075e..f84e56d714 100644 --- a/test/files/pos/attachments-typed-another-ident/Impls_1.scala +++ b/test/files/pos/attachments-typed-another-ident/Impls_1.scala @@ -1,10 +1,10 @@ -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext import language.experimental.macros object MyAttachment object Macros { - def impl(c: Context) = { + def impl(c: BlackboxContext) = { import c.universe._ val ident = Ident(TermName("bar")) updateAttachment MyAttachment assert(ident.attachments.get[MyAttachment.type].isDefined, ident.attachments) diff --git a/test/files/pos/attachments-typed-ident/Impls_1.scala b/test/files/pos/attachments-typed-ident/Impls_1.scala index c382cabc59..11d0f65844 100644 --- a/test/files/pos/attachments-typed-ident/Impls_1.scala +++ b/test/files/pos/attachments-typed-ident/Impls_1.scala @@ -1,10 +1,10 @@ -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext import language.experimental.macros object MyAttachment object Macros { - def impl(c: Context) = { + def impl(c: BlackboxContext) = { import c.universe._ val ident = Ident(TermName("bar")) updateAttachment MyAttachment assert(ident.attachments.get[MyAttachment.type].isDefined, ident.attachments) diff --git a/test/files/pos/t5692a/Macros_1.scala b/test/files/pos/t5692a/Macros_1.scala index e530713bb0..0e91f4d6a0 100644 --- a/test/files/pos/t5692a/Macros_1.scala +++ b/test/files/pos/t5692a/Macros_1.scala @@ -1,6 +1,6 @@ -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext object Macros { - def impl[T](c: Context) = { import c.universe._; c.Expr[Unit](q"()") } + def impl[T](c: BlackboxContext) = { import c.universe._; c.Expr[Unit](q"()") } def foo[T] = macro impl[T] } \ No newline at end of file diff --git a/test/files/pos/t5692b/Macros_1.scala b/test/files/pos/t5692b/Macros_1.scala index 45c672cfce..1034a1ea4a 100644 --- a/test/files/pos/t5692b/Macros_1.scala +++ b/test/files/pos/t5692b/Macros_1.scala @@ -1,6 +1,6 @@ -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext object Macros { - def impl[T, U](c: Context) = { import c.universe._; c.Expr[Unit](q"()") } + def impl[T, U](c: BlackboxContext) = { import c.universe._; c.Expr[Unit](q"()") } def foo[T, U] = macro impl[T, U] } \ No newline at end of file diff --git a/test/files/pos/t5706.scala b/test/files/pos/t5706.scala index 20a8b255cc..1970f5971f 100644 --- a/test/files/pos/t5706.scala +++ b/test/files/pos/t5706.scala @@ -1,10 +1,15 @@ -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext +import scala.reflect.macros.WhiteboxContext class Logger { - def error(message: String) = macro Impls.error + def error1(message: String) = macro Impls.error1 + def error2(message: String) = macro Impls.error2 } object Impls { - type LoggerContext = Context { type PrefixType = Logger } - def error(c: LoggerContext)(message: c.Expr[String]): c.Expr[Unit] = ??? + type LoggerContext1 = BlackboxContext { type PrefixType = Logger } + def error1(c: LoggerContext1)(message: c.Expr[String]): c.Expr[Unit] = ??? + + type LoggerContext2 = WhiteboxContext { type PrefixType = Logger } + def error2(c: LoggerContext2)(message: c.Expr[String]): c.Expr[Unit] = ??? } diff --git a/test/files/pos/t5744/Macros_1.scala b/test/files/pos/t5744/Macros_1.scala index 288a88653d..0fc13c12d7 100644 --- a/test/files/pos/t5744/Macros_1.scala +++ b/test/files/pos/t5744/Macros_1.scala @@ -1,18 +1,18 @@ import scala.language.experimental.macros -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext object Macros { def foo[U: Numeric](x: U) = macro foo_impl[U] def bar[U: Numeric : Equiv, Y <% String](x: U)(implicit s: String) = macro bar_impl[U, Y] - def foo_impl[U](c: Context)(x: c.Expr[U])(numeric: c.Expr[Numeric[U]]) = { + def foo_impl[U](c: BlackboxContext)(x: c.Expr[U])(numeric: c.Expr[Numeric[U]]) = { import c.universe._ val plusOne = Apply(Select(numeric.tree, newTermName("plus")), List(x.tree, Literal(Constant(1)))) val body = Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(plusOne)) c.Expr[Unit](body) } - def bar_impl[U, Y](c: Context)(x: c.Expr[U])(numeric: c.Expr[Numeric[U]], equiv: c.Expr[Equiv[U]], viewAsString: c.Expr[Y => String], s: c.Expr[String]) = { + def bar_impl[U, Y](c: BlackboxContext)(x: c.Expr[U])(numeric: c.Expr[Numeric[U]], equiv: c.Expr[Equiv[U]], viewAsString: c.Expr[Y => String], s: c.Expr[String]) = { import c.universe._ val plusOne = Apply(Select(numeric.tree, newTermName("plus")), List(x.tree, Literal(Constant(1)))) val plusLen = Apply(Select(numeric.tree, newTermName("plus")), List(plusOne, Select(s.tree, newTermName("length")))) diff --git a/test/files/pos/t6047.scala b/test/files/pos/t6047.scala index bc5f856bd2..c5bb44d87e 100644 --- a/test/files/pos/t6047.scala +++ b/test/files/pos/t6047.scala @@ -1,10 +1,10 @@ -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext import java.io.InputStream object Macros { def unpack[A](input: InputStream): A = macro unpack_impl[A] - def unpack_impl[A: c.WeakTypeTag](c: Context)(input: c.Expr[InputStream]): c.Expr[A] = { + def unpack_impl[A: c.WeakTypeTag](c: BlackboxContext)(input: c.Expr[InputStream]): c.Expr[A] = { import c.universe._ def unpackcode(tpe: c.Type): c.Expr[_] = { diff --git a/test/files/pos/t6447.scala b/test/files/pos/t6447.scala index 1c0c0f2a31..8203c0cddd 100644 --- a/test/files/pos/t6447.scala +++ b/test/files/pos/t6447.scala @@ -1,18 +1,18 @@ import scala.language.experimental.macros -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext class X { type T } object X { // this works def foo(x: X): x.T = macro fooImpl - def fooImpl(c: Context)(x: c.Expr[X]): c.Expr[x.value.T] = ??? + def fooImpl(c: BlackboxContext)(x: c.Expr[X]): c.Expr[x.value.T] = ??? // this doesn't def bar(x: X, y: X): (x.T, y.T) = macro barImpl - def barImpl(c: Context)(x: c.Expr[X], y: c.Expr[X]): c.Expr[(x.value.T, y.value.T)] = ??? + def barImpl(c: BlackboxContext)(x: c.Expr[X], y: c.Expr[X]): c.Expr[(x.value.T, y.value.T)] = ??? // neither does this def baz(x: X)(xs: List[x.T]): Unit = macro bazImpl - def bazImpl(c: Context)(x: c.Expr[X])(xs: c.Expr[List[x.value.T]]): c.Expr[Unit] = ??? + def bazImpl(c: BlackboxContext)(x: c.Expr[X])(xs: c.Expr[List[x.value.T]]): c.Expr[Unit] = ??? } diff --git a/test/files/pos/t6485a/Macros_1.scala b/test/files/pos/t6485a/Macros_1.scala index 85c2d5dbdb..c637c2cfee 100644 --- a/test/files/pos/t6485a/Macros_1.scala +++ b/test/files/pos/t6485a/Macros_1.scala @@ -1,5 +1,5 @@ -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext object Macros { - def crash(c: Context): c.Expr[Unit] = c.universe.reify(()) + def crash(c: BlackboxContext): c.Expr[Unit] = c.universe.reify(()) } \ No newline at end of file diff --git a/test/files/pos/t6485b/Test.scala b/test/files/pos/t6485b/Test.scala index 382df1c453..9897987516 100644 --- a/test/files/pos/t6485b/Test.scala +++ b/test/files/pos/t6485b/Test.scala @@ -1,10 +1,10 @@ import scala.language.experimental.macros -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext final class Ops[T](val x: T) extends AnyVal { def f = macro Macros.crash } object Macros { - def crash(c: Context): c.Expr[Unit] = c.universe.reify(()) + def crash(c: BlackboxContext): c.Expr[Unit] = c.universe.reify(()) } \ No newline at end of file diff --git a/test/files/pos/t6516.scala b/test/files/pos/t6516.scala index c004055de2..aed359976e 100644 --- a/test/files/pos/t6516.scala +++ b/test/files/pos/t6516.scala @@ -1,17 +1,17 @@ import scala.language.experimental.macros -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext import scala.collection.TraversableLike // This one compiles object Test { - type Alias[T, CC[_]] = Context { type PrefixType = TraversableLike[T, CC[T]] } + type Alias[T, CC[_]] = BlackboxContext { type PrefixType = TraversableLike[T, CC[T]] } def f() = macro f_impl def f_impl(c: Alias[Int, List])() = ??? } // This one doesn't object Test2 { - type Ctx = scala.reflect.macros.Context + type Ctx = scala.reflect.macros.BlackboxContext type Alias[T, CC[_]] = Ctx { type PrefixType = TraversableLike[T, CC[T]] } def f() = macro f_impl diff --git a/test/files/pos/t7377/Macro_1.scala b/test/files/pos/t7377/Macro_1.scala index a0ec1d84af..bb7ffb0f10 100644 --- a/test/files/pos/t7377/Macro_1.scala +++ b/test/files/pos/t7377/Macro_1.scala @@ -1,7 +1,7 @@ import language.experimental._ -import reflect.macros.Context +import reflect.macros.BlackboxContext object M { - def noopImpl[A](c: Context)(expr: c.Expr[A]): c.Expr[A] = c.Expr(c.typeCheck(c.resetLocalAttrs(expr.tree))) + def noopImpl[A](c: BlackboxContext)(expr: c.Expr[A]): c.Expr[A] = c.Expr(c.typeCheck(c.resetLocalAttrs(expr.tree))) def noop[A](expr: A): A = macro noopImpl[A] } diff --git a/test/files/pos/t7461/Macros_1.scala b/test/files/pos/t7461/Macros_1.scala index 8621650f77..126e9c067a 100644 --- a/test/files/pos/t7461/Macros_1.scala +++ b/test/files/pos/t7461/Macros_1.scala @@ -1,8 +1,8 @@ -import scala.reflect.macros.Context +import scala.reflect.macros.BlackboxContext import language.experimental.macros object Macros { - def impl(c: Context) = { + def impl(c: BlackboxContext) = { import c.universe._ val wut = c.typeCheck(Select(Literal(Constant(10)), newTermName("$minus")), silent = true) // println(showRaw(wut, printIds = true, printTypes = true)) diff --git a/test/files/pos/t7649.scala b/test/files/pos/t7649.scala index ff3c626fca..fa5d13369f 100644 --- a/test/files/pos/t7649.scala +++ b/test/files/pos/t7649.scala @@ -1,5 +1,5 @@ object Test { - val c: reflect.macros.Context = ??? + val c: reflect.macros.BlackboxContext = ??? import c.universe._ reify { // The lookup of the implicit WeakTypeTag[Any] -- cgit v1.2.3