Skip to content

Add TASTy reflect error with custom positions #6158

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
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
18 changes: 17 additions & 1 deletion compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
def error(msg: => String, pos: Position)(implicit ctx: Context): Unit =
ctx.error(msg, pos)

def error(msg: => String, sourceFile: SourceFile, start: Int, end: Int)(implicit ctx: Context): Unit =
ctx.error(msg, util.SourcePosition(sourceFile, util.Spans.Span(start, end)))

def warning(msg: => String, pos: Position)(implicit ctx: Context): Unit =
ctx.warning(msg, pos)

def warning(msg: => String, sourceFile: SourceFile, start: Int, end: Int)(implicit ctx: Context): Unit =
ctx.error(msg, util.SourcePosition(sourceFile, util.Spans.Span(start, end)))

//
// Settings
//
Expand Down Expand Up @@ -1297,7 +1303,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.

def Position_exists(self: Position): Boolean = self.exists

def Position_sourceFile(self: Position): java.nio.file.Path = self.source.file.jpath
def Position_sourceFile(self: Position): SourceFile = self.source

def Position_startLine(self: Position): Int = self.startLine

Expand All @@ -1310,6 +1316,16 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
def Position_sourceCode(self: Position): String =
new String(self.source.content(), self.start, self.end - self.start)

//
// SOURCE FILES
//

type SourceFile = util.SourceFile

def SourceFile_jpath(self: SourceFile): java.nio.file.Path = self.file.jpath

def SourceFile_content(self: SourceFile): String = new String(self.content())

//
// COMMENTS
//
Expand Down
5 changes: 4 additions & 1 deletion library/src/scala/tasty/reflect/Core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,12 @@ trait Core {
/** JVM signature of a method */
type Signature = kernel.Signature

/** Source position */
/** Position in a source file */
type Position = kernel.Position

/** Scala source file */
type SourceFile = kernel.SourceFile

/** Comment */
type Comment = kernel.Comment

Expand Down
23 changes: 21 additions & 2 deletions library/src/scala/tasty/reflect/Kernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,15 @@ trait Kernel {
/** Report a compilation error with the given message at the given position */
def error(msg: => String, pos: Position)(implicit ctx: Context): Unit

/** Report a compilation error with the given message at the given position range */
def error(msg: => String, source: SourceFile, start: Int, end: Int)(implicit ctx: Context): Unit

/** Report a compilation warning with the given message at the given position */
def warning(msg: => String, pos: Position)(implicit ctx: Context): Unit

/** Report a compilation warning with the given message at the given position range */
def warning(msg: => String, source: SourceFile, start: Int, end: Int)(implicit ctx: Context): Unit

//
// Settings
//
Expand Down Expand Up @@ -1037,7 +1043,7 @@ trait Kernel {
// POSITIONS
//

/** Source position */
/** Position in a source file */
type Position <: AnyRef

/** The start offset in the source file */
Expand All @@ -1050,7 +1056,7 @@ trait Kernel {
def Position_exists(self: Position): Boolean

/** Source file in which this position is located */
def Position_sourceFile(self: Position): java.nio.file.Path
def Position_sourceFile(self: Position): SourceFile

/** The start line in the source file */
def Position_startLine(self: Position): Int
Expand All @@ -1067,6 +1073,19 @@ trait Kernel {
/** Source code within the position */
def Position_sourceCode(self: Position): String

//
// SOURCE FILE
//

/** Scala source file */
type SourceFile <: AnyRef

/** Path to a source file */
def SourceFile_jpath(self: SourceFile): java.nio.file.Path

/** Content of a source file */
def SourceFile_content(self: SourceFile): String

//
// COMMENTS
//
Expand Down
12 changes: 11 additions & 1 deletion library/src/scala/tasty/reflect/PositionOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait PositionOps extends Core {
def exists: Boolean = kernel.Position_exists(pos)

/** Source file in which this position is located */
def sourceFile: java.nio.file.Path = kernel.Position_sourceFile(pos)
def sourceFile: SourceFile = kernel.Position_sourceFile(pos)

/** The start line in the source file */
def startLine: Int = kernel.Position_startLine(pos)
Expand All @@ -33,4 +33,14 @@ trait PositionOps extends Core {

}

implicit class SourceFileAPI(sourceFile: SourceFile) {

/** Path to this source file */
def jpath: java.nio.file.Path = kernel.SourceFile_jpath(sourceFile)

/** Content of this source file */
def content: String = kernel.SourceFile_content(sourceFile)

}

}
5 changes: 5 additions & 0 deletions library/src/scala/tasty/reflect/ReportingOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ trait ReportingOps extends Core {
def error(msg: => String, pos: Position)(implicit ctx: Context): Unit =
kernel.error(msg, pos)

def error(msg: => String, source: SourceFile, start: Int, end: Int)(implicit ctx: Context): Unit =
kernel.error(msg, source, start, end)

def warning(msg: => String, pos: Position)(implicit ctx: Context): Unit =
kernel.warning(msg, pos)

def warning(msg: => String, source: SourceFile, start: Int, end: Int)(implicit ctx: Context): Unit =
kernel.warning(msg, source, start, end)
}
4 changes: 4 additions & 0 deletions tests/neg/tasty-macro-positions.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<122..125> in quoted_2.scala
here (+5) is the the argument is foo
[117..120] in quoted_2.scala
here is the the argument is foo
17 changes: 17 additions & 0 deletions tests/neg/tasty-macro-positions/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import scala.quoted._

import scala.tasty._

object Macros {

inline def fun(x: Any): Unit = ${ impl('x) }

def impl(x: Expr[Any])(implicit reflect: Reflection): Expr[Unit] = {
import reflect._
val pos = x.unseal.underlyingArgument.pos
error("here is the the argument is " + x.unseal.underlyingArgument.showCode, pos)
error("here (+5) is the the argument is " + x.unseal.underlyingArgument.showCode, pos.sourceFile, pos.start + 5, pos.end + 5)
'{}
}

}
11 changes: 11 additions & 0 deletions tests/neg/tasty-macro-positions/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import Macros._

object Test {
def main(args: Array[String]): Unit = {
def foo: String = "abc"
fun(
foo // error // error
)
}
}
2 changes: 1 addition & 1 deletion tests/run/tasty-macro-positions/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ object Macros {

def posStr(relfection: Reflection)(pos: relfection.Position): Expr[String] = {
import relfection._
s"${pos.sourceFile.getFileName.toString}:[${pos.start}..${pos.end}]".toExpr
s"${pos.sourceFile.jpath.getFileName.toString}:[${pos.start}..${pos.end}]".toExpr
}
}
2 changes: 1 addition & 1 deletion tests/run/tasty-positioned/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Positioned {
import reflect.{Position => _, _}
val pos = rootPosition

val path = pos.sourceFile.toString.toExpr
val path = pos.sourceFile.jpath.toString.toExpr
val start = pos.start.toExpr
val end = pos.end.toExpr
val startLine = pos.startLine.toExpr
Expand Down