@@ -3,9 +3,14 @@ package transform
33
44import dotty .tools .dotc .ast .tpd
55import dotty .tools .dotc .core .Contexts ._
6+ import dotty .tools .dotc .core .Decorators ._
67import dotty .tools .dotc .core .quoted ._
78import dotty .tools .dotc .interpreter ._
89
10+ import scala .util .control .NonFatal
11+
12+ import java .lang .reflect .InvocationTargetException
13+
914/** Utility class to splice quoted expressions */
1015object Splicer {
1116 import tpd ._
@@ -22,7 +27,22 @@ object Splicer {
2227 /** Splice the Tree for a Quoted expression which is constructed via a reflective call to the given method */
2328 private def reflectiveSplice (tree : Tree )(implicit ctx : Context ): Tree = {
2429 val interpreter = new Interpreter
25- interpreter.interpretTree[scala.quoted.Expr [_]](tree).map(PickledQuotes .quotedExprToTree).getOrElse(tree)
30+ val interpreted =
31+ try interpreter.interpretTree[scala.quoted.Expr [_]](tree)
32+ catch { case ex : InvocationTargetException => handleTargetException(tree, ex); None }
33+ interpreted.fold(tree)(PickledQuotes .quotedExprToTree)
34+ }
35+
36+ private def handleTargetException (tree : Tree , ex : InvocationTargetException )(implicit ctx : Context ): Unit = ex.getCause match {
37+ case ex : scala.quoted.QuoteError => ctx.error(ex.getMessage, tree.pos)
38+ case NonFatal (ex) =>
39+ val msg =
40+ s """ Failed to evaluate inlined quote.
41+ | Caused by: ${ex.getMessage}
42+ | ${ex.getStackTrace.takeWhile(_.getClassName != " sun.reflect.NativeMethodAccessorImpl" ).mkString(" \n " )}
43+ """ .stripMargin
44+ ctx.error(msg, tree.pos)
45+ case _ => throw ex
2646 }
2747
2848}
0 commit comments