Skip to content

Commit 14f3793

Browse files
committed
Handle leading given parameters in inline unapplies
Fixes #12991
1 parent e587a81 commit 14f3793

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ object Inlines:
181181
// as its right hand side. The call to the wrapper unapply serves as the signpost for pattern matching.
182182
// After pattern matching, the anonymous class is removed in phase InlinePatterns with a beta reduction step.
183183
//
184-
// An inline unapply `P.unapply` in a plattern `P(x1,x2,...)` is transformed into
185-
// `{ class $anon { def unapply(t0: T0)(using t1: T1, t2: T2, ...): R = P.unapply(t0)(using t1, t2, ...) }; new $anon }.unapply`
186-
// and the call `P.unapply(x1, x2, ...)` is inlined.
184+
// An inline unapply `P.unapply` in a pattern `P(using y1,y2,...)(x1,x2,...)` is transformed into
185+
// `{ class $anon { def unapply(using l1: L1, l2: L2, ...)(s: S)(using t1: T1, t2: T2, ...): R = P.unapply(using l1, l2, ...)(s)(using t1, t2, ...) }; new $anon }.unapply(using y1,y2,...)`
186+
// and the call `P.unapply(using l1, l2,...)(x1, x2, ...)(using t1, t2, ...)` is inlined.
187187
// This serves as a placeholder for the inlined body until the `patternMatcher` phase. After pattern matcher
188188
// transforms the patterns into terms, the `inlinePatterns` phase removes this anonymous class by β-reducing
189189
// the call to the `unapply`.
@@ -193,9 +193,6 @@ object Inlines:
193193
case Apply(SplitFunAndGivenArgs(fn, argss), args) => (fn, argss :+ args)
194194
case _ => (tree, Nil)
195195
val UnApply(SplitFunAndGivenArgs(fun, leadingImplicits), trailingImplicits, patterns) = unapp
196-
if leadingImplicits.flatten.nonEmpty then
197-
// To support them see https://github.com/lampepfl/dotty/pull/13158
198-
report.error("inline unapply methods with given parameters before the scrutinee are not supported", fun)
199196

200197
val sym = unapp.symbol
201198

@@ -217,7 +214,7 @@ object Inlines:
217214
List(unapply)
218215
}
219216

220-
val newFun = newUnapply.select(unapplySym1).withSpan(unapp.span)
217+
val newFun = newUnapply.select(unapplySym1).appliedToArgss(leadingImplicits).withSpan(unapp.span)
221218
cpy.UnApply(unapp)(newFun, trailingImplicits, patterns)
222219
end inlinedUnapply
223220

tests/pos/i12991.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Foo:
2+
inline def unapply(using String)(i: Int): Some[Int] = Some(i)
3+
4+
object Bar:
5+
inline def unapply(using String)(using String)(i: Int): Some[Int] = Some(i)
6+
7+
object Baz:
8+
inline def unapply[T](using String)(i: T): Some[T] = Some(i)
9+
10+
given String = ""
11+
12+
val i = 10 match
13+
case Foo(x) => x
14+
case Bar(x) => x
15+
case Baz(x) => x

0 commit comments

Comments
 (0)