Skip to content

Commit 5fc9776

Browse files
gorilskijnicolasstucki
gorilskij
authored andcommitted
Support inline unapply extension methods
Fixes the computation of the inline unapply temporary unanimous unapply placeholder. Fixes #8577
1 parent 14f3793 commit 5fc9776

26 files changed

+304
-8
lines changed

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,7 @@ object Inlines:
199199
var unapplySym1: Symbol = NoSymbol // created from within AnonClass() and used afterwards
200200

201201
val newUnapply = AnonClass(ctx.owner, List(defn.ObjectType), sym.coord) { cls =>
202-
val targs = fun match
203-
case TypeApply(_, targs) => targs
204-
case _ => Nil
205-
val unapplyInfo = sym.info match
206-
case info: PolyType => info.instantiate(targs.map(_.tpe))
207-
case info => info
208-
209-
val unapplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method, unapplyInfo, coord = sym.coord).entered
202+
val unapplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method, fun.tpe.widen, coord = sym.coord).entered
210203
val unapply = DefDef(unapplySym.asTerm, argss =>
211204
inlineCall(fun.appliedToArgss(argss).withSpan(unapp.span))(using ctx.withOwner(unapplySym))
212205
)

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)