Skip to content

Commit 3419e16

Browse files
committed
Handle binding of beta reduced inlined lambdas
Fixes scala#16374
1 parent 8849730 commit 3419e16

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ class InlineReducer(inliner: Inliner)(using Context):
161161
*/
162162
def betaReduce(tree: Tree)(using Context): Tree = tree match {
163163
case Apply(Select(cl @ closureDef(ddef), nme.apply), args) if defn.isFunctionType(cl.tpe) =>
164-
// closureDef also returns a result for closures wrapped in Inlined nodes.
165-
// These need to be preserved.
164+
val bindingsBuf = new DefBuffer
166165
def recur(cl: Tree): Tree = cl match
166+
// closureDef also returns a result for closures wrapped in Inlined nodes.
167+
// These need to be preserved.
167168
case Inlined(call, bindings, expr) =>
168169
cpy.Inlined(cl)(call, bindings, recur(expr))
169170
case _ => ddef.tpe.widen match
170171
case mt: MethodType if ddef.paramss.head.length == args.length =>
171-
val bindingsBuf = new DefBuffer
172172
val argSyms = mt.paramNames.lazyZip(mt.paramInfos).lazyZip(args).map { (name, paramtp, arg) =>
173173
arg.tpe.dealias match {
174174
case ref @ TermRef(NoPrefix, _) => ref.symbol
@@ -183,9 +183,10 @@ class InlineReducer(inliner: Inliner)(using Context):
183183
newOwners = ctx.owner :: Nil,
184184
substFrom = ddef.paramss.head.map(_.symbol),
185185
substTo = argSyms)
186-
Block(bindingsBuf.toList, expander.transform(ddef.rhs)).withSpan(tree.span)
186+
expander.transform(ddef.rhs)
187187
case _ => tree
188-
recur(cl)
188+
val reduced = recur(cl)
189+
Block(bindingsBuf.toList, reduced).withSpan(tree.span)
189190
case _ => tree
190191
}
191192

@@ -281,7 +282,7 @@ class InlineReducer(inliner: Inliner)(using Context):
281282
// Test case is pos-macros/i15971
282283
val tptBinds = getBinds(Set.empty[TypeSymbol], tpt)
283284
val binds: Set[TypeSymbol] = pat match {
284-
case UnApply(TypeApply(_, tpts), _, _) =>
285+
case UnApply(TypeApply(_, tpts), _, _) =>
285286
getBinds(Set.empty[TypeSymbol], tpts) ++ tptBinds
286287
case _ => tptBinds
287288
}

tests/pos/i16374a.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def method(using String): String = ???
2+
3+
inline def inlineMethod(inline op: String => Unit)(using String): Unit =
4+
println(op(method))
5+
6+
def test(using String) =
7+
inlineMethod(c => print(c))

tests/pos/i16374b.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def method(using String): String = ???
2+
3+
inline def identity[T](inline x: T): T = x
4+
5+
inline def inlineMethod(inline op: String => Unit)(using String): Unit =
6+
println(identity(op)(method))
7+
8+
def test(using String) =
9+
inlineMethod(c => print(c))

0 commit comments

Comments
 (0)