From c6876ffd9ef44f9fe1b6619afd5621d3df251364 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 7 Feb 2019 14:02:18 +0100 Subject: [PATCH] Add sourceCode to TASTy reflect Position Partial fix for #5782: * get the ~raw code of arguments of the root context (in `def foo(s: Seq[Int])(implicit foo: Foo)`, with the implicit materialization of `Foo` backed by a macro - how can the macro get a String `"(1 to 10).sum"` in `f((1 to 10).sum)`?). Prevents to have `sourcecode.Text` work fine. --- .../tools/dotc/tastyreflect/PositionOpsImpl.scala | 2 ++ library/src/scala/tasty/reflect/PositionOps.scala | 3 +++ tests/run/tasty-original-source.check | 3 +++ tests/run/tasty-original-source/Macros_1.scala | 14 ++++++++++++++ tests/run/tasty-original-source/Test_2.scala | 13 +++++++++++++ 5 files changed, 35 insertions(+) create mode 100644 tests/run/tasty-original-source.check create mode 100644 tests/run/tasty-original-source/Macros_1.scala create mode 100644 tests/run/tasty-original-source/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/PositionOpsImpl.scala b/compiler/src/dotty/tools/dotc/tastyreflect/PositionOpsImpl.scala index 66d3bcac08f9..26b401dc90e3 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/PositionOpsImpl.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/PositionOpsImpl.scala @@ -15,5 +15,7 @@ trait PositionOpsImpl extends scala.tasty.reflect.PositionOps with CoreImpl { def startColumn: Int = pos.startColumn def endColumn: Int = pos.endColumn + + def sourceCode: String = new String(pos.source.content(), pos.start, pos.end - pos.start) } } diff --git a/library/src/scala/tasty/reflect/PositionOps.scala b/library/src/scala/tasty/reflect/PositionOps.scala index aba9fd344a02..625fc9ba1f42 100644 --- a/library/src/scala/tasty/reflect/PositionOps.scala +++ b/library/src/scala/tasty/reflect/PositionOps.scala @@ -26,6 +26,9 @@ trait PositionOps extends Core { /** The end column in the source file */ def endColumn: Int + /** Source code within the position */ + def sourceCode: String + } implicit def PositionDeco(pos: Position): PositionAPI diff --git a/tests/run/tasty-original-source.check b/tests/run/tasty-original-source.check new file mode 100644 index 000000000000..7ead7e557e02 --- /dev/null +++ b/tests/run/tasty-original-source.check @@ -0,0 +1,3 @@ +(foo("abc"),abc) +(foo( "def" ),def) +(foo("ghi" /* comment */),ghi) diff --git a/tests/run/tasty-original-source/Macros_1.scala b/tests/run/tasty-original-source/Macros_1.scala new file mode 100644 index 000000000000..44ce6448bbe7 --- /dev/null +++ b/tests/run/tasty-original-source/Macros_1.scala @@ -0,0 +1,14 @@ +import scala.quoted._ +import scala.tasty._ + +object Macros { + + implicit inline def withSource(arg: Any): (String, Any) = ~impl('(arg)) + + private def impl(arg: Expr[Any])(implicit reflect: Reflection): Expr[(String, Any)] = { + import reflect._ + val source = arg.unseal.underlyingArgument.pos.sourceCode.toString.toExpr + '(Tuple2(~source, ~arg)) + } + +} diff --git a/tests/run/tasty-original-source/Test_2.scala b/tests/run/tasty-original-source/Test_2.scala new file mode 100644 index 000000000000..9a49456af140 --- /dev/null +++ b/tests/run/tasty-original-source/Test_2.scala @@ -0,0 +1,13 @@ + +import Macros._ + +object Test { + def main(args: Array[String]): Unit = { + println(withSource(foo("abc"))) + println(withSource( foo( "def" ))) + println(withSource(foo("ghi" /* comment */))) + } + + def foo(x: Any): Any = x + +}