Skip to content

Commit d0b02b7

Browse files
committed
Handle suspension due to macro call completed in arbitrary phases
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.
1 parent 9ec99c7 commit d0b02b7

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

compiler/src/dotty/tools/dotc/core/Phases.scala

+9-5
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,18 @@ object Phases {
378378
()
379379
else
380380
run
381-
catch case ex: Throwable if !ctx.run.enrichedErrorMessage =>
382-
println(ctx.run.enrichErrorMessage(s"unhandled exception while running $phaseName on $unit"))
383-
throw ex
381+
buf += unitCtx.compilationUnit
382+
catch
383+
case _: CompilationUnit.SuspendException => // this unit will be run again in `Run#compileSuspendedUnits`
384+
case ex: Throwable if !ctx.run.enrichedErrorMessage =>
385+
println(ctx.run.enrichErrorMessage(s"unhandled exception while running $phaseName on $unit"))
386+
throw ex
384387
finally ctx.run.advanceUnit()
385-
buf += unitCtx.compilationUnit
386388
end if
387389
end for
388-
buf.result()
390+
val res = buf.result()
391+
ctx.run.nn.checkSuspendedUnits(res)
392+
res
389393
end runOn
390394

391395
/** Convert a compilation unit's tree to a string; can be overridden */

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ class Inlining extends MacroTransform, IdentityDenotTransformer {
3636

3737
override def run(using Context): Unit =
3838
if ctx.compilationUnit.needsInlining || ctx.compilationUnit.hasMacroAnnotations then
39-
try super.run
40-
catch case _: CompilationUnit.SuspendException => ()
41-
42-
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
43-
val newUnits = super.runOn(units).filterNot(_.suspended)
44-
ctx.run.nn.checkSuspendedUnits(newUnits)
45-
newUnits
39+
super.run
4640

4741
override def checkPostCondition(tree: Tree)(using Context): Unit =
4842
tree match {

tests/pos-macros/i18517/Caller.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dummy
2+
3+
trait BG {
4+
val description: { type Structure }
5+
type Structure = description.Structure
6+
}
7+
8+
abstract class Caller extends BG {
9+
type Foo >: this.type <: this.type
10+
11+
transparent inline def generate2() =
12+
${Macro.impl() }
13+
14+
final val description = {
15+
generate2()
16+
}
17+
}

tests/pos-macros/i18517/Macro.scala

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dummy
2+
3+
import scala.quoted.*
4+
5+
object Macro:
6+
def impl()(using quotes:Quotes) : Expr[Any] =
7+
'{ null }

tests/pos-macros/i18517/User.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dummy
2+
3+
trait User:
4+
final def bar(cell:Any) : Unit =
5+
(cell: cell.type) match
6+
case c: (Caller & cell.type) => ()

0 commit comments

Comments
 (0)