Skip to content

Commit 3b1d780

Browse files
committed
Support inline unapply extension methods
* Fixes the computation of the inline unapply temporary unanimous unapply placeholder * Handle leading given parameters in inline unapplies Fixes #8577 Fixes #12991 Fixes #15188
1 parent efa9bd5 commit 3b1d780

31 files changed

+364
-26
lines changed

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,35 +181,24 @@ 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 ...)(x1,x2,...)(using t1: T1, t2: T2, ...)` is transformed into
185+
// `{ class $anon { def unapply(s: S)(using t1: T1, t2: T2, ...): R = P.unapply[...](using ...)(s)(using t1, t2, ...) }; new $anon }.unapply(using y1,y2,...)`
186+
// and the call `P.unapply[...](using ...)(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`.
190190

191-
object SplitFunAndGivenArgs:
192-
def unapply(tree: Tree): (Tree, List[List[Tree]]) = tree match
193-
case Apply(SplitFunAndGivenArgs(fn, argss), args) => (fn, argss :+ args)
194-
case _ => (tree, Nil)
195-
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)
191+
val UnApply(fun, trailingImplicits, patterns) = unapp
199192

200193
val sym = unapp.symbol
201194

202195
var unapplySym1: Symbol = NoSymbol // created from within AnonClass() and used afterwards
203196

204197
val newUnapply = AnonClass(ctx.owner, List(defn.ObjectType), sym.coord) { cls =>
205-
val targs = fun match
206-
case TypeApply(_, targs) => targs
207-
case _ => Nil
208-
val unapplyInfo = sym.info match
209-
case info: PolyType => info.instantiate(targs.map(_.tpe))
210-
case info => info
211-
212-
val unapplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method, unapplyInfo, coord = sym.coord).entered
198+
// `fun` is a partially applied method that contains all type applications of the method.
199+
// The methodic type `fun.tpe.widen` is the type of the function starting from the scrutinee argument
200+
// and its type parameters are instantiated.
201+
val unapplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method, fun.tpe.widen, coord = sym.coord).entered
213202
val unapply = DefDef(unapplySym.asTerm, argss =>
214203
inlineCall(fun.appliedToArgss(argss).withSpan(unapp.span))(using ctx.withOwner(unapplySym))
215204
)

tests/neg/i12991.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/pos-macros/i8577a/Macro_1.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package i8577
2+
3+
import scala.quoted._
4+
5+
object Macro:
6+
opaque type StrCtx = StringContext
7+
def apply(ctx: StringContext): StrCtx = ctx
8+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
9+
10+
def implUnapply(sc: Expr[Macro.StrCtx], input: Expr[Int])(using Quotes): Expr[Option[Seq[Int]]] =
11+
'{ Some(Seq(${input})) }

tests/pos-macros/i8577a/Main_2.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package i8577
2+
3+
def main: Unit =
4+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
5+
extension (inline ctx: Macro.StrCtx) inline def unapplySeq(inline input: Int): Option[Seq[Int]] =
6+
${ implUnapply('ctx, 'input) }
7+
8+
val mac"$x" = 1
9+
assert(x == 1)

tests/pos-macros/i8577b/Macro_1.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package i8577
2+
3+
import scala.quoted._
4+
5+
object Macro:
6+
opaque type StrCtx = StringContext
7+
def apply(ctx: StringContext): StrCtx = ctx
8+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
9+
10+
def implUnapply[U](sc: Expr[Macro.StrCtx], input: Expr[U])(using Type[U])(using Quotes): Expr[Option[Seq[U]]] =
11+
'{ Some(Seq(${input})) }

tests/pos-macros/i8577b/Main_2.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package i8577
2+
3+
def main: Unit =
4+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
5+
extension (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: U): Option[Seq[U]] =
6+
${ implUnapply('ctx, 'input) }
7+
8+
val mac"$x" = 1
9+
assert(x == 1)

tests/pos-macros/i8577c/Macro_1.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package i8577
2+
3+
import scala.quoted._
4+
5+
object Macro:
6+
opaque type StrCtx = StringContext
7+
def apply(ctx: StringContext): StrCtx = ctx
8+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
9+
10+
def implUnapply[T](sc: Expr[Macro.StrCtx], input: Expr[T])(using Type[T])(using Quotes): Expr[Option[Seq[T]]] =
11+
'{ Some(Seq(${input})) }

tests/pos-macros/i8577c/Main_2.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package i8577
2+
3+
def main: Unit =
4+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
5+
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq(inline input: T): Option[Seq[T]] =
6+
${ implUnapply('ctx, 'input) }
7+
8+
val mac"$x" = 1
9+
assert(x == 1)

tests/pos-macros/i8577d/Macro_1.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package i8577
2+
3+
import scala.quoted._
4+
5+
object Macro:
6+
opaque type StrCtx = StringContext
7+
def apply(ctx: StringContext): StrCtx = ctx
8+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
9+
10+
def implUnapply[T](sc: Expr[Macro.StrCtx], input: Expr[T])(using Type[T])(using Quotes): Expr[Option[Seq[T]]] =
11+
'{ Some(Seq(${input})) }

tests/pos-macros/i8577d/Main_2.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package i8577
2+
3+
def main: Unit =
4+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
5+
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: T): Option[Seq[T]] =
6+
${ implUnapply('ctx, 'input) }
7+
8+
val mac"$x" = 1
9+
assert(x == 1)

0 commit comments

Comments
 (0)