From 79dfed0baabd14394ba078b62c7418ae0e738603 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Wed, 25 Apr 2012 19:37:26 +0300 Subject: removes Expr <-> Tree implicits --- src/library/scala/reflect/api/Exprs.scala | 34 +--------------------- src/library/scala/reflect/api/Trees.scala | 28 ------------------ src/library/scala/reflect/api/Universe.scala | 3 +- src/library/scala/reflect/makro/Context.scala | 4 +-- .../Impls_1.scala | 3 +- .../Impls_1.scala | 2 +- .../neg/macro-without-xmacros-a/Impls_1.scala | 6 ++-- .../neg/macro-without-xmacros-b/Impls_1.scala | 6 ++-- test/files/run/macro-abort-fresh/Test_2.scala | 2 +- test/files/run/macro-basic-mamd-mi/Impls_1.scala | 4 +-- .../run/macro-def-infer-return-type-b/Test_2.scala | 2 +- .../macro-expand-tparams-bounds-a/Impls_1.scala | 5 +--- .../macro-expand-tparams-bounds-b/Impls_1.scala | 5 +--- .../Impls_1.scala | 5 +--- .../macro-expand-tparams-prefix-c1/Impls_1.scala | 4 +-- .../Impls_Macros_1.scala | 4 +-- .../macro-expand-tparams-prefix-d1/Impls_1.scala | 4 +-- .../Macros_Test_2.scala | 2 +- .../Impls_Macros_1.scala | 2 +- .../Test_2.scala | 2 +- .../run/macro-invalidret-nontypeable/Test_2.scala | 2 +- .../run/macro-invalidusage-badret/Test_2.scala | 2 +- .../Test_2.scala | 2 +- .../run/macro-range/Expansion_Impossible_2.scala | 10 +++---- .../macro-reflective-ma-normal-mdmi/Test_2.scala | 2 +- .../macro-reify-eval-outside-reify/Test_2.scala | 2 +- test/files/run/macro-reify-freevars/Test_2.scala | 2 +- test/files/run/macro-reify-tagless-a/Test_2.scala | 2 +- .../macro-reify-value-outside-reify/Test_2.scala | 2 +- test/files/run/reify_newimpl_45.scala | 6 ++-- test/files/run/t5704.scala | 32 ++++++++++---------- .../run/toolbox_default_reporter_is_silent.scala | 2 +- test/files/run/toolbox_silent_reporter.scala | 2 +- 33 files changed, 62 insertions(+), 133 deletions(-) diff --git a/src/library/scala/reflect/api/Exprs.scala b/src/library/scala/reflect/api/Exprs.scala index ff89a1cd48..b8db64422e 100644 --- a/src/library/scala/reflect/api/Exprs.scala +++ b/src/library/scala/reflect/api/Exprs.scala @@ -16,36 +16,4 @@ trait Exprs { self: Universe => lazy val value: T = eval override def toString = "Expr["+tpe+"]("+tree+")" } - - // [Eugene] had to move this to the companion of Tree to make stuff compile. weirdo! -// object Expr { -// // would be great if in future this generated an Expr[Magic] -// // where Magic is a magic untyped type that propagates through the entire quasiquote -// // and turns off typechecking whenever it's involved -// // that'd allow us to splice trees into quasiquotes and still have these qqs to be partially typechecked -// // see some exploration of these ideas here: https://github.com/xeno-by/alphakeplerdemo -// implicit def tree2expr(tree: Tree): Expr[Nothing] = Expr[Nothing](tree) -// implicit def expr2tree(expr: Expr[_]): Tree = expr.tree -// -// // [Eugene] good idea? -// implicit def trees2exprs(trees: List[Tree]): List[Expr[Nothing]] = trees map tree2expr -// implicit def exprs2trees(exprs: List[Expr[_]]): List[Tree] = exprs map expr2tree -// } - - // [Eugene] even weirder - implicits didn't feel at home in Trees :( - - // would be great if in future this generated an Expr[Magic] - // where Magic is a magic untyped type that propagates through the entire quasiquote - // and turns off typechecking whenever it's involved - // that'd allow us to splice trees into quasiquotes and still have these qqs to be partially typechecked - // see some exploration of these ideas here: https://github.com/xeno-by/alphakeplerdemo - // [Martin] I have some doubts whether it's god to have implicit conversions. - implicit def tree2expr(tree: Tree): Expr[Nothing] = Expr[Nothing](tree)(TypeTag.Nothing) - implicit def expr2tree(expr: Expr[_]): Tree = expr.tree - - // [Eugene] good idea? - // [Martin] probably not. - implicit def trees2exprs(trees: List[Tree]): List[Expr[Nothing]] = trees map tree2expr - implicit def exprs2trees(exprs: List[Expr[_]]): List[Tree] = exprs map expr2tree -} - +} \ No newline at end of file diff --git a/src/library/scala/reflect/api/Trees.scala b/src/library/scala/reflect/api/Trees.scala index b70ca7e0fd..9f8906a8cd 100644 --- a/src/library/scala/reflect/api/Trees.scala +++ b/src/library/scala/reflect/api/Trees.scala @@ -278,34 +278,6 @@ trait Trees { self: Universe => override def equals(that: Any) = this eq that.asInstanceOf[AnyRef] } - // [Eugene] uh-oh - // locker.comp: - // [mkdir] Created dir: C:\Projects\Kepler\build\locker\classes\compiler - // [scalacfork] Compiling 471 files to C:\Projects\Kepler\build\locker\classes\compiler - // [scalacfork] amb prefix: Importers.this.type#class Tree Importer.this.from.type#class Tree - // [scalacfork] amb prefix: Importers.this.type#class Tree Importer.this.from.type#class Tree - // [scalacfork] amb prefix: Importers.this.type#class Tree Importer.this.from.type#class Tree - // [scalacfork] amb prefix: Importers.this.type#class Tree Importer.this.from.type#class Tree - // [scalacfork] amb prefix: Importers.this.type#class Tree Importer.this.from.type#class Tree - // [scalacfork] amb prefix: Importers.this.type#class Tree Importer.this.from.type#class Tree - // [scalacfork] amb prefix: Importers.this.type#class Tree Importer.this.from.type#class Tree - // [scalacfork] amb prefix: Importers.this.type#class Tree Importer.this.from.type#class Tree - // [scalacfork] amb prefix: Importers.this.type#class Tree Importer.this.from.type#class Tree - // [scalacfork] amb prefix: Importers.this.type#class Tree Importer.this.from.type#class Tree -// object Tree { -// // would be great if in future this generated an Expr[Magic] -// // where Magic is a magic untyped type that propagates through the entire quasiquote -// // and turns off typechecking whenever it's involved -// // that'd allow us to splice trees into quasiquotes and still have these qqs to be partially typechecked -// // see some exploration of these ideas here: https://github.com/xeno-by/alphakeplerdemo -// implicit def tree2expr(tree: Tree): Expr[Nothing] = Expr[Nothing](tree) -// implicit def expr2tree(expr: Expr[_]): Tree = expr.tree -// -// // [Eugene] good idea? -// implicit def trees2exprs(trees: List[Tree]): List[Expr[Nothing]] = trees map tree2expr -// implicit def exprs2trees(exprs: List[Expr[_]]): List[Tree] = exprs map expr2tree -// } - /** A tree for a term. Not all terms are TermTrees; use isTerm * to reliably identify terms. */ diff --git a/src/library/scala/reflect/api/Universe.scala b/src/library/scala/reflect/api/Universe.scala index 2a7445c41c..9670780522 100755 --- a/src/library/scala/reflect/api/Universe.scala +++ b/src/library/scala/reflect/api/Universe.scala @@ -65,8 +65,7 @@ abstract class Universe extends Symbols object Universe { def reify[T](cc: scala.reflect.makro.Context{ type PrefixType = Universe })(expr: cc.Expr[T]): cc.Expr[cc.prefix.value.Expr[T]] = { - import cc.mirror._ import scala.reflect.makro.internal._ - cc.materializeExpr(cc.prefix, expr) + cc.Expr(cc.materializeExpr(cc.prefix.tree, expr.tree)) } } diff --git a/src/library/scala/reflect/makro/Context.scala b/src/library/scala/reflect/makro/Context.scala index 668d239087..b8fb0dcce5 100644 --- a/src/library/scala/reflect/makro/Context.scala +++ b/src/library/scala/reflect/makro/Context.scala @@ -36,9 +36,9 @@ object Context { import cc.mirror._ import scala.reflect.makro.internal._ // [Eugene] how do I typecheck this without undergoing this tiresome (and, in general, incorrect) procedure? - val prefix: Tree = Select(cc.prefix, newTermName("mirror")) + val prefix: Tree = Select(cc.prefix.tree, newTermName("mirror")) val prefixTpe = cc.typeCheck(TypeApply(Select(prefix, newTermName("asInstanceOf")), List(SingletonTypeTree(prefix)))).tpe prefix setType prefixTpe - cc.materializeExpr(prefix, expr) + cc.Expr(cc.materializeExpr(prefix, expr.tree)) } } diff --git a/test/files/neg/macro-invalidsig-tparams-notparams-b/Impls_1.scala b/test/files/neg/macro-invalidsig-tparams-notparams-b/Impls_1.scala index 7bc46ff876..db79ddbcd8 100644 --- a/test/files/neg/macro-invalidsig-tparams-notparams-b/Impls_1.scala +++ b/test/files/neg/macro-invalidsig-tparams-notparams-b/Impls_1.scala @@ -2,10 +2,9 @@ import scala.reflect.makro.{Context => Ctx} object Impls { def foo[T: c.TypeTag, U: c.TypeTag, V](c: Ctx)(implicit V: c.TypeTag[V]): c.Expr[Unit] = { - import c.mirror._ println(implicitly[c.TypeTag[T]]) println(implicitly[c.TypeTag[U]]) println(V) - Literal(Constant(())) + c.literalUnit } } diff --git a/test/files/neg/macro-invalidsig-tparams-notparams-c/Impls_1.scala b/test/files/neg/macro-invalidsig-tparams-notparams-c/Impls_1.scala index 7bc46ff876..43c5a2acd5 100644 --- a/test/files/neg/macro-invalidsig-tparams-notparams-c/Impls_1.scala +++ b/test/files/neg/macro-invalidsig-tparams-notparams-c/Impls_1.scala @@ -6,6 +6,6 @@ object Impls { println(implicitly[c.TypeTag[T]]) println(implicitly[c.TypeTag[U]]) println(V) - Literal(Constant(())) + c.literalUnit } } diff --git a/test/files/neg/macro-without-xmacros-a/Impls_1.scala b/test/files/neg/macro-without-xmacros-a/Impls_1.scala index 01daf12b1a..e6cee46e85 100644 --- a/test/files/neg/macro-without-xmacros-a/Impls_1.scala +++ b/test/files/neg/macro-without-xmacros-a/Impls_1.scala @@ -3,16 +3,16 @@ import scala.reflect.makro.{Context => Ctx} object Impls { def foo_impl(c: Ctx)(x: c.Expr[Int]): c.Expr[Int] = { import c.mirror._ - Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(1)))) + Expr(Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(1))))) } def bar_impl(c: Ctx)(x: c.Expr[Int]): c.Expr[Int] = { import c.mirror._ - Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(2)))) + Expr(Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(2))))) } def quux_impl(c: Ctx)(x: c.Expr[Int]): c.Expr[Int] = { import c.mirror._ - Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(3)))) + Expr(Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(3))))) } } diff --git a/test/files/neg/macro-without-xmacros-b/Impls_1.scala b/test/files/neg/macro-without-xmacros-b/Impls_1.scala index 01daf12b1a..e6cee46e85 100644 --- a/test/files/neg/macro-without-xmacros-b/Impls_1.scala +++ b/test/files/neg/macro-without-xmacros-b/Impls_1.scala @@ -3,16 +3,16 @@ import scala.reflect.makro.{Context => Ctx} object Impls { def foo_impl(c: Ctx)(x: c.Expr[Int]): c.Expr[Int] = { import c.mirror._ - Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(1)))) + Expr(Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(1))))) } def bar_impl(c: Ctx)(x: c.Expr[Int]): c.Expr[Int] = { import c.mirror._ - Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(2)))) + Expr(Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(2))))) } def quux_impl(c: Ctx)(x: c.Expr[Int]): c.Expr[Int] = { import c.mirror._ - Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(3)))) + Expr(Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(3))))) } } diff --git a/test/files/run/macro-abort-fresh/Test_2.scala b/test/files/run/macro-abort-fresh/Test_2.scala index f389231ca6..ecf48b9752 100644 --- a/test/files/run/macro-abort-fresh/Test_2.scala +++ b/test/files/run/macro-abort-fresh/Test_2.scala @@ -1,6 +1,6 @@ object Test extends App { import scala.reflect.mirror._ val tree = Select(Ident("Macros"), newTermName("foo")) - try tree.eval + try Expr(tree).eval catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-basic-mamd-mi/Impls_1.scala b/test/files/run/macro-basic-mamd-mi/Impls_1.scala index eadb63fa8e..82d2cce2f5 100644 --- a/test/files/run/macro-basic-mamd-mi/Impls_1.scala +++ b/test/files/run/macro-basic-mamd-mi/Impls_1.scala @@ -3,12 +3,12 @@ import scala.reflect.makro.{Context => Ctx} object Impls { def foo(c: Ctx)(x: c.Expr[Int]): c.Expr[Int] = { import c.mirror._ - Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(1)))) + Expr(Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(1))))) } def bar(c: Ctx)(x: c.Expr[Int]): c.Expr[Int] = { import c.mirror._ - Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(2)))) + Expr(Apply(Select(x.tree, newTermName("$plus")), List(Literal(Constant(2))))) } def quux(c: Ctx)(x: c.Expr[Int]): c.Expr[Int] = { diff --git a/test/files/run/macro-def-infer-return-type-b/Test_2.scala b/test/files/run/macro-def-infer-return-type-b/Test_2.scala index 8ff4494750..f8f15b4224 100644 --- a/test/files/run/macro-def-infer-return-type-b/Test_2.scala +++ b/test/files/run/macro-def-infer-return-type-b/Test_2.scala @@ -1,6 +1,6 @@ object Test extends App { import scala.reflect.mirror._ val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(42)))) - try tree.eval + try Expr(tree).eval catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-expand-tparams-bounds-a/Impls_1.scala b/test/files/run/macro-expand-tparams-bounds-a/Impls_1.scala index 4cd98c5838..5019d5d4a7 100644 --- a/test/files/run/macro-expand-tparams-bounds-a/Impls_1.scala +++ b/test/files/run/macro-expand-tparams-bounds-a/Impls_1.scala @@ -1,8 +1,5 @@ import scala.reflect.makro.{Context => Ctx} object Impls { - def foo[U <: String](c: Ctx): c.Expr[Unit] = { - import c.mirror._ - Literal(Constant(())) - } + def foo[U <: String](c: Ctx): c.Expr[Unit] = c.literalUnit } diff --git a/test/files/run/macro-expand-tparams-bounds-b/Impls_1.scala b/test/files/run/macro-expand-tparams-bounds-b/Impls_1.scala index 9103ddb08a..abd68696b6 100644 --- a/test/files/run/macro-expand-tparams-bounds-b/Impls_1.scala +++ b/test/files/run/macro-expand-tparams-bounds-b/Impls_1.scala @@ -3,8 +3,5 @@ import scala.reflect.makro.{Context => Ctx} class C object Impls { - def foo[U <: C](c: Ctx): c.Expr[Unit] = { - import c.mirror._ - Literal(Constant(())) - } + def foo[U <: C](c: Ctx): c.Expr[Unit] = c.literalUnit } diff --git a/test/files/run/macro-expand-tparams-only-in-impl/Impls_1.scala b/test/files/run/macro-expand-tparams-only-in-impl/Impls_1.scala index 4cd98c5838..5019d5d4a7 100644 --- a/test/files/run/macro-expand-tparams-only-in-impl/Impls_1.scala +++ b/test/files/run/macro-expand-tparams-only-in-impl/Impls_1.scala @@ -1,8 +1,5 @@ import scala.reflect.makro.{Context => Ctx} object Impls { - def foo[U <: String](c: Ctx): c.Expr[Unit] = { - import c.mirror._ - Literal(Constant(())) - } + def foo[U <: String](c: Ctx): c.Expr[Unit] = c.literalUnit } diff --git a/test/files/run/macro-expand-tparams-prefix-c1/Impls_1.scala b/test/files/run/macro-expand-tparams-prefix-c1/Impls_1.scala index bc880fdf77..4da5a9b823 100644 --- a/test/files/run/macro-expand-tparams-prefix-c1/Impls_1.scala +++ b/test/files/run/macro-expand-tparams-prefix-c1/Impls_1.scala @@ -3,10 +3,10 @@ import scala.reflect.makro.{Context => Ctx} object Impls { def foo[T, U: c.TypeTag, V](c: Ctx)(implicit T: c.TypeTag[T], V: c.TypeTag[V]): c.Expr[Unit] = { import c.mirror._ - Block(List( + Expr(Block(List( Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant(T.toString)))), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant(implicitly[c.TypeTag[U]].toString)))), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant(V.toString))))), - Literal(Constant(()))) + Literal(Constant(())))) } } diff --git a/test/files/run/macro-expand-tparams-prefix-c2/Impls_Macros_1.scala b/test/files/run/macro-expand-tparams-prefix-c2/Impls_Macros_1.scala index c83e401afb..4d137a4aef 100644 --- a/test/files/run/macro-expand-tparams-prefix-c2/Impls_Macros_1.scala +++ b/test/files/run/macro-expand-tparams-prefix-c2/Impls_Macros_1.scala @@ -3,11 +3,11 @@ import scala.reflect.makro.{Context => Ctx} object Impls { def foo[T, U: c.TypeTag, V](c: Ctx)(implicit T: c.TypeTag[T], V: c.TypeTag[V]): c.Expr[Unit] = { import c.mirror._ - Block(List( + Expr(Block(List( Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant(T.toString)))), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant(implicitly[c.TypeTag[U]].toString)))), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant(V.toString))))), - Literal(Constant(()))) + Literal(Constant(())))) } } diff --git a/test/files/run/macro-expand-tparams-prefix-d1/Impls_1.scala b/test/files/run/macro-expand-tparams-prefix-d1/Impls_1.scala index bc880fdf77..4da5a9b823 100644 --- a/test/files/run/macro-expand-tparams-prefix-d1/Impls_1.scala +++ b/test/files/run/macro-expand-tparams-prefix-d1/Impls_1.scala @@ -3,10 +3,10 @@ import scala.reflect.makro.{Context => Ctx} object Impls { def foo[T, U: c.TypeTag, V](c: Ctx)(implicit T: c.TypeTag[T], V: c.TypeTag[V]): c.Expr[Unit] = { import c.mirror._ - Block(List( + Expr(Block(List( Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant(T.toString)))), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant(implicitly[c.TypeTag[U]].toString)))), Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Literal(Constant(V.toString))))), - Literal(Constant(()))) + Literal(Constant(())))) } } diff --git a/test/files/run/macro-expand-varargs-explicit-over-nonvarargs-bad/Macros_Test_2.scala b/test/files/run/macro-expand-varargs-explicit-over-nonvarargs-bad/Macros_Test_2.scala index 523c6b0645..01fc9d4462 100644 --- a/test/files/run/macro-expand-varargs-explicit-over-nonvarargs-bad/Macros_Test_2.scala +++ b/test/files/run/macro-expand-varargs-explicit-over-nonvarargs-bad/Macros_Test_2.scala @@ -5,6 +5,6 @@ object Macros { object Test extends App { import scala.reflect.mirror._ val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Typed(Apply(Ident(definitions.ListModule), List(Literal(Constant(1)), Literal(Constant(2)))), Ident(tpnme.WILDCARD_STAR)))) - try tree.eval + try Expr(tree).eval catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Impls_Macros_1.scala b/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Impls_Macros_1.scala index 882867fcab..828ccf34e2 100644 --- a/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Impls_Macros_1.scala +++ b/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Impls_Macros_1.scala @@ -3,7 +3,7 @@ import scala.reflect.makro.{Context => Ctx} object Impls { def foo(c: Ctx): c.Expr[Int] = { import c.mirror._ - Literal(Constant("42")) + Expr(Literal(Constant("42"))) } } diff --git a/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Test_2.scala b/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Test_2.scala index f389231ca6..ecf48b9752 100644 --- a/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Test_2.scala +++ b/test/files/run/macro-invalidret-doesnt-conform-to-def-rettype/Test_2.scala @@ -1,6 +1,6 @@ object Test extends App { import scala.reflect.mirror._ val tree = Select(Ident("Macros"), newTermName("foo")) - try tree.eval + try Expr(tree).eval catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-invalidret-nontypeable/Test_2.scala b/test/files/run/macro-invalidret-nontypeable/Test_2.scala index f389231ca6..ecf48b9752 100644 --- a/test/files/run/macro-invalidret-nontypeable/Test_2.scala +++ b/test/files/run/macro-invalidret-nontypeable/Test_2.scala @@ -1,6 +1,6 @@ object Test extends App { import scala.reflect.mirror._ val tree = Select(Ident("Macros"), newTermName("foo")) - try tree.eval + try Expr(tree).eval catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-invalidusage-badret/Test_2.scala b/test/files/run/macro-invalidusage-badret/Test_2.scala index e171c9d05a..aeb3c26eaa 100644 --- a/test/files/run/macro-invalidusage-badret/Test_2.scala +++ b/test/files/run/macro-invalidusage-badret/Test_2.scala @@ -1,6 +1,6 @@ object Test extends App { import scala.reflect.mirror._ val tree = Typed(Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(42)))), Ident(newTypeName("String"))) - try tree.eval + try Expr(tree).eval catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-invalidusage-partialapplication/Test_2.scala b/test/files/run/macro-invalidusage-partialapplication/Test_2.scala index 63371a4a82..293d906048 100644 --- a/test/files/run/macro-invalidusage-partialapplication/Test_2.scala +++ b/test/files/run/macro-invalidusage-partialapplication/Test_2.scala @@ -1,6 +1,6 @@ object Test extends App { import scala.reflect.mirror._ val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(40)))) - try tree.eval + try Expr(tree).eval catch { case ex: Throwable => println(ex.getMessage) } } diff --git a/test/files/run/macro-range/Expansion_Impossible_2.scala b/test/files/run/macro-range/Expansion_Impossible_2.scala index 7a093b74ee..051afff99c 100644 --- a/test/files/run/macro-range/Expansion_Impossible_2.scala +++ b/test/files/run/macro-range/Expansion_Impossible_2.scala @@ -14,7 +14,7 @@ object Impls { // scala"{ var i = $low; val h = $hi; while (i < h) { $f(i); i = i + 1 } } // or: // scala"($_this: RangeDefault).foreach($f)" - c.prefix.tree match { + Expr(c.prefix.tree match { case Apply(Select(New(tpt), initName), List(lo, hi)) if tpt.symbol.fullName == "Range" => val iname = newTermName("$i") val hname = newTermName("$h") @@ -23,7 +23,7 @@ object Impls { val labelname = newTermName("$while") val cond = makeBinop(iref, "$less", href) val body = Block( - List(makeApply(f, List(iref))), + List(makeApply(f.tree, List(iref))), Assign(iref, makeBinop(iref, "$plus", Literal(Constant(1))))) val generated = Block( @@ -37,10 +37,10 @@ object Impls { case _ => Apply( Select( - Typed(c.prefix, Ident(newTypeName("RangeDefault"))), + Typed(c.prefix.tree, Ident(newTypeName("RangeDefault"))), newTermName("foreach")), - List(f)) - } + List(f.tree)) + }) } } diff --git a/test/files/run/macro-reflective-ma-normal-mdmi/Test_2.scala b/test/files/run/macro-reflective-ma-normal-mdmi/Test_2.scala index 3c594aed75..632708a125 100644 --- a/test/files/run/macro-reflective-ma-normal-mdmi/Test_2.scala +++ b/test/files/run/macro-reflective-ma-normal-mdmi/Test_2.scala @@ -1,5 +1,5 @@ object Test extends App { import scala.reflect.mirror._ val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(42)))) - println(tree.eval) + println(Expr(tree).eval) } diff --git a/test/files/run/macro-reify-eval-outside-reify/Test_2.scala b/test/files/run/macro-reify-eval-outside-reify/Test_2.scala index 3c594aed75..632708a125 100644 --- a/test/files/run/macro-reify-eval-outside-reify/Test_2.scala +++ b/test/files/run/macro-reify-eval-outside-reify/Test_2.scala @@ -1,5 +1,5 @@ object Test extends App { import scala.reflect.mirror._ val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(42)))) - println(tree.eval) + println(Expr(tree).eval) } diff --git a/test/files/run/macro-reify-freevars/Test_2.scala b/test/files/run/macro-reify-freevars/Test_2.scala index 55c677155a..e7077f9c7f 100644 --- a/test/files/run/macro-reify-freevars/Test_2.scala +++ b/test/files/run/macro-reify-freevars/Test_2.scala @@ -4,6 +4,6 @@ object Test extends App { val x = ValDef(NoMods, newTermName("x"), Ident("Int"), EmptyTree) val fn = Function(List(x), Apply(Select(Ident(newTermName("x")), newTermName("$plus")), List(Literal(Constant("5"))))) val tree = Apply(Select(q, newTermName("map")), List(fn)) - try tree.eval + try Expr(tree).eval catch { case ex: Throwable => println(ex.getMessage) } } \ No newline at end of file diff --git a/test/files/run/macro-reify-tagless-a/Test_2.scala b/test/files/run/macro-reify-tagless-a/Test_2.scala index d996da1570..94efbd0c37 100644 --- a/test/files/run/macro-reify-tagless-a/Test_2.scala +++ b/test/files/run/macro-reify-tagless-a/Test_2.scala @@ -7,6 +7,6 @@ object Test extends App { val rhs = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant("hello world")))) val list = ValDef(NoMods, newTermName("list"), tpt, rhs) val tree = Block(list, Apply(Select(Ident(definitions.PredefModule), newTermName("println")), List(Ident(list.name)))) - try tree.eval + try Expr(tree).eval catch { case ex: Throwable => println(ex.getMessage) } } diff --git a/test/files/run/macro-reify-value-outside-reify/Test_2.scala b/test/files/run/macro-reify-value-outside-reify/Test_2.scala index 8225eb0b39..128cad4510 100644 --- a/test/files/run/macro-reify-value-outside-reify/Test_2.scala +++ b/test/files/run/macro-reify-value-outside-reify/Test_2.scala @@ -1,6 +1,6 @@ object Test extends App { import scala.reflect.mirror._ val tree = Apply(Select(Ident("Macros"), newTermName("foo")), List(Literal(Constant(42)))) - try println(tree.eval) + try println(Expr(tree).eval) catch { case ex: Throwable => println(ex.getMessage) } } diff --git a/test/files/run/reify_newimpl_45.scala b/test/files/run/reify_newimpl_45.scala index 241b7d4bc3..b2b52eaf5b 100644 --- a/test/files/run/reify_newimpl_45.scala +++ b/test/files/run/reify_newimpl_45.scala @@ -3,9 +3,9 @@ import scala.reflect.mirror._ object Test extends App { class C[T >: Null] { val code = reify{val x: T = "2".asInstanceOf[T]; println("ima worx: %s".format(x)); x} - println(freeTypes(code)) - val T = freeTypes(code)(0) - mkToolBox().runExpr(code, Map(T -> definitions.StringClass.asType)) + println(freeTypes(code.tree)) + val T = freeTypes(code.tree)(0) + mkToolBox().runExpr(code.tree, Map(T -> definitions.StringClass.asType)) } new C[String] diff --git a/test/files/run/t5704.scala b/test/files/run/t5704.scala index 25a6575dc3..8fd721d4e7 100644 --- a/test/files/run/t5704.scala +++ b/test/files/run/t5704.scala @@ -1,16 +1,16 @@ -import scala.reflect.mirror._ - -object Test extends App { - class MyQuerycollection{ - def findUserByName( name:String ) = { - val tree = reify{ "test" == name } - val toolbox = mkToolBox() - toolbox.typeCheck(tree) match{ - case Apply(Select(lhs,op),rhs::Nil) => - println(rhs.tpe) - } - } - } - val qc = new MyQuerycollection - qc.findUserByName("some value") -} +import scala.reflect.mirror._ + +object Test extends App { + class MyQuerycollection{ + def findUserByName( name:String ) = { + val tree = reify{ "test" == name }.tree + val toolbox = mkToolBox() + toolbox.typeCheck(tree) match{ + case Apply(Select(lhs,op),rhs::Nil) => + println(rhs.tpe) + } + } + } + val qc = new MyQuerycollection + qc.findUserByName("some value") +} diff --git a/test/files/run/toolbox_default_reporter_is_silent.scala b/test/files/run/toolbox_default_reporter_is_silent.scala index 78606e2abc..6908c001a3 100644 --- a/test/files/run/toolbox_default_reporter_is_silent.scala +++ b/test/files/run/toolbox_default_reporter_is_silent.scala @@ -9,5 +9,5 @@ object Test extends App { } Utils.foo - }) + }.tree) } diff --git a/test/files/run/toolbox_silent_reporter.scala b/test/files/run/toolbox_silent_reporter.scala index 5f264f7d74..6f5687ba4f 100644 --- a/test/files/run/toolbox_silent_reporter.scala +++ b/test/files/run/toolbox_silent_reporter.scala @@ -9,7 +9,7 @@ object Test extends App { } Utils.foo - }) + }.tree) println("============compiler messages============") toolbox.frontEnd.infos.foreach(println(_)) println("=========================================") -- cgit v1.2.3 From bd3eacbae21f39b1ac7fe8ade4ed71fa98e1a28d Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Wed, 25 Apr 2012 20:28:35 +0300 Subject: fixes SI-5693 --- .../scala/reflect/internal/StdAttachments.scala | 2 - .../scala/tools/nsc/typechecker/Analyzer.scala | 1 + .../scala/tools/nsc/typechecker/Macros.scala | 161 ++++++++++----------- .../tools/nsc/typechecker/StdAttachments.scala | 10 ++ 4 files changed, 85 insertions(+), 89 deletions(-) create mode 100644 src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala diff --git a/src/compiler/scala/reflect/internal/StdAttachments.scala b/src/compiler/scala/reflect/internal/StdAttachments.scala index 488195b7dd..ae2ad87deb 100644 --- a/src/compiler/scala/reflect/internal/StdAttachments.scala +++ b/src/compiler/scala/reflect/internal/StdAttachments.scala @@ -7,6 +7,4 @@ trait StdAttachments { self: SymbolTable => case class ReifyAttachment(original: Symbol) - - case class MacroAttachment(delayed: Boolean, context: Option[MacroContext]) } \ No newline at end of file diff --git a/src/compiler/scala/tools/nsc/typechecker/Analyzer.scala b/src/compiler/scala/tools/nsc/typechecker/Analyzer.scala index 18c7635b1e..a77df71312 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Analyzer.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Analyzer.scala @@ -24,6 +24,7 @@ trait Analyzer extends AnyRef with NamesDefaults with TypeDiagnostics with ContextErrors + with StdAttachments { val global : Global import global._ diff --git a/src/compiler/scala/tools/nsc/typechecker/Macros.scala b/src/compiler/scala/tools/nsc/typechecker/Macros.scala index ef9bae700e..6d4c543a5a 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Macros.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Macros.scala @@ -294,8 +294,6 @@ trait Macros { self: Analyzer => } finally { openMacros = openMacros.tail } - case Delay(result) => - result case Fallback(fallback) => typer.typed1(fallback, EXPRmode, WildcardType) case Other(result) => @@ -828,7 +826,7 @@ trait Macros { self: Analyzer => case _ => } collectMacroArgs(expandee) - val context = expandee.attachmentOpt[MacroAttachment].flatMap(_.context).getOrElse(macroContext(typer, prefixTree, expandee)) + val context = expandee.attachmentOpt[MacroAttachment].flatMap(_.macroContext).getOrElse(macroContext(typer, prefixTree, expandee)) var argss: List[List[Any]] = List(context) :: exprArgs.toList macroTrace("argss: ")(argss) @@ -1007,18 +1005,6 @@ trait Macros { self: Analyzer => } finally { openMacros = openMacros.tail } - case Delay(expandee) => - // need to save the context to preserve enclosures - macroArgs(typer, expandee) match { - case Some((context: MacroContext) :: _) => - // adapting here would be premature, we must wait until undetparams are inferred - expandee withAttachment MacroAttachment(delayed = true, context = Some(context)) - case _ => - // !!! The correct place to issue an error needs to be clarified. - // I have the else condition here only as a fallback. - if (expandee.isErroneous) expandee - else fail("macros cannot be partially applied", expandee) - } case Fallback(fallback) => typer.context.withImplicitsEnabled(typer.typed(fallback, EXPRmode, pt)) case Other(result) => @@ -1032,8 +1018,8 @@ trait Macros { self: Analyzer => private sealed abstract class MacroExpansionResult extends Product with Serializable private case class Success(expanded: Tree) extends MacroExpansionResult private case class Fallback(fallback: Tree) extends MacroExpansionResult - private case class Delay(expandee: Tree) extends MacroExpansionResult private case class Other(result: Tree) extends MacroExpansionResult + private def Delay(expanded: Tree) = Other(expanded) private def Skip(expanded: Tree) = Other(expanded) private def Cancel(expandee: Tree) = Other(expandee) private def Failure(expandee: Tree) = Other(expandee) @@ -1086,75 +1072,78 @@ trait Macros { self: Analyzer => if (!wasDelayed) { if (macroDebug || macroCopypaste) println("typechecking macro expansion %s at %s".format(expandee, expandee.pos)) - if (nowDelayed) { - if (macroDebug || macroCopypaste) println("macro expansion is delayed: %s".format(expandee)) - delayed += expandee -> (typer.context, undetparams) - Delay(expandee) - } else { - val args = macroArgs(typer, expandee) - args match { - case Some(args) => - // adding stuff to openMacros is easy, but removing it is a nightmare - // it needs to be sprinkled over several different code locations - val (context: MacroContext) :: _ = args - openMacros = context :: openMacros - val expanded: MacroExpansionResult = try { - val prevNumErrors = reporter.ERROR.count - expandee.detach(null) - val expanded = runtime(args) - val currNumErrors = reporter.ERROR.count - if (currNumErrors != prevNumErrors) { - fail(typer, expandee) // errors have been reported by the macro itself - } else { - expanded match { - case expanded: Expr[_] => - if (macroDebug || macroCopypaste) { - if (macroDebug) println("original:") - println(expanded.tree) - println(showRaw(expanded.tree)) - } - - freeTerms(expanded.tree) foreach (fte => typer.context.error(expandee.pos, - ("macro expansion contains free term variable %s %s. "+ - "have you forgot to use eval when splicing this variable into a reifee? " + - "if you have troubles tracking free term variables, consider using -Xlog-free-terms").format(fte.name, fte.origin))) - freeTypes(expanded.tree) foreach (fty => typer.context.error(expandee.pos, - ("macro expansion contains free type variable %s %s. "+ - "have you forgot to use c.TypeTag annotation for this type parameter? " + - "if you have troubles tracking free type variables, consider using -Xlog-free-types").format(fty.name, fty.origin))) - - val currNumErrors = reporter.ERROR.count - if (currNumErrors != prevNumErrors) { - fail(typer, expandee) - } else { - // inherit the position from the first position-ful expandee in macro callstack - // this is essential for sane error messages - var tree = expanded.tree - var position = openMacros.find(c => c.expandee.pos != NoPosition).map(_.expandee.pos).getOrElse(NoPosition) - tree = atPos(position.focus)(tree) - - // now macro expansion gets typechecked against the macro definition return type - // however, this happens in macroExpand, not here in macroExpand1 - Success(tree) - } - case expanded if expanded.isInstanceOf[Expr[_]] => - val msg = "macro must return a compiler-specific expr; returned value is Expr, but it doesn't belong to this compiler's universe" - fail(typer, expandee, msg) - case expanded => - val msg = "macro must return a compiler-specific expr; returned value is of class: %s".format(expanded.getClass) - fail(typer, expandee, msg) + val args = macroArgs(typer, expandee) + args match { + case Some(args) => + val (context: MacroContext) :: _ = args + if (nowDelayed) { + if (macroDebug || macroCopypaste) println("macro expansion is delayed: %s".format(expandee)) + delayed += expandee -> undetparams + // need to save typer context for `macroExpandAll` + // need to save macro context to preserve enclosures + expandee attach MacroAttachment(delayed = true, typerContext = typer.context, macroContext = Some(context)) + Delay(expandee) + } else { + val expanded: MacroExpansionResult = + try { + val prevNumErrors = reporter.ERROR.count + openMacros = context :: openMacros + val expanded = runtime(args) + val currNumErrors = reporter.ERROR.count + if (currNumErrors != prevNumErrors) { + fail(typer, expandee) // errors have been reported by the macro itself + } else { + expanded match { + case expanded: Expr[_] => + if (macroDebug || macroCopypaste) { + if (macroDebug) println("original:") + println(expanded.tree) + println(showRaw(expanded.tree)) + } + + freeTerms(expanded.tree) foreach (fte => typer.context.error(expandee.pos, + ("macro expansion contains free term variable %s %s. "+ + "have you forgot to use eval when splicing this variable into a reifee? " + + "if you have troubles tracking free term variables, consider using -Xlog-free-terms").format(fte.name, fte.origin))) + freeTypes(expanded.tree) foreach (fty => typer.context.error(expandee.pos, + ("macro expansion contains free type variable %s %s. "+ + "have you forgot to use c.TypeTag annotation for this type parameter? " + + "if you have troubles tracking free type variables, consider using -Xlog-free-types").format(fty.name, fty.origin))) + + val currNumErrors = reporter.ERROR.count + if (currNumErrors != prevNumErrors) { + fail(typer, expandee) + } else { + // inherit the position from the first position-ful expandee in macro callstack + // this is essential for sane error messages + var tree = expanded.tree + var position = openMacros.find(c => c.expandee.pos != NoPosition).map(_.expandee.pos).getOrElse(NoPosition) + tree = atPos(position.focus)(tree) + + // now macro expansion gets typechecked against the macro definition return type + // however, this happens in macroExpand, not here in macroExpand1 + Success(tree) + } + case expanded if expanded.isInstanceOf[Expr[_]] => + val msg = "macro must return a compiler-specific expr; returned value is Expr, but it doesn't belong to this compiler's universe" + fail(typer, expandee, msg) + case expanded => + val msg = "macro must return a compiler-specific expr; returned value is of class: %s".format(expanded.getClass) + fail(typer, expandee, msg) + } } + } catch { + case ex: Throwable => + openMacros = openMacros.tail + throw ex + } finally { + expandee.detach(classOf[MacroAttachment]) } - } catch { - case ex: Throwable => - openMacros = openMacros.tail - throw ex - } if (!expanded.isInstanceOf[Success]) openMacros = openMacros.tail expanded - case None => - fail(typer, expandee) // error has been reported by macroArgs - } + } + case None => + fail(typer, expandee) // error has been reported by macroArgs } } else { if (nowDelayed) @@ -1164,8 +1153,6 @@ trait Macros { self: Analyzer => } } catch { case ex => handleMacroExpansionException(typer, expandee, ex) - } finally { - expandee.detach(classOf[MacroAttachment]) } private def macroExpandWithoutRuntime(typer: Typer, expandee: Tree): MacroExpansionResult = { @@ -1261,10 +1248,10 @@ trait Macros { self: Analyzer => * 2) undetparams (sym.isTypeParameter && !sym.isSkolem) */ var hasPendingMacroExpansions = false - private val delayed = perRunCaches.newWeakMap[Tree, (Context, collection.mutable.Set[Int])] + private val delayed = perRunCaches.newWeakMap[Tree, collection.mutable.Set[Int]] private def isDelayed(expandee: Tree) = delayed contains expandee private def calculateUndetparams(expandee: Tree): collection.mutable.Set[Int] = - delayed.get(expandee).map(_._2).getOrElse { + delayed.get(expandee).getOrElse { val calculated = collection.mutable.Set[Symbol]() expandee foreach (sub => { def traverse(sym: Symbol) = if (sym != null && (undetparams contains sym.id)) calculated += sym @@ -1284,7 +1271,7 @@ trait Macros { self: Analyzer => if (macroDebug) (undetNoMore zip inferreds) foreach {case (sym, tpe) => println("undetParam inferred: %s as %s".format(sym, tpe))} if (!delayed.isEmpty) delayed.toList foreach { - case (expandee, (_, undetparams)) if !undetparams.isEmpty => + case (expandee, undetparams) if !undetparams.isEmpty => undetparams --= undetNoMore map (_.id) if (undetparams.isEmpty) { hasPendingMacroExpansions = true @@ -1304,7 +1291,7 @@ trait Macros { self: Analyzer => override def transform(tree: Tree) = super.transform(tree match { // todo. expansion should work from the inside out case wannabe if (delayed contains wannabe) && calculateUndetparams(wannabe).isEmpty => - val (context, _) = delayed(wannabe) + val context = wannabe.attachment[MacroAttachment].typerContext delayed -= wannabe context.implicitsEnabled = typer.context.implicitsEnabled context.enrichmentEnabled = typer.context.enrichmentEnabled diff --git a/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala b/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala new file mode 100644 index 0000000000..329a247106 --- /dev/null +++ b/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala @@ -0,0 +1,10 @@ +package scala.tools.nsc +package typechecker + +import scala.reflect.makro.runtime.{Context => MacroContext} + +trait StdAttachments { + self: Analyzer => + + case class MacroAttachment(delayed: Boolean, typerContext: Context, macroContext: Option[MacroContext]) +} \ No newline at end of file -- cgit v1.2.3 From b242637ee1fc6db79992a67073219ec87150d06a Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Wed, 25 Apr 2012 21:13:29 +0300 Subject: diagnostics for SI-5692 --- src/compiler/scala/tools/nsc/symtab/classfile/Pickler.scala | 13 ++++++++++--- test/pending/run/t5692.flags | 1 + test/pending/run/t5692/Impls_Macros_1.scala | 9 +++++++++ test/pending/run/t5692/Test_2.scala | 4 ++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 test/pending/run/t5692.flags create mode 100644 test/pending/run/t5692/Impls_Macros_1.scala create mode 100644 test/pending/run/t5692/Test_2.scala diff --git a/src/compiler/scala/tools/nsc/symtab/classfile/Pickler.scala b/src/compiler/scala/tools/nsc/symtab/classfile/Pickler.scala index 52648380ec..192cc94b90 100644 --- a/src/compiler/scala/tools/nsc/symtab/classfile/Pickler.scala +++ b/src/compiler/scala/tools/nsc/symtab/classfile/Pickler.scala @@ -62,9 +62,16 @@ abstract class Pickler extends SubComponent { // when we pickle it: so let's report an error instead. We know next // to nothing about what happened, but our supposition is a lot better // than "bad type: " in terms of explanatory power. - for (t <- unit.body ; if t.isErroneous) { - unit.error(t.pos, "erroneous or inaccessible type") - return + for (t <- unit.body) { + if (t.isErroneous) { + unit.error(t.pos, "erroneous or inaccessible type") + return + } + + if (!t.isDef && t.hasSymbol && t.symbol.isTermMacro) { + unit.error(t.pos, "macro has not been expanded") + return + } } pickle(unit.body) diff --git a/test/pending/run/t5692.flags b/test/pending/run/t5692.flags new file mode 100644 index 0000000000..cd66464f2f --- /dev/null +++ b/test/pending/run/t5692.flags @@ -0,0 +1 @@ +-language:experimental.macros \ No newline at end of file diff --git a/test/pending/run/t5692/Impls_Macros_1.scala b/test/pending/run/t5692/Impls_Macros_1.scala new file mode 100644 index 0000000000..f9c1e5f12b --- /dev/null +++ b/test/pending/run/t5692/Impls_Macros_1.scala @@ -0,0 +1,9 @@ +import scala.reflect.makro.Context + +object Impls { + def impl[A](c: reflect.makro.Context) = c.reify(()) +} + +object Macros { + def decl[A] = macro Impls.impl[A] +} \ No newline at end of file diff --git a/test/pending/run/t5692/Test_2.scala b/test/pending/run/t5692/Test_2.scala new file mode 100644 index 0000000000..29251a5ef5 --- /dev/null +++ b/test/pending/run/t5692/Test_2.scala @@ -0,0 +1,4 @@ +object Test extends App { + val x = Macros.decl + def y() { Macros.decl(); } +} \ No newline at end of file -- cgit v1.2.3