From 9ba7cf856b5eb6a9c6277a09f5da75e2bfea05c2 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Wed, 12 Dec 2012 14:19:58 +0100 Subject: fixes incorrect handling of Annotated in lazy copier --- test/files/pos/annotated-treecopy.check | 0 test/files/pos/annotated-treecopy.flags | 1 + .../pos/annotated-treecopy/Impls_Macros_1.scala | 53 ++++++++++++++++++++++ test/files/pos/annotated-treecopy/Test_2.scala | 5 ++ 4 files changed, 59 insertions(+) create mode 100644 test/files/pos/annotated-treecopy.check create mode 100644 test/files/pos/annotated-treecopy.flags create mode 100644 test/files/pos/annotated-treecopy/Impls_Macros_1.scala create mode 100644 test/files/pos/annotated-treecopy/Test_2.scala (limited to 'test') diff --git a/test/files/pos/annotated-treecopy.check b/test/files/pos/annotated-treecopy.check new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/files/pos/annotated-treecopy.flags b/test/files/pos/annotated-treecopy.flags new file mode 100644 index 0000000000..cd66464f2f --- /dev/null +++ b/test/files/pos/annotated-treecopy.flags @@ -0,0 +1 @@ +-language:experimental.macros \ No newline at end of file diff --git a/test/files/pos/annotated-treecopy/Impls_Macros_1.scala b/test/files/pos/annotated-treecopy/Impls_Macros_1.scala new file mode 100644 index 0000000000..d92fbca380 --- /dev/null +++ b/test/files/pos/annotated-treecopy/Impls_Macros_1.scala @@ -0,0 +1,53 @@ +import scala.language.experimental.macros +import scala.reflect.macros.Context +import collection.mutable.ListBuffer +import collection.mutable.Stack + +object Macros { + trait TypedFunction { + def tree: scala.reflect.runtime.universe.Tree + val typeIn: String + val typeOut: String + } + + 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) + (f:c.Expr[Function1[T,U]]): c.Expr[Function1[T,U]] = { + import c.universe._ + val ttag = c.weakTypeTag[U] + f match { + case Expr(Function(List(ValDef(_,n,tp,_)),b)) => + // normalize argument name + var b1 = new Transformer { + override def transform(tree: Tree): Tree = tree match { + case Ident(x) if (x==n) => Ident(newTermName("_arg")) + case tt @ TypeTree() if tt.original != null => TypeTree(tt.tpe) setOriginal transform(tt.original) + // without the fix to LazyTreeCopier.Annotated, we would need to uncomment the line below to make the macro work + // that's because the pattern match in the input expression gets expanded into Typed(, TypeTree()) + // with the original of the TypeTree being Annotated(<@unchecked>, Ident()) + // then the macro tries to replace all Ident() trees with Ident(<_arg>), recurs into the original of the TypeTree, changes it, + // but leaves the <@unchecked> part untouched. this signals the misguided LazyTreeCopier that the Annotated tree hasn't been modified, + // so the original tree should be copied over and returned => crash when later re-emerges from TypeTree.original + // case Annotated(annot, arg) => treeCopy.Annotated(tree, transform(annot).duplicate, transform(arg)) + case _ => super.transform(tree) + } + }.transform(b) + + val reifiedTree = c.reifyTree(treeBuild.mkRuntimeUniverseRef, EmptyTree, b1) + val reifiedExpr = c.Expr[scala.reflect.runtime.universe.Expr[T => U]](reifiedTree) + val template = + c.universe.reify(new (T => U) with TypedFunction { + override def toString = c.literal(tp+" => "+ttag.tpe+" { "+b1.toString+" } ").splice // DEBUG + def tree = reifiedExpr.splice.tree + val typeIn = c.literal(tp.toString).splice + val typeOut = c.literal(ttag.tpe.toString).splice + def apply(_arg: T): U = c.Expr[U](b1)(ttag.asInstanceOf[c.WeakTypeTag[U]]).splice + }) + val untyped = c.resetLocalAttrs(template.tree) + + c.Expr[T => U](untyped) + case _ => sys.error("Bad function type") + } + } +} \ No newline at end of file diff --git a/test/files/pos/annotated-treecopy/Test_2.scala b/test/files/pos/annotated-treecopy/Test_2.scala new file mode 100644 index 0000000000..836e0d888d --- /dev/null +++ b/test/files/pos/annotated-treecopy/Test_2.scala @@ -0,0 +1,5 @@ +object Test extends App { + import Macros._ + // tree { (x:((Int,Int,Int),(Int,Int,Int))) => { val y=x; val ((r1,m1,c1),(r2,m2,c2))=y; (r1, m1 + m2 + r1 * c1 * c2, c2) } } + tree { (x:((Int,Int,Int),(Int,Int,Int))) => { val ((r1,m1,c1),(r2,m2,c2))=x; (r1, m1 + m2 + r1 * c1 * c2, c2) } } +} \ No newline at end of file -- cgit v1.2.3