diff --git a/compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala b/compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala index 8bb791616a69..2052e0324823 100644 --- a/compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala +++ b/compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala @@ -3,16 +3,14 @@ package dotty.tools.dotc.quoted import dotty.tools.dotc.ast.tpd import dotty.tools.dotc.Driver import dotty.tools.dotc.core.Contexts.{Context, ContextBase} +import dotty.tools.dotc.tastyreflect.TastyImpl import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory} import dotty.tools.repl.AbstractFileClassLoader import scala.quoted.{Expr, Type} import scala.quoted.Toolbox - import java.net.URLClassLoader -import dotty.tools.dotc.tastyreflect.TastyImpl - class QuoteDriver extends Driver { import tpd._ diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala b/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala index 4b6409a80310..ff9e8ca1d556 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala @@ -9,7 +9,7 @@ import dotty.tools.dotc.core._ import dotty.tools.dotc.core.quoted.PickledQuotes import dotty.tools.dotc.reporting.Reporter import dotty.tools.dotc.reporting.diagnostic.MessageContainer -import dotty.tools.dotc.util.SourcePosition +import dotty.tools.dotc.util.{Positions, SourcePosition} import scala.quoted import scala.reflect.ClassTag @@ -45,6 +45,8 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s def source: java.nio.file.Path = ctx.compilationUnit.source.file.jpath } + def rootPosition: SourcePosition = SourcePosition(rootContext.source, Positions.NoPosition) + // ===== Id ======================================================= type Id = untpd.Ident diff --git a/compiler/src/dotty/tools/dotc/transform/Splicer.scala b/compiler/src/dotty/tools/dotc/transform/Splicer.scala index cd2cc08ee4c3..80b0446916e0 100644 --- a/compiler/src/dotty/tools/dotc/transform/Splicer.scala +++ b/compiler/src/dotty/tools/dotc/transform/Splicer.scala @@ -21,6 +21,7 @@ import dotty.tools.dotc.tastyreflect.TastyImpl import scala.util.control.NonFatal import dotty.tools.dotc.util.Positions.Position +import dotty.tools.dotc.util.SourcePosition import scala.reflect.ClassTag @@ -103,7 +104,9 @@ object Splicer { value.asInstanceOf[Object] protected def interpretTastyContext()(implicit env: Env): Object = - new TastyImpl(ctx) + new TastyImpl(ctx) { + override def rootPosition: SourcePosition = SourcePosition(ctx.source, pos) + } protected def interpretStaticMethodCall(fn: Tree, args: => List[Object])(implicit env: Env): Object = { val (clazz, instance) = loadModule(fn.symbol.owner) diff --git a/library/src/scala/tasty/Tasty.scala b/library/src/scala/tasty/Tasty.scala index 38137c94f321..7312ecb393ae 100644 --- a/library/src/scala/tasty/Tasty.scala +++ b/library/src/scala/tasty/Tasty.scala @@ -39,6 +39,9 @@ abstract class Tasty { tasty => implicit def rootContext: Context + /** Root position of this tasty context. For macros it corresponds to the expansion site. */ + def rootPosition: Position + // ===== Id ======================================================= type Id diff --git a/tests/run-with-compiler/tasty-positioned.check b/tests/run-with-compiler/tasty-positioned.check deleted file mode 100644 index cccdff761382..000000000000 --- a/tests/run-with-compiler/tasty-positioned.check +++ /dev/null @@ -1,3 +0,0 @@ -a -b columns:6-25 lines:15-16 -Foo columns:16-19 lines:17-17 diff --git a/tests/run-with-compiler/tasty-positioned/quoted_1.scala b/tests/run-with-compiler/tasty-positioned/quoted_1.scala deleted file mode 100644 index c7203a3b8cc3..000000000000 --- a/tests/run-with-compiler/tasty-positioned/quoted_1.scala +++ /dev/null @@ -1,37 +0,0 @@ -import scala.quoted._ - -import scala.tasty._ - -case class Position(path: String, start: Int, end: Int, - startLine: Int, startColumn: Int, endLine: Int, endColumn: Int) - -case class Positioned[T](value: T, position: Position) - -object Positioned { - - implicit transparent def apply[T](x: => T): Positioned[T] = - ~impl('(x))('[T], TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~ - - def impl[T](x: Expr[T])(implicit ev: Type[T], tasty: Tasty): Expr[Positioned[T]] = { - import tasty.{Position => _, _} - implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make - val pos = x.toTasty.pos - - x.toTasty match { - case Term.Inlined(_, Nil, y) => - val pos = y.pos - - val path = pos.sourceFile.toString.toExpr - val start = pos.start.toExpr - val end = pos.end.toExpr - val startLine = pos.startLine.toExpr - val endLine = pos.endLine.toExpr - val startColumn = pos.startColumn.toExpr - val endColumn = pos.endColumn.toExpr - - '(Positioned[T](~x, new Position(~path, ~start, ~end, ~startLine, ~startColumn, ~endLine, ~endColumn))) - case _ => - '(Positioned[T](~x, new Position(null, -1, -1, -1, -1, -1, -1))) - } - } -} diff --git a/tests/run/tasty-linenumber-2.check b/tests/run/tasty-linenumber-2.check new file mode 100644 index 000000000000..7f936437beb7 --- /dev/null +++ b/tests/run/tasty-linenumber-2.check @@ -0,0 +1,4 @@ +foo 5 +foo 6 +foo 8 +foo 9 \ No newline at end of file diff --git a/tests/run/tasty-linenumber-2/quoted_1.scala b/tests/run/tasty-linenumber-2/quoted_1.scala new file mode 100644 index 000000000000..6349af730d8b --- /dev/null +++ b/tests/run/tasty-linenumber-2/quoted_1.scala @@ -0,0 +1,19 @@ +import scala.quoted._ + +import scala.tasty._ + +class LineNumber(val value: Int) { + override def toString: String = value.toString +} + +object LineNumber { + + implicit transparent def line: LineNumber = + ~lineImpl(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~ + + def lineImpl(implicit tasty: Tasty): Expr[LineNumber] = { + import tasty._ + '(new LineNumber(~rootPosition.startLine.toExpr)) + } + +} diff --git a/tests/pending/run/tasty-linenumber/quoted_2.scala b/tests/run/tasty-linenumber-2/quoted_2.scala similarity index 100% rename from tests/pending/run/tasty-linenumber/quoted_2.scala rename to tests/run/tasty-linenumber-2/quoted_2.scala diff --git a/tests/pending/run/tasty-linenumber/quoted_1.scala b/tests/run/tasty-linenumber/quoted_1.scala similarity index 88% rename from tests/pending/run/tasty-linenumber/quoted_1.scala rename to tests/run/tasty-linenumber/quoted_1.scala index 09a42088f6e3..f7ab41c2ac52 100644 --- a/tests/pending/run/tasty-linenumber/quoted_1.scala +++ b/tests/run/tasty-linenumber/quoted_1.scala @@ -13,8 +13,7 @@ object LineNumber { def lineImpl(x: Type[Unit])(implicit tasty: Tasty): Expr[LineNumber] = { import tasty._ - - '(new LineNumber(~x.toTasty.pos.startLine.toExpr)) + '(new LineNumber(~rootPosition.startLine.toExpr)) } } diff --git a/tests/run/tasty-linenumber/quoted_2.scala b/tests/run/tasty-linenumber/quoted_2.scala new file mode 100644 index 000000000000..253f4f85ead9 --- /dev/null +++ b/tests/run/tasty-linenumber/quoted_2.scala @@ -0,0 +1,16 @@ + +import LineNumber._ + +object Test { + def main(args: Array[String]): Unit = { + foo(line) + foo + + foo + foo + } + + def foo(implicit line: LineNumber): Unit = { + println("foo " + line) + } +} diff --git a/tests/run/tasty-positioned.check b/tests/run/tasty-positioned.check new file mode 100644 index 000000000000..6730ef470015 --- /dev/null +++ b/tests/run/tasty-positioned.check @@ -0,0 +1,8 @@ +0 columns:13-26 lines:9-9 +10 columns:13-15 lines:10-10 +4530 columns:13-17 lines:11-11 +acbvasdfa columns:13-36 lines:12-12 +acbvasdfa columns:13-24 lines:13-13 +a +b columns:6-25 lines:15-16 +Foo columns:16-19 lines:17-17 diff --git a/tests/run/tasty-positioned/quoted_1.scala b/tests/run/tasty-positioned/quoted_1.scala new file mode 100644 index 000000000000..74ee127a9a0f --- /dev/null +++ b/tests/run/tasty-positioned/quoted_1.scala @@ -0,0 +1,30 @@ +import scala.quoted._ + +import scala.tasty._ + +case class Position(path: String, start: Int, end: Int, + startLine: Int, startColumn: Int, endLine: Int, endColumn: Int) + +case class Positioned[T](value: T, position: Position) + +object Positioned { + + implicit transparent def apply[T](x: => T): Positioned[T] = + ~impl('(x))('[T], TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~ + + def impl[T](x: Expr[T])(implicit ev: Type[T], tasty: Tasty): Expr[Positioned[T]] = { + import tasty.{Position => _, _} + val pos = rootPosition + + val path = pos.sourceFile.toString.toExpr + val start = pos.start.toExpr + val end = pos.end.toExpr + val startLine = pos.startLine.toExpr + val endLine = pos.endLine.toExpr + val startColumn = pos.startColumn.toExpr + val endColumn = pos.endColumn.toExpr + + val liftedPosition = '(new Position(~path, ~start, ~end, ~startLine, ~startColumn, ~endLine, ~endColumn)) + '(Positioned[T](~x, ~liftedPosition)) + } +} diff --git a/tests/run-with-compiler/tasty-positioned/quoted_2.scala b/tests/run/tasty-positioned/quoted_2.scala similarity index 67% rename from tests/run-with-compiler/tasty-positioned/quoted_2.scala rename to tests/run/tasty-positioned/quoted_2.scala index dfeed79753fa..4026b53b3ff2 100644 --- a/tests/run-with-compiler/tasty-positioned/quoted_2.scala +++ b/tests/run/tasty-positioned/quoted_2.scala @@ -7,11 +7,11 @@ object Test { val pos = x.position println(s"${x.value} columns:${pos.startColumn}-${pos.endColumn} lines:${pos.startLine}-${pos.endLine}") } -// printPos(Positioned(0)) // FIXME we cannot get possition of values that get inlined -// printPos(10) -// printPos(4530) -// printPos(Positioned("acbvasdfa")) -// printPos("acbvasdfa") + printPos(Positioned(0)) + printPos(10) + printPos(4530) + printPos(Positioned("acbvasdfa")) + printPos("acbvasdfa") printPos( """a |b""".stripMargin)