Skip to content
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
}

val tree1 =
if (level == 0) cpy.Inlined(tree)(call, stagedBindings, Splicer.splice(seq(splicedBindings, body)))
if (level == 0) cpy.Inlined(tree)(call, stagedBindings, Splicer.splice(seq(splicedBindings, body).withPos(tree.pos)))
else seq(stagedBindings, cpy.Select(expansion)(cpy.Inlined(tree)(call, splicedBindings, body), name))
val tree2 = transform(tree1)

Expand Down
22 changes: 21 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package transform

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.interpreter._

import scala.util.control.NonFatal

import java.lang.reflect.InvocationTargetException

/** Utility class to splice quoted expressions */
object Splicer {
import tpd._
Expand All @@ -22,7 +27,22 @@ object Splicer {
/** Splice the Tree for a Quoted expression which is constructed via a reflective call to the given method */
private def reflectiveSplice(tree: Tree)(implicit ctx: Context): Tree = {
val interpreter = new Interpreter
interpreter.interpretTree[scala.quoted.Expr[_]](tree).map(PickledQuotes.quotedExprToTree).getOrElse(tree)
val interpreted =
try interpreter.interpretTree[scala.quoted.Expr[_]](tree)
catch { case ex: InvocationTargetException => handleTargetException(tree, ex); None }
interpreted.fold(tree)(PickledQuotes.quotedExprToTree)
}

private def handleTargetException(tree: Tree, ex: InvocationTargetException)(implicit ctx: Context): Unit = ex.getCause match {
case ex: scala.quoted.QuoteError => ctx.error(ex.getMessage, tree.pos)
case NonFatal(ex) =>
val msg =
s"""Failed to evaluate inlined quote.
| Caused by: ${ex.getMessage}
| ${ex.getStackTrace.takeWhile(_.getClassName != "sun.reflect.NativeMethodAccessorImpl").mkString("\n ")}
""".stripMargin
ctx.error(msg, tree.pos)
case _ => throw ex
}

}
11 changes: 11 additions & 0 deletions library/src/scala/quoted/QuoteError.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package scala.quoted

/** Throwing this error in the implementation of an inline macro
* will result in a compilation error with the given message.
*/
class QuoteError(message: String) extends Throwable(message)

object QuoteError {
/** Throws a QuoteError with the given message */
def apply(message: => String): Nothing = throw new QuoteError(message)
}
8 changes: 8 additions & 0 deletions tests/neg/quote-error/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import quoted._

object Macro_1 {
inline def foo(inline b: Boolean): Unit = ~fooImpl(b)
def fooImpl(b: Boolean): Expr[Unit] =
if (b) '(println("foo(true)"))
else QuoteError("foo cannot be called with false")
}
6 changes: 6 additions & 0 deletions tests/neg/quote-error/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Macro_1._

object Test_2 {
foo(true)
foo(false) // error: foo cannot be called with false
}
8 changes: 8 additions & 0 deletions tests/neg/quote-exception/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import quoted._

object Macro_1 {
inline def foo(inline b: Boolean): Unit = ~fooImpl(b)
def fooImpl(b: Boolean): Expr[Unit] =
if (b) '(println("foo(true)"))
else ???
}
6 changes: 6 additions & 0 deletions tests/neg/quote-exception/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Macro_1._

object Test_2 {
foo(true)
foo(false) // error: Failed to evaluate inlined quote. Caused by: an implementation is missing
}