From c619f94a9cfbddc12c9c5df3affb4636f8982a0a Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sat, 8 Sep 2012 18:37:59 +0200 Subject: SI-6331 Avoid typing an If tree with a constant type. The fast path in typedIf added in 8552740b avoided lubbing the if/else branch types if they are identical, but this fails to deconst the type. This could lead to the entire if expression being replaced by a constant. Also introduces a new tool in partest for nicer checkfiles. // in Test.scala trace(if (t) -0d else 0d) // in Test.check trace> if (Test.this.t) -0.0 else 0.0 res: Double = -0.0 --- src/partest/scala/tools/partest/package.scala | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/partest') diff --git a/src/partest/scala/tools/partest/package.scala b/src/partest/scala/tools/partest/package.scala index 08934ef143..49d3ed301c 100644 --- a/src/partest/scala/tools/partest/package.scala +++ b/src/partest/scala/tools/partest/package.scala @@ -73,4 +73,31 @@ package object partest { def isPartestDebug: Boolean = propOrEmpty("partest.debug") == "true" + + + import language.experimental.macros + + /** + * `trace("".isEmpty)` will return `true` and as a side effect print the following to standard out. + * {{{ + * trace> "".isEmpty + * res: Boolean = true + * + * }}} + * + * An alternative to [[scala.tools.partest.ReplTest]] that avoids the inconvenience of embedding + * test code in a string. + */ + def trace[A](a: A) = macro traceImpl[A] + + import scala.reflect.macros.Context + def traceImpl[A: c.AbsTypeTag](c: Context)(a: c.Expr[A]): c.Expr[A] = { + import c.universe._ + val exprCode = c.literal(show(a.tree)) + val exprType = c.literal(show(a.actualType)) + reify { + println(s"trace> ${exprCode.splice}\nres: ${exprType.splice} = ${a.splice}\n") + a.splice + } + } } -- cgit v1.2.3