Skip to content

Commit d361e18

Browse files
committed
Factor out common macro suspend logic
1 parent b5e5154 commit d361e18

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

compiler/src/dotty/tools/dotc/quoted/Interpreter.scala

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,8 @@ class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context):
166166
(inst, inst.getClass)
167167
}
168168
catch
169-
case MissingClassDefinedInCurrentRun(sym) if ctx.compilationUnit.isSuspendable =>
170-
if (ctx.settings.XprintSuspension.value)
171-
report.echo(i"suspension triggered by a dependency on $sym", pos)
172-
ctx.compilationUnit.suspend() // this throws a SuspendException
169+
case MissingClassDefinedInCurrentRun(sym) =>
170+
suspendOnMissing(sym, pos)
173171

174172
val name = fn.name.asTermName
175173
val method = getMethod(clazz, name, paramsSig(fn))
@@ -214,23 +212,19 @@ class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context):
214212

215213
private def loadClass(name: String): Class[?] =
216214
try classLoader.loadClass(name)
217-
catch {
218-
case MissingClassDefinedInCurrentRun(sym) if ctx.compilationUnit.isSuspendable =>
219-
if (ctx.settings.XprintSuspension.value)
220-
report.echo(i"suspension triggered by a dependency on $sym", pos)
221-
ctx.compilationUnit.suspend() // this throws a SuspendException
222-
}
215+
catch
216+
case MissingClassDefinedInCurrentRun(sym) =>
217+
suspendOnMissing(sym, pos)
218+
223219

224220
private def getMethod(clazz: Class[?], name: Name, paramClasses: List[Class[?]]): JLRMethod =
225221
try clazz.getMethod(name.toString, paramClasses: _*)
226222
catch {
227223
case _: NoSuchMethodException =>
228224
val msg = em"Could not find method ${clazz.getCanonicalName}.$name with parameters ($paramClasses%, %)"
229225
throw new StopInterpretation(msg, pos)
230-
case MissingClassDefinedInCurrentRun(sym) if ctx.compilationUnit.isSuspendable =>
231-
if (ctx.settings.XprintSuspension.value)
232-
report.echo(i"suspension triggered by a dependency on $sym", pos)
233-
ctx.compilationUnit.suspend() // this throws a SuspendException
226+
case MissingClassDefinedInCurrentRun(sym) =>
227+
suspendOnMissing(sym, pos)
234228
}
235229

236230
private def stopIfRuntimeException[T](thunk: => T, method: JLRMethod): T =
@@ -248,10 +242,8 @@ class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context):
248242
ex.getTargetException match {
249243
case ex: scala.quoted.runtime.StopMacroExpansion =>
250244
throw ex
251-
case MissingClassDefinedInCurrentRun(sym) if ctx.compilationUnit.isSuspendable =>
252-
if (ctx.settings.XprintSuspension.value)
253-
report.echo(i"suspension triggered by a dependency on $sym", pos)
254-
ctx.compilationUnit.suspend() // this throws a SuspendException
245+
case MissingClassDefinedInCurrentRun(sym) =>
246+
suspendOnMissing(sym, pos)
255247
case targetException =>
256248
val sw = new StringWriter()
257249
sw.write("Exception occurred while executing macro expansion.\n")
@@ -364,3 +356,8 @@ object Interpreter:
364356
case _ => None
365357
}
366358
}
359+
360+
def suspendOnMissing(sym: Symbol, pos: SrcPos)(using Context): Nothing =
361+
if ctx.settings.XprintSuspension.value then
362+
report.echo(i"suspension triggered by a dependency on $sym", pos)
363+
ctx.compilationUnit.suspend() // this throws a SuspendException

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ class MacroAnnotations(thisPhase: DenotTransformer):
6868
if !ctx.reporter.hasErrors then
6969
report.error("Macro expansion was aborted by the macro without any errors reported. Macros should issue errors to end-users to facilitate debugging when aborting a macro expansion.", annot.tree)
7070
List(tree)
71-
case Interpreter.MissingClassDefinedInCurrentRun(sym) if ctx.compilationUnit.isSuspendable =>
72-
if (ctx.settings.XprintSuspension.value)
73-
report.echo(i"suspension triggered by a dependency on $sym", annot.tree)
74-
ctx.compilationUnit.suspend() // this throws a SuspendException
71+
case Interpreter.MissingClassDefinedInCurrentRun(sym) =>
72+
Interpreter.suspendOnMissing(sym, annot.tree)
7573
case NonFatal(ex) =>
7674
val stack0 = ex.getStackTrace.takeWhile(_.getClassName != "dotty.tools.dotc.transform.MacroAnnotations")
7775
val stack = stack0.take(1 + stack0.lastIndexWhere(_.getMethodName == "transform"))

0 commit comments

Comments
 (0)