Skip to content

Commit 98215d3

Browse files
Refactor MacroAnnotations.callMacro (#18581)
Move error handling into call macro This method should handle the reflective calls and their errors.
2 parents 7dc9798 + 4c3502d commit 98215d3

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

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

+25-26
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,7 @@ class MacroAnnotations:
5353
debug.println(i"Expanding macro annotation: ${annot}")
5454

5555
// Interpret call to `new myAnnot(..).transform(using <Quotes>)(<tree>)`
56-
val transformedTrees =
57-
try callMacro(macroInterpreter, tree, annot)
58-
catch
59-
// TODO: Replace this case when scala.annaotaion.MacroAnnotation is no longer experimental and reflectiveSelectable is not used
60-
// Replace this case with the nested cases.
61-
case ex0: InvocationTargetException =>
62-
ex0.getCause match
63-
case ex: scala.quoted.runtime.StopMacroExpansion =>
64-
if !ctx.reporter.hasErrors then
65-
report.error("Macro expansion was aborted by the macro without any errors reported. Macros should issue errors to end-users when aborting a macro expansion with StopMacroExpansion.", annot.tree)
66-
List(tree)
67-
case Interpreter.MissingClassDefinedInCurrentRun(sym) =>
68-
Interpreter.suspendOnMissing(sym, annot.tree)
69-
case NonFatal(ex) =>
70-
val stack0 = ex.getStackTrace.takeWhile(_.getClassName != "dotty.tools.dotc.transform.MacroAnnotations")
71-
val stack = stack0.take(1 + stack0.lastIndexWhere(_.getMethodName == "transform"))
72-
val msg =
73-
em"""Failed to evaluate macro.
74-
| Caused by ${ex.getClass}: ${if (ex.getMessage == null) "" else ex.getMessage}
75-
| ${stack.mkString("\n ")}
76-
|"""
77-
report.error(msg, annot.tree)
78-
List(tree)
79-
case _ =>
80-
throw ex0
56+
val transformedTrees = callMacro(macroInterpreter, tree, annot)
8157
transformedTrees.span(_.symbol != tree.symbol) match
8258
case (prefixed, newTree :: suffixed) =>
8359
allTrees ++= prefixed
@@ -117,7 +93,30 @@ class MacroAnnotations:
11793
assert(annotInstance.getClass.getClassLoader.loadClass("scala.annotation.MacroAnnotation").isInstance(annotInstance))
11894

11995
val quotes = QuotesImpl()(using SpliceScope.contextWithNewSpliceScope(tree.symbol.sourcePos)(using MacroExpansion.context(tree)).withOwner(tree.symbol.owner))
120-
annotInstance.transform(using quotes)(tree.asInstanceOf[quotes.reflect.Definition])
96+
try annotInstance.transform(using quotes)(tree.asInstanceOf[quotes.reflect.Definition])
97+
catch
98+
// TODO: Replace this case when scala.annaotaion.MacroAnnotation is no longer experimental and reflectiveSelectable is not used
99+
// Replace this case with the nested cases.
100+
case ex0: InvocationTargetException =>
101+
ex0.getCause match
102+
case ex: scala.quoted.runtime.StopMacroExpansion =>
103+
if !ctx.reporter.hasErrors then
104+
report.error("Macro expansion was aborted by the macro without any errors reported. Macros should issue errors to end-users when aborting a macro expansion with StopMacroExpansion.", annot.tree)
105+
List(tree)
106+
case Interpreter.MissingClassDefinedInCurrentRun(sym) =>
107+
Interpreter.suspendOnMissing(sym, annot.tree)
108+
case NonFatal(ex) =>
109+
val stack0 = ex.getStackTrace.takeWhile(_.getClassName != "dotty.tools.dotc.transform.MacroAnnotations")
110+
val stack = stack0.take(1 + stack0.lastIndexWhere(_.getMethodName == "transform"))
111+
val msg =
112+
em"""Failed to evaluate macro.
113+
| Caused by ${ex.getClass}: ${if (ex.getMessage == null) "" else ex.getMessage}
114+
| ${stack.mkString("\n ")}
115+
|"""
116+
report.error(msg, annot.tree)
117+
List(tree)
118+
case _ =>
119+
throw ex0
121120

122121
/** Check that this tree can be added by the macro annotation */
123122
private def checkMacroDef(newTree: DefTree, annotatedTree: Tree, annot: Annotation)(using Context) =

0 commit comments

Comments
 (0)