Skip to content

Commit 8dd7187

Browse files
authored
Allow macro annotations to recover from suspension (#21969)
This changes makes the execution of the test case in #20353 stable across different run
2 parents d86f44d + 16c4a82 commit 8dd7187

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

Diff for: compiler/src/dotty/tools/dotc/inlines/Inliner.scala

+1-3
Original file line numberDiff line numberDiff line change
@@ -1080,9 +1080,7 @@ class Inliner(val call: tpd.Tree)(using Context):
10801080
hints.nn += i"suspension triggered by macro call to ${sym.showLocated} in ${sym.associatedFile}"
10811081
if suspendable then
10821082
if ctx.settings.YnoSuspendedUnits.value then
1083-
return ref(defn.Predef_undefined)
1084-
.withType(ErrorType(em"could not expand macro, suspended units are disabled by -Yno-suspended-units"))
1085-
.withSpan(splicePos.span)
1083+
return errorTree(ref(defn.Predef_undefined), em"could not expand macro, suspended units are disabled by -Yno-suspended-units", splicePos)
10861084
else
10871085
ctx.compilationUnit.suspend(hints.nn.toList.mkString(", ")) // this throws a SuspendException
10881086

Diff for: compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ object MacroAnnotations:
127127
// Replace this case with the nested cases.
128128
case ex0: InvocationTargetException =>
129129
ex0.getCause match
130+
case ex: CompilationUnit.SuspendException =>
131+
throw ex
130132
case ex: scala.quoted.runtime.StopMacroExpansion =>
131133
if !ctx.reporter.hasErrors then
132134
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)
@@ -137,7 +139,7 @@ object MacroAnnotations:
137139
val stack0 = ex.getStackTrace.takeWhile(_.getClassName != this.getClass().getName())
138140
val stack = stack0.take(1 + stack0.lastIndexWhere(_.getMethodName == "transform"))
139141
val msg =
140-
em"""Failed to evaluate macro.
142+
em"""Failed to evaluate macro annotation '$annot'.
141143
| Caused by ${ex.getClass}: ${if (ex.getMessage == null) "" else ex.getMessage}
142144
| ${stack.mkString("\n ")}
143145
|"""

Diff for: tests/neg-macros/annot-crash.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- Error: tests/neg-macros/annot-crash/Test_2.scala:1:0 ----------------------------------------------------------------
33
1 |@crash // error
44
|^^^^^^
5-
|Failed to evaluate macro.
5+
|Failed to evaluate macro annotation '@crash'.
66
| Caused by class scala.NotImplementedError: an implementation is missing
77
| scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
88
| crash.transform(Macro_1.scala:7)

0 commit comments

Comments
 (0)