Skip to content

Handle QuotedError when evaluating holes in quotes #4479

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
merged 2 commits into from
May 18, 2018
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
19 changes: 12 additions & 7 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ object Splicer {
val liftedArgs = getLiftedArgs(call, bindings)
val interpreter = new Interpreter(pos, classLoader)
val interpreted = interpreter.interpretCallToSymbol[Seq[Any] => Object](call.symbol)
interpreted.flatMap(lambda => evaluateLambda(lambda, liftedArgs, pos)).fold(tree)(PickledQuotes.quotedExprToTree)
evaluateMacro(pos) {
// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
val evaluated = interpreted.map(lambda => lambda(liftedArgs).asInstanceOf[scala.quoted.Expr[Nothing]])
evaluated.fold(tree)(PickledQuotes.quotedExprToTree)
}
}

/** Given the inline code and bindings, compute the lifted arguments that will be used to execute the macro
Expand Down Expand Up @@ -72,20 +76,21 @@ object Splicer {
liftArgs(call.symbol.info, allArgs(call, Nil))
}

private def evaluateLambda(lambda: Seq[Any] => Object, args: Seq[Any], pos: Position)(implicit ctx: Context): Option[scala.quoted.Expr[Nothing]] = {
try Some(lambda(args).asInstanceOf[scala.quoted.Expr[Nothing]])
/* Evaluate the code in the macro and handle exceptions durring evaluation */
private def evaluateMacro(pos: Position)(code: => Tree)(implicit ctx: Context): Tree = {
try code
catch {
case ex: scala.quoted.QuoteError =>
ctx.error(ex.getMessage, pos)
None
EmptyTree
case NonFatal(ex) =>
val msg =
s"""Failed to evaluate inlined quote.
| Caused by: ${ex.getMessage}
| ${ex.getStackTrace.takeWhile(_.getClassName != "dotty.tools.dotc.transform.Splicer$").init.mkString("\n ")}
| Caused by ${ex.getClass}: ${if (ex.getMessage == null) "" else ex.getMessage}
| ${ex.getStackTrace.takeWhile(_.getClassName != "dotty.tools.dotc.transform.Splicer$").init.mkString("\n ")}
""".stripMargin
ctx.error(msg, pos)
None
EmptyTree
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/neg/quote-error-2/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import quoted._

object Macro_1 {
inline def foo(inline b: Boolean): Unit = ~fooImpl(b)
def fooImpl(b: Boolean): Expr[Unit] =
'(println(~msg(b)))

def msg(b: Boolean): Expr[String] =
if (b) '("foo(true)")
else QuoteError("foo cannot be called with false")

}
6 changes: 6 additions & 0 deletions tests/neg/quote-error-2/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
}