Skip to content

Add rootPosition to Tasty reflect #4859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/tasty/Tasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions tests/run-with-compiler/tasty-positioned.check

This file was deleted.

37 changes: 0 additions & 37 deletions tests/run-with-compiler/tasty-positioned/quoted_1.scala

This file was deleted.

4 changes: 4 additions & 0 deletions tests/run/tasty-linenumber-2.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo 5
foo 6
foo 8
foo 9
19 changes: 19 additions & 0 deletions tests/run/tasty-linenumber-2/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -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))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

}
16 changes: 16 additions & 0 deletions tests/run/tasty-linenumber/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
8 changes: 8 additions & 0 deletions tests/run/tasty-positioned.check
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions tests/run/tasty-positioned/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down