Skip to content

Commit

Permalink
Handle suspension due to macro call completed in arbitrary phases
Browse files Browse the repository at this point in the history
Previously we only supported suspension in Typer and Inliner. In the added test
case this happens in PostTyper, but I've seen it happen in Mixin too.

Fixes #18517.

[Cherry-picked bac8781][modified]
  • Loading branch information
WojciechMazur committed Dec 9, 2024
1 parent 848820b commit ef10468
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
17 changes: 11 additions & 6 deletions compiler/src/dotty/tools/dotc/core/Phases.scala
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,20 @@ object Phases {
for unit <- units do
given unitCtx: Context = runCtx.fresh.setPhase(this.start).setCompilationUnit(unit).withRootImports
if ctx.run.enterUnit(unit) then
try run
catch case ex: Throwable if !ctx.run.enrichedErrorMessage =>
println(ctx.run.enrichErrorMessage(s"unhandled exception while running $phaseName on $unit"))
throw ex
try
run
buf += unitCtx.compilationUnit
catch
case _: CompilationUnit.SuspendException => // this unit will be run again in `Run#compileSuspendedUnits`
case ex: Throwable if !ctx.run.enrichedErrorMessage =>
println(ctx.run.enrichErrorMessage(s"unhandled exception while running $phaseName on $unit"))
throw ex
finally ctx.run.advanceUnit()
buf += unitCtx.compilationUnit
end if
end for
buf.result()
val res = buf.result()
ctx.run.nn.checkSuspendedUnits(res)
res
end runOn

/** Convert a compilation unit's tree to a string; can be overridden */
Expand Down
8 changes: 1 addition & 7 deletions compiler/src/dotty/tools/dotc/transform/Inlining.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ class Inlining extends MacroTransform {

override def run(using Context): Unit =
if ctx.compilationUnit.needsInlining || ctx.compilationUnit.hasMacroAnnotations then
try super.run
catch case _: CompilationUnit.SuspendException => ()

override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
val newUnits = super.runOn(units).filterNot(_.suspended)
ctx.run.nn.checkSuspendedUnits(newUnits)
newUnits
super.run

override def checkPostCondition(tree: Tree)(using Context): Unit =
tree match {
Expand Down
17 changes: 17 additions & 0 deletions tests/pos-macros/i18517/Caller.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dummy

trait BG {
val description: { type Structure }
type Structure = description.Structure
}

abstract class Caller extends BG {
type Foo >: this.type <: this.type

transparent inline def generate2() =
${Macro.impl() }

final val description = {
generate2()
}
}
7 changes: 7 additions & 0 deletions tests/pos-macros/i18517/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dummy

import scala.quoted.*

object Macro:
def impl()(using quotes:Quotes) : Expr[Any] =
'{ null }
6 changes: 6 additions & 0 deletions tests/pos-macros/i18517/User.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dummy

trait User:
final def bar(cell:Any) : Unit =
(cell: cell.type) match
case c: (Caller & cell.type) => ()

0 comments on commit ef10468

Please sign in to comment.