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 1 commit
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
34 changes: 16 additions & 18 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,22 @@ 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)
try {
val evaluated = interpreted.map(lambda => lambda(liftedArgs).asInstanceOf[scala.quoted.Expr[Nothing]])
evaluated.fold(tree)(PickledQuotes.quotedExprToTree)
} catch {
case ex: scala.quoted.QuoteError =>
ctx.error(ex.getMessage, pos)
EmptyTree
case NonFatal(ex) =>
val msg =
s"""Failed to evaluate inlined quote.
| 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)
EmptyTree
}
}

/** Given the inline code and bindings, compute the lifted arguments that will be used to execute the macro
Expand Down Expand Up @@ -72,23 +87,6 @@ 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]])
catch {
case ex: scala.quoted.QuoteError =>
ctx.error(ex.getMessage, pos)
None
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 ")}
""".stripMargin
ctx.error(msg, pos)
None
}
}

/** Tree interpreter that can interpret calls to static methods with it's default arguments
*
* The interpreter assumes that all calls in the trees are to code that was
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
}