Skip to content

Commit 896598a

Browse files
Merge pull request #4859 from dotty-staging/fix-position-macros
Add rootPosition to Tasty reflect
2 parents 87ca55d + 1d82c2d commit 896598a

File tree

14 files changed

+94
-52
lines changed

14 files changed

+94
-52
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ package dotty.tools.dotc.quoted
33
import dotty.tools.dotc.ast.tpd
44
import dotty.tools.dotc.Driver
55
import dotty.tools.dotc.core.Contexts.{Context, ContextBase}
6+
import dotty.tools.dotc.tastyreflect.TastyImpl
67
import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory}
78
import dotty.tools.repl.AbstractFileClassLoader
89

910
import scala.quoted.{Expr, Type}
1011
import scala.quoted.Toolbox
11-
1212
import java.net.URLClassLoader
1313

14-
import dotty.tools.dotc.tastyreflect.TastyImpl
15-
1614
class QuoteDriver extends Driver {
1715
import tpd._
1816

compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import dotty.tools.dotc.core._
99
import dotty.tools.dotc.core.quoted.PickledQuotes
1010
import dotty.tools.dotc.reporting.Reporter
1111
import dotty.tools.dotc.reporting.diagnostic.MessageContainer
12-
import dotty.tools.dotc.util.SourcePosition
12+
import dotty.tools.dotc.util.{Positions, SourcePosition}
1313

1414
import scala.quoted
1515
import scala.reflect.ClassTag
@@ -45,6 +45,8 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
4545
def source: java.nio.file.Path = ctx.compilationUnit.source.file.jpath
4646
}
4747

48+
def rootPosition: SourcePosition = SourcePosition(rootContext.source, Positions.NoPosition)
49+
4850
// ===== Id =======================================================
4951

5052
type Id = untpd.Ident

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import dotty.tools.dotc.tastyreflect.TastyImpl
2121

2222
import scala.util.control.NonFatal
2323
import dotty.tools.dotc.util.Positions.Position
24+
import dotty.tools.dotc.util.SourcePosition
2425

2526
import scala.reflect.ClassTag
2627

@@ -103,7 +104,9 @@ object Splicer {
103104
value.asInstanceOf[Object]
104105

105106
protected def interpretTastyContext()(implicit env: Env): Object =
106-
new TastyImpl(ctx)
107+
new TastyImpl(ctx) {
108+
override def rootPosition: SourcePosition = SourcePosition(ctx.source, pos)
109+
}
107110

108111
protected def interpretStaticMethodCall(fn: Tree, args: => List[Object])(implicit env: Env): Object = {
109112
val (clazz, instance) = loadModule(fn.symbol.owner)

library/src/scala/tasty/Tasty.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ abstract class Tasty { tasty =>
3939

4040
implicit def rootContext: Context
4141

42+
/** Root position of this tasty context. For macros it corresponds to the expansion site. */
43+
def rootPosition: Position
44+
4245
// ===== Id =======================================================
4346

4447
type Id

tests/run-with-compiler/tasty-positioned.check

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/run-with-compiler/tasty-positioned/quoted_1.scala

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/run/tasty-linenumber-2.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
foo 5
2+
foo 6
3+
foo 8
4+
foo 9
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import scala.quoted._
2+
3+
import scala.tasty._
4+
5+
class LineNumber(val value: Int) {
6+
override def toString: String = value.toString
7+
}
8+
9+
object LineNumber {
10+
11+
implicit transparent def line: LineNumber =
12+
~lineImpl(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
13+
14+
def lineImpl(implicit tasty: Tasty): Expr[LineNumber] = {
15+
import tasty._
16+
'(new LineNumber(~rootPosition.startLine.toExpr))
17+
}
18+
19+
}

tests/pending/run/tasty-linenumber/quoted_1.scala renamed to tests/run/tasty-linenumber/quoted_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ object LineNumber {
1313

1414
def lineImpl(x: Type[Unit])(implicit tasty: Tasty): Expr[LineNumber] = {
1515
import tasty._
16-
17-
'(new LineNumber(~x.toTasty.pos.startLine.toExpr))
16+
'(new LineNumber(~rootPosition.startLine.toExpr))
1817
}
1918

2019
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
import LineNumber._
3+
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
foo(line)
7+
foo
8+
9+
foo
10+
foo
11+
}
12+
13+
def foo(implicit line: LineNumber): Unit = {
14+
println("foo " + line)
15+
}
16+
}

tests/run/tasty-positioned.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
0 columns:13-26 lines:9-9
2+
10 columns:13-15 lines:10-10
3+
4530 columns:13-17 lines:11-11
4+
acbvasdfa columns:13-36 lines:12-12
5+
acbvasdfa columns:13-24 lines:13-13
6+
a
7+
b columns:6-25 lines:15-16
8+
Foo columns:16-19 lines:17-17
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import scala.quoted._
2+
3+
import scala.tasty._
4+
5+
case class Position(path: String, start: Int, end: Int,
6+
startLine: Int, startColumn: Int, endLine: Int, endColumn: Int)
7+
8+
case class Positioned[T](value: T, position: Position)
9+
10+
object Positioned {
11+
12+
implicit transparent def apply[T](x: => T): Positioned[T] =
13+
~impl('(x))('[T], TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
14+
15+
def impl[T](x: Expr[T])(implicit ev: Type[T], tasty: Tasty): Expr[Positioned[T]] = {
16+
import tasty.{Position => _, _}
17+
val pos = rootPosition
18+
19+
val path = pos.sourceFile.toString.toExpr
20+
val start = pos.start.toExpr
21+
val end = pos.end.toExpr
22+
val startLine = pos.startLine.toExpr
23+
val endLine = pos.endLine.toExpr
24+
val startColumn = pos.startColumn.toExpr
25+
val endColumn = pos.endColumn.toExpr
26+
27+
val liftedPosition = '(new Position(~path, ~start, ~end, ~startLine, ~startColumn, ~endLine, ~endColumn))
28+
'(Positioned[T](~x, ~liftedPosition))
29+
}
30+
}

tests/run-with-compiler/tasty-positioned/quoted_2.scala renamed to tests/run/tasty-positioned/quoted_2.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ object Test {
77
val pos = x.position
88
println(s"${x.value} columns:${pos.startColumn}-${pos.endColumn} lines:${pos.startLine}-${pos.endLine}")
99
}
10-
// printPos(Positioned(0)) // FIXME we cannot get possition of values that get inlined
11-
// printPos(10)
12-
// printPos(4530)
13-
// printPos(Positioned("acbvasdfa"))
14-
// printPos("acbvasdfa")
10+
printPos(Positioned(0))
11+
printPos(10)
12+
printPos(4530)
13+
printPos(Positioned("acbvasdfa"))
14+
printPos("acbvasdfa")
1515
printPos(
1616
"""a
1717
|b""".stripMargin)

0 commit comments

Comments
 (0)