Skip to content

Commit 3c18715

Browse files
committed
Outline macro evaluation exception handling
1 parent aadd17f commit 3c18715

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,21 +36,10 @@ 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

@@ -87,6 +76,24 @@ object Splicer {
8776
liftArgs(call.symbol.info, allArgs(call, Nil))
8877
}
8978

79+
/* Evaluate the code in the macro and handle exceptions durring evaluation */
80+
private def evaluateMacro(pos: Position)(code: => Tree)(implicit ctx: Context): Tree = {
81+
try code
82+
catch {
83+
case ex: scala.quoted.QuoteError =>
84+
ctx.error(ex.getMessage, pos)
85+
EmptyTree
86+
case NonFatal(ex) =>
87+
val msg =
88+
s"""Failed to evaluate inlined quote.
89+
| Caused by ${ex.getClass}: ${if (ex.getMessage == null) "" else ex.getMessage}
90+
| ${ex.getStackTrace.takeWhile(_.getClassName != "dotty.tools.dotc.transform.Splicer$").init.mkString("\n ")}
91+
""".stripMargin
92+
ctx.error(msg, pos)
93+
EmptyTree
94+
}
95+
}
96+
9097
/** Tree interpreter that can interpret calls to static methods with it's default arguments
9198
*
9299
* The interpreter assumes that all calls in the trees are to code that was

0 commit comments

Comments
 (0)