Skip to content

Commit 9e67417

Browse files
committed
Outline macro evaluation exception handling
1 parent aadd17f commit 9e67417

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,31 @@ object Splicer {
3636
val liftedArgs = getLiftedArgs(call, bindings)
3737
val interpreter = new Interpreter(pos, classLoader)
3838
val interpreted = interpreter.interpretCallToSymbol[Seq[Any] => Object](call.symbol)
39-
try {
39+
evaluateMacro(pos) {
40+
// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
4041
val evaluated = interpreted.map(lambda => lambda(liftedArgs).asInstanceOf[scala.quoted.Expr[Nothing]])
4142
evaluated.fold(tree)(PickledQuotes.quotedExprToTree)
42-
} catch {
43-
case ex: scala.quoted.QuoteError =>
44-
ctx.error(ex.getMessage, pos)
45-
EmptyTree
46-
case NonFatal(ex) =>
47-
val msg =
48-
s"""Failed to evaluate inlined quote.
49-
| Caused by ${ex.getClass}: ${if (ex.getMessage == null) "" else ex.getMessage}
50-
| ${ex.getStackTrace.takeWhile(_.getClassName != "dotty.tools.dotc.transform.Splicer$").init.mkString("\n ")}
51-
""".stripMargin
52-
ctx.error(msg, pos)
53-
EmptyTree
5443
}
5544
}
5645

46+
/* Evaluate the code in the macro and handle exceptions durring evaluation */
47+
private def evaluateMacro(pos: Position)(code: => Tree)(implicit ctx: Context): Tree = {
48+
try code
49+
catch {
50+
case ex: scala.quoted.QuoteError =>
51+
ctx.error(ex.getMessage, pos)
52+
EmptyTree
53+
case NonFatal(ex) =>
54+
val msg =
55+
s"""Failed to evaluate inlined quote.
56+
| Caused by ${ex.getClass}: ${if (ex.getMessage == null) "" else ex.getMessage}
57+
| ${ex.getStackTrace.takeWhile(_.getClassName != "dotty.tools.dotc.transform.Splicer$").init.mkString("\n ")}
58+
""".stripMargin
59+
ctx.error(msg, pos)
60+
EmptyTree
61+
}
62+
}
63+
5764
/** Given the inline code and bindings, compute the lifted arguments that will be used to execute the macro
5865
* - Type parameters are lifted to quoted.Types.TreeType
5966
* - Inline parameters are listed as their value

0 commit comments

Comments
 (0)