Skip to content

Commit c6d7d89

Browse files
committed
Refactor bridge forwarders (2)
1 parent a129eac commit c6d7d89

File tree

1 file changed

+30
-41
lines changed

1 file changed

+30
-41
lines changed

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

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -125,47 +125,36 @@ class Bridges(root: ClassSymbol, thisPhase: DenotTransformer)(using Context) {
125125
* not yet instantiated.
126126
*/
127127
def etaExpand(ref: Tree, args: List[Tree])(using Context): Tree =
128-
val toAbstract: List[TermSymbol] =
129-
def anonFuns(tp: Type, n: Int, owner: Symbol): List[TermSymbol] =
130-
if n <= 0 then Nil
131-
else
132-
val defn.ContextFunctionType(argTpes, resTpe, isErased) = tp: @unchecked
133-
val anonFun = newAnonFun(
134-
owner,
135-
MethodType(if isErased then Nil else argTpes, resTpe),
136-
coord = owner.coord)
137-
anonFun :: anonFuns(resTpe, n - 1, anonFun)
138-
val otherCount = contextResultCount(other)
139-
val resType = contextFunctionResultTypeAfter(member, otherCount)(using preErasureCtx)
140-
anonFuns(resType, memberCount - otherCount, bridge)
141-
142-
def expand(args: List[Tree], anonFuns: List[TermSymbol], owner: Symbol): Tree =
143-
anonFuns match
144-
case Nil =>
145-
val app = untpd.cpy.Apply(ref)(ref, args)
146-
assert(ctx.typer.isInstanceOf[Erasure.Typer])
147-
ctx.typer.typed(app, member.info.finalResultType)
148-
case anonFun :: anonFuns1 =>
149-
val origType = anonFun.info
150-
anonFun.info = transformInfo(anonFun, anonFun.info)
151-
inContext(ctx.withOwner(owner)) {
152-
def lambdaBody(refss: List[List[Tree]]) =
153-
val refs :: Nil = refss: @unchecked
154-
val expandedRefs = refs.map(_.withSpan(owner.span.endPos)) match
155-
case (bunchedParam @ Ident(nme.ALLARGS)) :: Nil =>
156-
origType.firstParamTypes.indices.toList.map(n =>
157-
bunchedParam
158-
.select(nme.primitive.arrayApply)
159-
.appliedTo(Literal(Constant(n))))
160-
case refs1 => refs1
161-
expand(args ::: expandedRefs, anonFuns1, anonFun)
162-
163-
val unadapted = Closure(anonFun, lambdaBody)
164-
cpy.Block(unadapted)(unadapted.stats,
165-
adaptClosure(unadapted.expr.asInstanceOf[Closure]))
166-
}
167-
168-
expand(args, toAbstract, bridge)
128+
def expand(args: List[Tree], tp: Type, n: Int)(using Context): Tree =
129+
if n <= 0 then
130+
assert(ctx.typer.isInstanceOf[Erasure.Typer])
131+
ctx.typer.typed(untpd.cpy.Apply(ref)(ref, args), member.info.finalResultType)
132+
else
133+
val defn.ContextFunctionType(argTypes, resType, isErased) = tp: @unchecked
134+
val anonFun = newAnonFun(ctx.owner,
135+
MethodType(if isErased then Nil else argTypes, resType),
136+
coord = ctx.owner.coord)
137+
anonFun.info = transformInfo(anonFun, anonFun.info)
138+
139+
def lambdaBody(refss: List[List[Tree]]) =
140+
val refs :: Nil = refss: @unchecked
141+
val expandedRefs = refs.map(_.withSpan(ctx.owner.span.endPos)) match
142+
case (bunchedParam @ Ident(nme.ALLARGS)) :: Nil =>
143+
argTypes.indices.toList.map(n =>
144+
bunchedParam
145+
.select(nme.primitive.arrayApply)
146+
.appliedTo(Literal(Constant(n))))
147+
case refs1 => refs1
148+
expand(args ::: expandedRefs, resType, n - 1)(using ctx.withOwner(anonFun))
149+
150+
val unadapted = Closure(anonFun, lambdaBody)
151+
cpy.Block(unadapted)(unadapted.stats,
152+
adaptClosure(unadapted.expr.asInstanceOf[Closure]))
153+
end expand
154+
155+
val otherCount = contextResultCount(other)
156+
val start = contextFunctionResultTypeAfter(member, otherCount)(using preErasureCtx)
157+
expand(args, start, memberCount - otherCount)(using ctx.withOwner(bridge))
169158
end etaExpand
170159

171160
def bridgeRhs(argss: List[List[Tree]]) =

0 commit comments

Comments
 (0)