Skip to content

Commit d89ccf5

Browse files
committed
Add error reporting to TASTy reflect
1 parent fa232e0 commit d89ccf5

File tree

8 files changed

+86
-0
lines changed

8 files changed

+86
-0
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
2929

3030
def Context_source(self: Context): java.nio.file.Path = self.compilationUnit.source.file.jpath
3131

32+
//
33+
// REPORTING
34+
//
35+
36+
def error(msg: => String, pos: Position)(implicit ctx: Context): Unit =
37+
ctx.error(msg, pos)
38+
39+
def warning(msg: => String, pos: Position)(implicit ctx: Context): Unit =
40+
ctx.warning(msg, pos)
41+
3242
//
3343
// Settings
3444
//

compiler/src/dotty/tools/dotc/typer/Inliner.scala

+1
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
441441
case tree: Ident =>
442442
paramProxy.get(tree.tpe) match {
443443
case Some(t) if tree.isTerm && t.isSingleton =>
444+
// FIXME wrong span, this is the span inside the inline method
444445
singleton(t.dealias).withSpan(tree.span)
445446
case Some(t) if tree.isType =>
446447
TypeTree(t).withSpan(tree.span)

library/src/scala/tasty/Reflection.scala

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ abstract class Reflection
1414
with PatternOps
1515
with PositionOps
1616
with Printers
17+
with ReportingOps
1718
with RootPosition
1819
with SettingsOps
1920
with SignatureOps

library/src/scala/tasty/reflect/Kernel.scala

+10
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ trait Kernel {
136136
/** Returns the source file being compiled. The path is relative to the current working directory. */
137137
def Context_source(self: Context): java.nio.file.Path
138138

139+
//
140+
// REPORTING
141+
//
142+
143+
/** Report a compilation error with the given message at the given position */
144+
def error(msg: => String, pos: Position)(implicit ctx: Context): Unit
145+
146+
/** Report a compilation warning with the given message at the given position */
147+
def warning(msg: => String, pos: Position)(implicit ctx: Context): Unit
148+
139149
//
140150
// Settings
141151
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package scala.tasty.reflect
2+
3+
trait ReportingOps extends Core {
4+
5+
def error(msg: => String, pos: Position)(implicit ctx: Context): Unit =
6+
kernel.error(msg, pos)
7+
8+
def warning(msg: => String, pos: Position)(implicit ctx: Context): Unit =
9+
kernel.warning(msg, pos)
10+
11+
}

tests/neg/tasty-macro-error.check

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[108..109] in quoted_2.scala
2+
here is the the argument x
3+
[206..207] in quoted_2.scala
4+
here is the the argument "abc"
5+
[108..109] in quoted_2.scala
6+
here is the the argument x
7+
[128..133] in quoted_2.scala
8+
here is the the argument "abc"
9+
// TODO
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import scala.quoted._
2+
3+
import scala.tasty._
4+
5+
object Macros {
6+
7+
inline def fun(x: Any): Unit = ${ impl('x) }
8+
9+
inline def fun2(x: =>Any): Unit = ${ impl('x) }
10+
11+
inline def fun3[T]: Unit = ${ impl2('[T]) }
12+
13+
def impl(x: Expr[Any])(implicit reflect: Reflection): Expr[Unit] = {
14+
import reflect._
15+
error("here is the the argument " + x.unseal.showCode, x.unseal.underlyingArgument.pos)
16+
'{}
17+
}
18+
19+
def impl2[T](x: quoted.Type[T])(implicit reflect: Reflection): Expr[Unit] = {
20+
import reflect._
21+
error("here is the the argument " + x.unseal.showCode, x.unseal.pos)
22+
'{}
23+
}
24+
25+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import Macros._
3+
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
def x: String = "abc"
7+
fun(x) // error
8+
fun("abc") // error
9+
10+
fun2(x) // error
11+
fun2("abc") // error
12+
13+
type T
14+
fun3[T] // error
15+
fun3[String] // error
16+
fun3["abc"] // error
17+
}
18+
19+
}

0 commit comments

Comments
 (0)