Skip to content

Commit

Permalink
Handle leading given parameters in inline unapplies
Browse files Browse the repository at this point in the history
Fixes #12991
  • Loading branch information
nicolasstucki committed Nov 17, 2022
1 parent e587a81 commit 14f3793
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
11 changes: 4 additions & 7 deletions compiler/src/dotty/tools/dotc/inlines/Inlines.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ object Inlines:
// as its right hand side. The call to the wrapper unapply serves as the signpost for pattern matching.
// After pattern matching, the anonymous class is removed in phase InlinePatterns with a beta reduction step.
//
// An inline unapply `P.unapply` in a plattern `P(x1,x2,...)` is transformed into
// `{ class $anon { def unapply(t0: T0)(using t1: T1, t2: T2, ...): R = P.unapply(t0)(using t1, t2, ...) }; new $anon }.unapply`
// and the call `P.unapply(x1, x2, ...)` is inlined.
// An inline unapply `P.unapply` in a pattern `P(using y1,y2,...)(x1,x2,...)` is transformed into
// `{ 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,...)`
// and the call `P.unapply(using l1, l2,...)(x1, x2, ...)(using t1, t2, ...)` is inlined.
// This serves as a placeholder for the inlined body until the `patternMatcher` phase. After pattern matcher
// transforms the patterns into terms, the `inlinePatterns` phase removes this anonymous class by β-reducing
// the call to the `unapply`.
Expand All @@ -193,9 +193,6 @@ object Inlines:
case Apply(SplitFunAndGivenArgs(fn, argss), args) => (fn, argss :+ args)
case _ => (tree, Nil)
val UnApply(SplitFunAndGivenArgs(fun, leadingImplicits), trailingImplicits, patterns) = unapp
if leadingImplicits.flatten.nonEmpty then
// To support them see https://github.com/lampepfl/dotty/pull/13158
report.error("inline unapply methods with given parameters before the scrutinee are not supported", fun)

val sym = unapp.symbol

Expand All @@ -217,7 +214,7 @@ object Inlines:
List(unapply)
}

val newFun = newUnapply.select(unapplySym1).withSpan(unapp.span)
val newFun = newUnapply.select(unapplySym1).appliedToArgss(leadingImplicits).withSpan(unapp.span)
cpy.UnApply(unapp)(newFun, trailingImplicits, patterns)
end inlinedUnapply

Expand Down
15 changes: 15 additions & 0 deletions tests/pos/i12991.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
object Foo:
inline def unapply(using String)(i: Int): Some[Int] = Some(i)

object Bar:
inline def unapply(using String)(using String)(i: Int): Some[Int] = Some(i)

object Baz:
inline def unapply[T](using String)(i: T): Some[T] = Some(i)

given String = ""

val i = 10 match
case Foo(x) => x
case Bar(x) => x
case Baz(x) => x

0 comments on commit 14f3793

Please sign in to comment.