From dfbaaa179fddd3d37abf66a08080f646b8557b6f Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sun, 27 Jan 2013 13:31:06 +0100 Subject: SI-6187 Make partial functions re-typable - `New(tpe)` doesn't survive a `resetAttrs` / typecheck; use a name instead. - Abandon the tree attachment that passed the default case from `typer` to `patmat`; this tree eluded the attribute reset performed in the macro. Instead, add it to the match. Apart from making the tree re-typable, it also exposes the true code structure to macros, which is important if they need to perform other code transformations. - Install original trees on the declared types of the parameters of the `applyOrElse` method to ensure that references to them within the method pick up the correct type parameter skolems upon retypechecking. - Propagate `TypeTree#original` through `copyAttrs`, which is called during tree duplication / `TreeCopiers`. Without this, the original trees that we installed were not visible anymore during `ResetAttrs`. We are not able to reify partial functions yet -- the particular sticking point is reification of the parentage which is only available in the `ClassInfoType`. --- test/files/run/idempotency-partial-functions.scala | 25 ----------------- test/files/run/t6187.check | 32 ++++++++++++++++++++++ test/files/run/t6187.scala | 18 ++++++++++++ test/files/run/t6187b.scala | 5 ++++ 4 files changed, 55 insertions(+), 25 deletions(-) delete mode 100644 test/files/run/idempotency-partial-functions.scala create mode 100644 test/files/run/t6187.check create mode 100644 test/files/run/t6187.scala create mode 100644 test/files/run/t6187b.scala (limited to 'test/files/run') diff --git a/test/files/run/idempotency-partial-functions.scala b/test/files/run/idempotency-partial-functions.scala deleted file mode 100644 index dd5f1167f1..0000000000 --- a/test/files/run/idempotency-partial-functions.scala +++ /dev/null @@ -1,25 +0,0 @@ -import scala.reflect.runtime.universe._ -import scala.reflect.runtime.{currentMirror => cm} -import scala.tools.reflect.{ToolBox, ToolBoxError} -import scala.tools.reflect.Eval - -object Test extends App { - val partials = reify { - List((false,true)) collect { case (x,true) => x } - } - try { - println(partials.eval) - } catch { - case _: ToolBoxError => println("error!!") - } - try { - val tb = cm.mkToolBox() - val tpartials = tb.typeCheck(partials.tree) - println(tpartials) - val rtpartials = tb.resetAllAttrs(tpartials) - println(tb.eval(rtpartials)) - } catch { - // this is the current behaviour, rather than the desired behavior; see SI-6187 - case _: ToolBoxError => println("error!") - } -} \ No newline at end of file diff --git a/test/files/run/t6187.check b/test/files/run/t6187.check new file mode 100644 index 0000000000..c0ca02923b --- /dev/null +++ b/test/files/run/t6187.check @@ -0,0 +1,32 @@ +Type in expressions to have them evaluated. +Type :help for more information. + +scala> import language.experimental.macros, reflect.macros.Context +import language.experimental.macros +import reflect.macros.Context + +scala> def macroImpl[T: c.WeakTypeTag](c: Context)(t: c.Expr[T]): c.Expr[List[T]] = { + val r = c.universe.reify { List(t.splice) } + c.Expr[List[T]]( c.resetLocalAttrs(r.tree) ) +} +macroImpl: [T](c: scala.reflect.macros.Context)(t: c.Expr[T])(implicit evidence$1: c.WeakTypeTag[T])c.Expr[List[T]] + +scala> def demo[T](t: T): List[T] = macro macroImpl[T] +demo: [T](t: T)List[T] + +scala> def m[T](t: T): List[List[T]] = + demo( List((t,true)) collect { case (x,true) => x } ) +m: [T](t: T)List[List[T]] + +scala> m(List(1)) +res0: List[List[List[Int]]] = List(List(List(1))) + +scala> // Showing we haven't added unreachable warnings + +scala> List(1) collect { case x => x } +res1: List[Int] = List(1) + +scala> List("") collect { case x => x } +res2: List[String] = List("") + +scala> diff --git a/test/files/run/t6187.scala b/test/files/run/t6187.scala new file mode 100644 index 0000000000..ae642917e7 --- /dev/null +++ b/test/files/run/t6187.scala @@ -0,0 +1,18 @@ +import scala.tools.partest.ReplTest + +object Test extends ReplTest { + override def code = """ +import language.experimental.macros, reflect.macros.Context +def macroImpl[T: c.WeakTypeTag](c: Context)(t: c.Expr[T]): c.Expr[List[T]] = { + val r = c.universe.reify { List(t.splice) } + c.Expr[List[T]]( c.resetLocalAttrs(r.tree) ) +} +def demo[T](t: T): List[T] = macro macroImpl[T] +def m[T](t: T): List[List[T]] = + demo( List((t,true)) collect { case (x,true) => x } ) +m(List(1)) +// Showing we haven't added unreachable warnings +List(1) collect { case x => x } +List("") collect { case x => x } + """.trim +} diff --git a/test/files/run/t6187b.scala b/test/files/run/t6187b.scala new file mode 100644 index 0000000000..d2d3e9797d --- /dev/null +++ b/test/files/run/t6187b.scala @@ -0,0 +1,5 @@ +object Test extends App { + val x: PartialFunction[Int, Int] = { case 1 => 1 } + val o: Any = "" + assert(x.applyOrElse(0, (_: Int) => o) == "") +} -- cgit v1.2.3