Skip to content

Commit 1a84513

Browse files
committed
Prepare bodies of inline forwarders eagerly
Fixes #14131 Fixes #16469
1 parent 43d0ec4 commit 1a84513

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,13 +1227,21 @@ class Namer { typer: Typer =>
12271227
case pt: MethodOrPoly => 1 + extensionParamsCount(pt.resType)
12281228
case _ => 0
12291229
val ddef = tpd.DefDef(forwarder.asTerm, prefss => {
1230+
val forwarderCtx = ctx.withOwner(forwarder)
12301231
val (pathRefss, methRefss) = prefss.splitAt(extensionParamsCount(path.tpe.widen))
12311232
val ref = path.appliedToArgss(pathRefss).select(sym.asTerm)
1232-
ref.appliedToArgss(adaptForwarderParams(Nil, sym.info, methRefss))
1233-
.etaExpandCFT(using ctx.withOwner(forwarder))
1233+
val rhs = ref.appliedToArgss(adaptForwarderParams(Nil, sym.info, methRefss))
1234+
.etaExpandCFT(using forwarderCtx)
1235+
if forwarder.isInlineMethod then
1236+
// Eagerly make the body inlineable. `registerInlineInfo` does this lazily
1237+
// but it does not get evaluated during typer as the forwarder we are creating
1238+
// is already typed.
1239+
val inlinableRhs = PrepareInlineable.makeInlineable(rhs)(using forwarderCtx)
1240+
PrepareInlineable.registerInlineInfo(forwarder, inlinableRhs)(using forwarderCtx)
1241+
inlinableRhs
1242+
else
1243+
rhs
12341244
})
1235-
if forwarder.isInlineMethod then
1236-
PrepareInlineable.registerInlineInfo(forwarder, ddef.rhs)
12371245
buf += ddef.withSpan(span)
12381246
if hasDefaults then
12391247
foreachDefaultGetterOf(sym.asTerm,

tests/pos-macros/i14131.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Dog:
2+
inline given bark(using msg: String = "Woof!"): String = s"bark: $msg"
3+
4+
class Wolf:
5+
private val dog: Dog = Dog()
6+
export dog.given
7+
8+
def test =
9+
val w = Wolf()
10+
import w.given
11+
summon[String]

tests/pos/i16469.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Context {
2+
def normalMethod(): String = "normal"
3+
inline def inlineMethod(): String = "inline"
4+
}
5+
6+
class Script(ctx: Context) {
7+
export ctx.*
8+
normalMethod()
9+
inlineMethod()
10+
}
11+
12+
class MyScript(context: Context) extends Script(context) {
13+
normalMethod()
14+
inlineMethod()
15+
}

0 commit comments

Comments
 (0)