Skip to content

Commit 37994e9

Browse files
committed
Add error reporting to TASTy reflect
1 parent 4447d83 commit 37994e9

File tree

8 files changed

+112
-0
lines changed

8 files changed

+112
-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
@@ -15,6 +15,7 @@ abstract class Reflection
1515
with PatternOps
1616
with PositionOps
1717
with Printers
18+
with ReportingOps
1819
with RootPosition
1920
with SettingsOps
2021
with SignatureOps

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

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

141+
//
142+
// REPORTING
143+
//
144+
145+
/** Report a compilation error with the given message at the given position */
146+
def error(msg: => String, pos: Position)(implicit ctx: Context): Unit
147+
148+
/** Report a compilation warning with the given message at the given position */
149+
def warning(msg: => String, pos: Position)(implicit ctx: Context): Unit
150+
141151
//
142152
// Settings
143153
//
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

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[486..500] in quoted_2.scala
2+
here is the the argument is _root_.scala.StringContext.apply(("abc", "": scala.<repeated>[scala#Predef.String])).s(("def": scala.<repeated>[scala.Any]))
3+
[453..466] in quoted_2.scala
4+
here is the the argument is ("abc": scala.Predef.String)
5+
[407..408] in quoted_2.scala
6+
here is the the argument is d
7+
[386..387] in quoted_2.scala
8+
here is the the argument is c
9+
[309..323] in quoted_2.scala
10+
here is the the argument is _root_.scala.StringContext.apply(("abc", "": scala.<repeated>[scala#Predef.String])).s(("def": scala.<repeated>[scala.Any]))
11+
[277..290] in quoted_2.scala
12+
here is the the argument is ("abc": scala.Predef.String)
13+
[233..234] in quoted_2.scala
14+
here is the the argument is d
15+
[213..214] in quoted_2.scala
16+
here is the the argument is c
+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 is " + x.unseal.underlyingArgument.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 is " + x.unseal.showCode, x.unseal.pos)
22+
'{}
23+
}
24+
25+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
import Macros._
3+
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
val a: String = "abc"
7+
val b: 42 = 42
8+
def c: String = "abc"
9+
def d: 42 = 42
10+
11+
// fun(a) // ERROR
12+
// fun(b) // ERROR
13+
fun(c) // error
14+
fun(d) // error
15+
// fun("abc") // ERROR
16+
fun("abc": String) // error
17+
fun(s"abc${"def"}") // error
18+
19+
// fun2(a) // ERROR
20+
// fun2(b) // ERROR
21+
fun2(c) // error
22+
fun2(d) // error
23+
// fun2("abc") // ERROR
24+
fun2("abc": String) // error
25+
fun2(s"abc${"def"}") // error
26+
27+
// type T
28+
// type U = "abc"
29+
//
30+
// fun3[T] // ERROR
31+
// fun3[String] // ERROR
32+
// fun3["abc"] // ERROR
33+
// fun3[U] // ERROR
34+
}
35+
// FIXME all the lines marked as ERROR have the wrong position on the three of the argument.
36+
// they all have as source file this file but the span of `'x` in `fun` or `fun2`.
37+
// see #6026 and #6027
38+
}

0 commit comments

Comments
 (0)