Skip to content

Commit bdbfd22

Browse files
author
gorilskij
committed
separate the two type argument lists in an extension unapply method
move test cases to pos-macros split up test cases add test cases for two argument lists
1 parent 22bb628 commit bdbfd22

25 files changed

+183
-259
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,28 @@ object Inliner {
208208
val cls = newNormalizedClassSymbol(ctx.owner, tpnme.ANON_CLASS, Synthetic | Final, List(defn.ObjectType), coord = sym.coord)
209209
val constr = newConstructor(cls, Synthetic, Nil, Nil, coord = sym.coord).entered
210210

211-
val targs = fun match
212-
case TypeApply(_, targs) => targs
213-
case _ => Nil
214-
215-
val unapplyInfo = sym.info match
216-
case info: PolyType => info.instantiate(targs.map(_.tpe)) match
211+
val (targs1, targs2) = fun match
212+
// extension [T] (x: X) def unapply[U](y: Y)
213+
case TypeApply(Apply(TypeApply(_, targs1), _), targs2) => (targs1, targs2)
214+
// def unapply[U](y: Y)
215+
case TypeApply(_, targs) => (targs, Nil)
216+
// extension [T] (x: X) def unapply(y: Y)
217+
case Apply(TypeApply(_, targs1), _) => (targs1, Nil)
218+
// def unapply(y: Y)
219+
case _ => (Nil, Nil)
220+
221+
def extensionInfo(info: Type, targs: List[Tree]): Type =
222+
info match
217223
case MethodTpe(_, _, rt: PolyType) => rt.instantiate(targs.map(_.tpe))
218-
case MethodTpe(_, _, rt) if sym.flags.is(ExtensionMethod) => rt
224+
case MethodTpe(_, _, rt) => rt
219225
case info => info
220-
case MethodTpe(_, _, rt: PolyType) => rt.instantiate(targs.map(_.tpe))
221-
case MethodTpe(_, _, rt) if sym.flags.is(ExtensionMethod) => rt
222-
case info => info
226+
227+
val unapplyInfo = sym.info match
228+
case pt: PolyType =>
229+
val info = pt.instantiate(targs1.map(_.tpe))
230+
if sym.flags.is(ExtensionMethod) then extensionInfo(info, targs2) else info
231+
case info =>
232+
if sym.flags.is(ExtensionMethod) then extensionInfo(info, targs1) else info
223233

224234
val unappplySym = newSymbol(cls, sym.name.toTermName, Synthetic | Method, unapplyInfo, coord = sym.coord).entered
225235
val unapply = DefDef(unappplySym, argss =>

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/pos/i8577/MacroA_1.scala

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

tests/pos/i8577/MacroB_1.scala

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

tests/pos/i8577/MacroC_1.scala

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

tests/pos/i8577/MacroD_1.scala

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

tests/pos/i8577/MacroE_1.scala

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

tests/pos/i8577/MacroF_1.scala

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

tests/pos/i8577/MacroG_1.scala

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

0 commit comments

Comments
 (0)