Skip to content

Commit 6e0f59a

Browse files
committed
Add implicit reporter for inline quotes
1 parent 0628792 commit 6e0f59a

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,10 @@ class Definitions {
635635
lazy val QuotedType_applyR = QuotedTypeModule.requiredMethodRef(nme.apply)
636636
def QuotedType_apply(implicit ctx: Context) = QuotedType_applyR.symbol
637637

638+
lazy val QuotedReporterModule = ctx.requiredModule("scala.quoted.Reporter")
639+
lazy val QuotedReporterDummyR = QuotedReporterModule.requiredMethod("dummy".toTermName)
640+
def QuotedReporterDummy(implicit ctx: Context) = QuotedReporterDummyR.symbol
641+
638642
def Unpickler_unpickleExpr = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleExpr")
639643
def Unpickler_unpickleType = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleType")
640644

compiler/src/dotty/tools/dotc/interpreter/Interpreter.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ class Interpreter(implicit ctx: Context) {
7272
if (quotedTree.isTerm) new scala.quoted.Exprs.TreeExpr(quotedTree)
7373
else new scala.quoted.Types.TreeType(quotedTree)
7474

75+
case tree: RefTree if tree.symbol eq defn.QuotedReporterDummy =>
76+
new scala.quoted.Reporter {
77+
override def error(message: String): Unit = ctx.error(message, pos)
78+
override def warn(message: String): Unit = ctx.warning(message, pos)
79+
}
80+
7581
case Literal(Constant(c)) => c.asInstanceOf[Object]
7682

7783
case Apply(fn, args) if fn.symbol.isConstructor =>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package scala.quoted
2+
3+
trait Reporter {
4+
def error(message: String): Unit
5+
def warn(message: String): Unit
6+
}
7+
8+
object Reporter {
9+
object DummyReporter extends Reporter {
10+
override def error(message: String): Unit = ()
11+
override def warn(message: String): Unit = ()
12+
}
13+
implicit def dummy: Reporter = DummyReporter
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import quoted._
2+
3+
object Macro_1 {
4+
inline def foo(inline b: Boolean): Unit = ~fooImpl(b)
5+
def fooImpl(b: Boolean)(implicit reporter: Reporter): Expr[Unit] =
6+
if (b) '(println("foo(true)"))
7+
else reporter.error("foo cannot be called with false")
8+
}

tests/neg/quote-reporter/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Macro_1._
2+
3+
object Test_2 {
4+
foo(true)
5+
foo(false) // error: foo cannot be called with false
6+
}

0 commit comments

Comments
 (0)