summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2014-05-23 12:26:08 +0200
committerEugene Burmako <xeno.by@gmail.com>2014-05-23 12:26:08 +0200
commitfcb3932b32a2133612473e7b061859f97aa562df (patch)
tree9bcab3f20da1d1a1a83730805eb635979864f0c2
parentd079e769b9372daae8d7770c4156f85ea1af6621 (diff)
downloadscala-fcb3932b32a2133612473e7b061859f97aa562df.tar.gz
scala-fcb3932b32a2133612473e7b061859f97aa562df.tar.bz2
scala-fcb3932b32a2133612473e7b061859f97aa562df.zip
macro args now correctly preserve range positions
Somewhen in the 2.11.0 development cycle we started duplicating macro arguments for increased robustness. What wasn't taken into account though is that Tree.duplicate destroys range positions. This commit fixes the problem. 2.10.x is unaffected by this bug, because it doesn't duplicate the args yet.
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Macros.scala5
-rw-r--r--test/files/run/macro-rangepos-args.check1
-rw-r--r--test/files/run/macro-rangepos-args.flags1
-rw-r--r--test/files/run/macro-rangepos-args/Macros_1.scala10
-rw-r--r--test/files/run/macro-rangepos-args/Test_2.scala4
5 files changed, 19 insertions, 2 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Macros.scala b/src/compiler/scala/tools/nsc/typechecker/Macros.scala
index f4456998c0..f40a92f8e5 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Macros.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Macros.scala
@@ -417,9 +417,10 @@ trait Macros extends MacroRuntimes with Traces with Helpers {
val wrappedArgs = mapWithIndex(args)((arg, j) => {
val fingerprint = implParams(min(j, implParams.length - 1))
+ val duplicatedArg = duplicateAndKeepPositions(arg)
fingerprint match {
- case LiftedTyped => context.Expr[Nothing](arg.duplicate)(TypeTag.Nothing) // TODO: SI-5752
- case LiftedUntyped => arg.duplicate
+ case LiftedTyped => context.Expr[Nothing](duplicatedArg)(TypeTag.Nothing) // TODO: SI-5752
+ case LiftedUntyped => duplicatedArg
case _ => abort(s"unexpected fingerprint $fingerprint in $binding with paramss being $paramss " +
s"corresponding to arg $arg in $argss")
}
diff --git a/test/files/run/macro-rangepos-args.check b/test/files/run/macro-rangepos-args.check
new file mode 100644
index 0000000000..d779505c66
--- /dev/null
+++ b/test/files/run/macro-rangepos-args.check
@@ -0,0 +1 @@
+Line: 3. Width: 5.
diff --git a/test/files/run/macro-rangepos-args.flags b/test/files/run/macro-rangepos-args.flags
new file mode 100644
index 0000000000..fcf951d907
--- /dev/null
+++ b/test/files/run/macro-rangepos-args.flags
@@ -0,0 +1 @@
+-Yrangepos \ No newline at end of file
diff --git a/test/files/run/macro-rangepos-args/Macros_1.scala b/test/files/run/macro-rangepos-args/Macros_1.scala
new file mode 100644
index 0000000000..97b938613c
--- /dev/null
+++ b/test/files/run/macro-rangepos-args/Macros_1.scala
@@ -0,0 +1,10 @@
+import scala.language.experimental.macros
+import scala.reflect.macros.blackbox.Context
+
+object Macros {
+ def impl(c: Context)(x: c.Tree): c.Tree = {
+ import c.universe._
+ Literal(Constant(s"Line: ${x.pos.line}. Width: ${x.pos.end - x.pos.start}."))
+ }
+ def pos(x: Any): String = macro impl
+}
diff --git a/test/files/run/macro-rangepos-args/Test_2.scala b/test/files/run/macro-rangepos-args/Test_2.scala
new file mode 100644
index 0000000000..8c770e9010
--- /dev/null
+++ b/test/files/run/macro-rangepos-args/Test_2.scala
@@ -0,0 +1,4 @@
+object Test extends App {
+ val x = 2
+ println(Macros.pos(x + 2))
+} \ No newline at end of file