From b6755f6927c07337b0819d7503f2c7b1674d892f Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sun, 21 Dec 2014 12:44:03 +0100 Subject: tests reorgs Move some tests into proper slots + comments what they are. --- .../pos/annotated-original/C_2.scala | 7 +++ .../pos/annotated-original/M_1.scala | 7 +++ .../pos/annotated-treecopy/Impls_Macros_1.scala | 54 ++++++++++++++++++++++ .../pos/annotated-treecopy/Test_2.scala | 5 ++ tests/disabled/rewrite-needed/CustomGlobal.scala | 33 +++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 tests/disabled/not-representable/pos/annotated-original/C_2.scala create mode 100644 tests/disabled/not-representable/pos/annotated-original/M_1.scala create mode 100644 tests/disabled/not-representable/pos/annotated-treecopy/Impls_Macros_1.scala create mode 100644 tests/disabled/not-representable/pos/annotated-treecopy/Test_2.scala create mode 100644 tests/disabled/rewrite-needed/CustomGlobal.scala (limited to 'tests/disabled') diff --git a/tests/disabled/not-representable/pos/annotated-original/C_2.scala b/tests/disabled/not-representable/pos/annotated-original/C_2.scala new file mode 100644 index 000000000..36a09ffe0 --- /dev/null +++ b/tests/disabled/not-representable/pos/annotated-original/C_2.scala @@ -0,0 +1,7 @@ +object Bug { + M.m { + def s = "" + M.m(s): @unchecked // error: macro has not been expanded. + ??? + } +} diff --git a/tests/disabled/not-representable/pos/annotated-original/M_1.scala b/tests/disabled/not-representable/pos/annotated-original/M_1.scala new file mode 100644 index 000000000..84a01bcce --- /dev/null +++ b/tests/disabled/not-representable/pos/annotated-original/M_1.scala @@ -0,0 +1,7 @@ +import language.experimental.macros +import scala.reflect.macros.blackbox.Context + +object M { + def impl(c: Context)(a: c.Expr[Any]) = c.Expr[Any](c.untypecheck(a.tree)) + def m(a: Any) = macro impl +} diff --git a/tests/disabled/not-representable/pos/annotated-treecopy/Impls_Macros_1.scala b/tests/disabled/not-representable/pos/annotated-treecopy/Impls_Macros_1.scala new file mode 100644 index 000000000..986287dfa --- /dev/null +++ b/tests/disabled/not-representable/pos/annotated-treecopy/Impls_Macros_1.scala @@ -0,0 +1,54 @@ +import scala.language.experimental.macros +import scala.reflect.macros.blackbox.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._ + import internal._ + 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(TermName("_arg")) + case tt: TypeTree if tt.original != null => setOriginal(TypeTree(tt.tpe), 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(gen.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.Expr[String](q"""${tp+" => "+ttag.tpe+" { "+b1.toString+" } "}""").splice // DEBUG + def tree = reifiedExpr.splice.tree + val typeIn = c.Expr[String](q"${tp.toString}").splice + val typeOut = c.Expr[String](q"${ttag.tpe.toString}").splice + def apply(_arg: T): U = c.Expr[U](b1)(ttag.asInstanceOf[c.WeakTypeTag[U]]).splice + }) + val untyped = c.untypecheck(template.tree) + + c.Expr[T => U](untyped) + case _ => sys.error("Bad function type") + } + } +} diff --git a/tests/disabled/not-representable/pos/annotated-treecopy/Test_2.scala b/tests/disabled/not-representable/pos/annotated-treecopy/Test_2.scala new file mode 100644 index 000000000..1c6b862ef --- /dev/null +++ b/tests/disabled/not-representable/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) } } +} diff --git a/tests/disabled/rewrite-needed/CustomGlobal.scala b/tests/disabled/rewrite-needed/CustomGlobal.scala new file mode 100644 index 000000000..a5668bd7c --- /dev/null +++ b/tests/disabled/rewrite-needed/CustomGlobal.scala @@ -0,0 +1,33 @@ +package custom + +import scala.tools.nsc._, reporters._, typechecker._ + +/** Demonstration of a custom Global with a custom Typer, + * decoupled from trunk. Demonstration: + * +{{{ +scalac -d . CustomGlobal.scala && scala -nc -Yglobal-class custom.CustomGlobal \ + -e 'class Bippy(x: Int) ; def f = new Bippy(5)' + +I'm typing a Bippy! It's a ClassDef. +I'm typing a Bippy! It's a Ident. +I'm typing a Bippy! It's a DefDef. +}}} + * + */ +class CustomGlobal(currentSettings: Settings, reporter: Reporter) extends Global(currentSettings, reporter) { + override lazy val analyzer = new { + val global: CustomGlobal.this.type = CustomGlobal.this + } with Analyzer { + override def newTyper(context: Context): Typer = new CustomTyper(context) + + class CustomTyper(context : Context) extends Typer(context) { + override def typed(tree: Tree, mode: Mode, pt: Type): Tree = { + if (tree.summaryString contains "Bippy") + println("I'm typing a Bippy! It's a " + tree.shortClass + ".") + + super.typed(tree, mode, pt) + } + } + } +} -- cgit v1.2.3