Skip to content

Commit d0138bf

Browse files
gorilskijnicolasstucki
gorilskij
authored andcommitted
Support inline unapply extension methods
Fixes #8577
1 parent 80f67df commit d0138bf

26 files changed

+307
-11
lines changed

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,13 @@ object Inlines:
190190
val cls = newNormalizedClassSymbol(ctx.owner, tpnme.ANON_CLASS, Synthetic | Final, List(defn.ObjectType), coord = sym.coord)
191191
val constr = newConstructor(cls, Synthetic, Nil, Nil, coord = sym.coord).entered
192192

193-
val targs = fun match
194-
case TypeApply(_, targs) => targs
195-
case _ => Nil
196-
val unapplyInfo = sym.info match
197-
case info: PolyType => info.instantiate(targs.map(_.tpe))
198-
case info => info
199-
200-
val unappplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method, unapplyInfo, coord = sym.coord).entered
201-
val unapply = DefDef(unappplySym, argss =>
202-
inlineCall(fun.appliedToArgss(argss).withSpan(unapp.span))(using ctx.withOwner(unappplySym))
193+
val unapplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method, fun.tpe.widen, coord = sym.coord).entered
194+
val unapply = DefDef(unapplySym, argss =>
195+
inlineCall(fun.appliedToArgss(argss).withSpan(unapp.span))(using ctx.withOwner(unapplySym))
203196
)
204197
val cdef = ClassDef(cls, DefDef(constr), List(unapply))
205198
val newUnapply = Block(cdef :: Nil, New(cls.typeRef, Nil))
206-
val newFun = newUnapply.select(unappplySym).withSpan(unapp.span)
199+
val newFun = newUnapply.select(unapplySym).withSpan(unapp.span)
207200
cpy.UnApply(unapp)(newFun, implicits, patterns)
208201
end inlinedUnapply
209202

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)

tests/pos-macros/i8577e/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, U](sc: Expr[Macro.StrCtx], input: Expr[U])(using Type[U])(using Quotes): Expr[Option[Seq[U]]] =
11+
'{ Some(Seq(${input})) }

tests/pos-macros/i8577e/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: U): Option[Seq[U]] =
6+
${ implUnapply('ctx, 'input) }
7+
8+
val mac"$x" = 1
9+
assert(x == 1)

tests/pos-macros/i8577f/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, U](sc: Expr[Macro.StrCtx], input: Expr[(T, U)])(using Type[T], Type[U])(using Quotes): Expr[Option[Seq[(T, U)]]] =
11+
'{ Some(Seq(${input})) }

tests/pos-macros/i8577f/Main_2.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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, U)): Option[Seq[(T, U)]] =
6+
${ implUnapply('ctx, 'input) }
7+
8+
val mac"$x" = (1, 2)
9+
assert(x == (1, 2))
10+
11+
val mac"$y" = (1, "a")
12+
assert(y == (1, "a"))

tests/pos-macros/i8577g/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, U](sc: Expr[Macro.StrCtx], input: Expr[T | U])(using Type[T], Type[U])(using Quotes): Expr[Option[Seq[T | U]]] =
11+
'{ Some(Seq(${input})) }

tests/pos-macros/i8577g/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 | U): Option[Seq[T | U]] =
6+
${ implUnapply('ctx, 'input) }
7+
8+
val mac"$x" = 1
9+
assert(x == 1)

tests/pos-macros/i8577h/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, U](sc: Expr[Macro.StrCtx], input: Expr[T | U])(using Type[T], Type[U])(using Quotes): Expr[Option[Seq[T | U]]] =
11+
'{ Some(Seq(${input})) }

tests/pos-macros/i8577h/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: U | T): Option[Seq[T | U]] =
6+
${ implUnapply('ctx, 'input) }
7+
8+
val mac"$x" = 1
9+
assert(x == 1)

tests/run/i8577a.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
object Macro:
4+
opaque type StrCtx = StringContext
5+
def apply(ctx: StringContext): StrCtx = ctx
6+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
7+
8+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
9+
extension (inline ctx: Macro.StrCtx) inline def unapplySeq(inline input: Int): Option[Seq[Int]] =
10+
Some(Seq(input))
11+
12+
@main def Test: Unit =
13+
val mac"$x" = 1
14+
val y: Int = x
15+
assert(x == 1)

tests/run/i8577b.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
object Macro:
4+
opaque type StrCtx = StringContext
5+
def apply(ctx: StringContext): StrCtx = ctx
6+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
7+
8+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
9+
extension (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: U): Option[Seq[U]] =
10+
Some(Seq(input))
11+
12+
@main def Test: Unit =
13+
val mac"$x" = 1
14+
val y: Int = x
15+
assert(x == 1)

tests/run/i8577c.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
object Macro:
4+
opaque type StrCtx = StringContext
5+
def apply(ctx: StringContext): StrCtx = ctx
6+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
7+
8+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
9+
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq(inline input: T): Option[Seq[T]] =
10+
Some(Seq(input))
11+
12+
@main def Test: Unit =
13+
val mac"$x" = 1
14+
val y: Int = x
15+
assert(x == 1)

tests/run/i8577d.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
object Macro:
4+
opaque type StrCtx = StringContext
5+
def apply(ctx: StringContext): StrCtx = ctx
6+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
7+
8+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
9+
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: T): Option[Seq[T]] =
10+
Some(Seq(input))
11+
12+
@main def Test: Unit =
13+
val mac"$x" = 1
14+
val y: Int = x
15+
assert(x == 1)

tests/run/i8577e.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import scala.quoted._
2+
3+
object Macro:
4+
opaque type StrCtx = StringContext
5+
def apply(ctx: StringContext): StrCtx = ctx
6+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
7+
8+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
9+
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: (T, U)): Option[Seq[(T, U)]] =
10+
Some(Seq(input))
11+
12+
@main def Test: Unit =
13+
val mac"$x" = (1, 2)
14+
val x2: (Int, Int) = x
15+
assert(x == (1, 2))
16+
17+
val mac"$y" = (1, "a")
18+
val y2: (Int, String) = y
19+
assert(y == (1, "a"))

tests/run/i8577f.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
object Macro:
4+
opaque type StrCtx = StringContext
5+
def apply(ctx: StringContext): StrCtx = ctx
6+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
7+
8+
@main def Test: Unit =
9+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
10+
extension (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: U): Option[Seq[U]] =
11+
Some(Seq(input))
12+
13+
val mac"$x" = 1
14+
val y: Int = x
15+
assert(x == 1)

tests/run/i8577g.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
object Macro:
4+
opaque type StrCtx = StringContext
5+
def apply(ctx: StringContext): StrCtx = ctx
6+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
7+
8+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
9+
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: T | U): Option[Seq[T | U]] =
10+
Some(Seq(input))
11+
12+
@main def Test: Unit =
13+
val mac"$x" = 1
14+
val y: Int = x
15+
assert(x == 1)

tests/run/i8577h.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
object Macro:
4+
opaque type StrCtx = StringContext
5+
def apply(ctx: StringContext): StrCtx = ctx
6+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
7+
8+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
9+
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: U | T): Option[Seq[T | U]] =
10+
Some(Seq(input))
11+
12+
@main def Test: Unit =
13+
val mac"$x" = 1
14+
val y: Int = x
15+
assert(x == 1)

tests/run/i8577i.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import scala.quoted._
2+
3+
object Macro:
4+
opaque type StrCtx = StringContext
5+
def apply(ctx: StringContext): StrCtx = ctx
6+
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx)
7+
8+
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx)
9+
extension (inline ctx: Macro.StrCtx) transparent inline def unapplySeq(inline input: String): Option[Seq[Any]] =
10+
Some(Seq(123))
11+
12+
@main def Test: Unit =
13+
"abc" match
14+
case mac"$x" =>
15+
val y: Int = x
16+
assert(x == 123)

0 commit comments

Comments
 (0)