Skip to content

Commit 6a481a9

Browse files
committed
Add MatchTypeCase to Tasty Reflect
1 parent feb8b2e commit 6a481a9

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,27 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
18811881
end extension
18821882
end MatchTypeMethods
18831883

1884+
type MatchTypeCase = dotc.core.Types.Type
1885+
1886+
object MatchTypeCase extends MatchTypeCaseModule:
1887+
def apply(pattern: TypeRepr, body: TypeRepr): MatchTypeCase =
1888+
Types.AppliedType(ctx.definitions.MatchCaseClass.typeRef, List(pattern, body))
1889+
1890+
def apply(
1891+
paramNames: List[String],
1892+
boundsFn: TypeLambda => List[TypeBounds],
1893+
patternFn: TypeLambda => TypeRepr,
1894+
bodyFn: TypeLambda => TypeRepr,
1895+
): MatchTypeCase =
1896+
reflect.TypeLambda(paramNames, boundsFn, tl => MatchTypeCase(patternFn(tl), bodyFn(tl)))
1897+
1898+
def unapply(x: MatchTypeCase): Option[(List[String], List[TypeBounds], TypeRepr, TypeRepr)] =
1899+
x match
1900+
case AppliedType(tycon, Seq(from, to)) if tycon.isRef(ctx.definitions.MatchCaseClass) =>
1901+
Some(Nil, Nil, from, to)
1902+
case TypeLambda(paramNames, paramBounds, AppliedType(tycon, Seq(from, to))) if tycon.isRef(ctx.definitions.MatchCaseClass) =>
1903+
Some(paramNames.map(_.toString), paramBounds, from, to)
1904+
18841905
type ByNameType = dotc.core.Types.ExprType
18851906

18861907
object ByNameTypeTypeTest extends TypeTest[TypeRepr, ByNameType]:

library/src/scala/quoted/Quotes.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,7 +2442,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
24422442
/** Methods of the module object `val MatchType` */
24432443
trait MatchTypeModule { this: MatchType.type =>
24442444
def apply(bound: TypeRepr, scrutinee: TypeRepr, cases: List[TypeRepr]): MatchType
2445-
def unapply(x: MatchType): Option[(TypeRepr, TypeRepr, List[TypeRepr])]
2445+
def unapply(x: MatchType): Option[(TypeRepr, TypeRepr, List[MatchTypeCase])]
24462446
}
24472447

24482448
/** Makes extension methods on `MatchType` available without any imports */
@@ -2453,10 +2453,25 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
24532453
extension (self: MatchType):
24542454
def bound: TypeRepr
24552455
def scrutinee: TypeRepr
2456-
def cases: List[TypeRepr]
2456+
def cases: List[MatchTypeCase]
24572457
end extension
24582458
end MatchTypeMethods
24592459

2460+
type MatchTypeCase <: TypeRepr
2461+
2462+
val MatchTypeCase: MatchTypeCaseModule
2463+
2464+
trait MatchTypeCaseModule { this: MatchTypeCase.type =>
2465+
def apply(pattern: TypeRepr, body: TypeRepr): MatchTypeCase
2466+
2467+
def apply(
2468+
paramNames: List[String],
2469+
boundsFn: TypeLambda => List[TypeBounds],
2470+
patternFn: TypeLambda => TypeRepr,
2471+
bodyFn: TypeLambda => TypeRepr,
2472+
): MatchTypeCase
2473+
}
2474+
24602475
/** Type of a by by name parameter */
24612476
type ByNameType <: TypeRepr
24622477

scala3doc/src/dotty/dokka/tasty/SyntheticSupport.scala

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,3 @@ trait SyntheticsSupport:
9696
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
9797
val cSym = c.symbol.asInstanceOf[dotc.core.Symbols.Symbol]
9898
cSym.typeRef.appliedTo(cSym.typeParams.map(_.typeRef)).asInstanceOf[TypeRepr]
99-
100-
object MatchTypeCase:
101-
def unapply(tpe: TypeRepr): Option[(TypeRepr, TypeRepr)] =
102-
tpe match
103-
case AppliedType(t, Seq(from, to)) /*if t == MatchCaseType*/ =>
104-
Some((from, to))
105-
case TypeLambda(paramNames, paramTypes, AppliedType(t, Seq(from, to))) /*if t == MatchCaseType*/ =>
106-
Some((from, to))
107-
case _ =>
108-
None

scala3doc/src/dotty/dokka/tasty/TypesSupport.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ trait TypesSupport:
240240

241241
case MatchType(bond, sc, cases) =>
242242
val casesTexts = cases.flatMap {
243-
case MatchTypeCase(from, to) =>
243+
case MatchTypeCase(_, _, from, to) =>
244244
texts(" case ") ++ inner(from) ++ texts(" => ") ++ inner(to) ++ texts("\n")
245245
}
246246
inner(sc) ++ texts(" match {\n") ++ casesTexts ++ texts("}")

tests/run-macros/tasty-construct-types/Macro_1.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@ object Macros {
3333
TypeRepr.of[Int],
3434
TypeRepr.of[List[8]],
3535
List(
36-
TypeLambda(
36+
MatchTypeCase(
3737
List("t"),
3838
_ => List(TypeBounds(TypeRepr.of[Nothing], TypeRepr.of[Any])),
39-
tl => TypeRepr.of[scala.runtime.MatchCase].appliedTo(List(TypeRepr.of[List].appliedTo(tl.param(0)), tl.param(0)))))
39+
tl => TypeRepr.of[List].appliedTo(tl.param(0)),
40+
tl => tl.param(0),
41+
),
42+
MatchTypeCase(
43+
TypeRepr.of[Int],
44+
TypeRepr.of[Int],
45+
)
46+
)
4047
)
4148

4249
assert(x1T =:= TypeRepr.of[1])
@@ -46,7 +53,7 @@ object Macros {
4653
assert(x5T =:= TypeRepr.of[RefineMe { type T = Int }])
4754
assert(x6T =:= TypeRepr.of[List[Int]])
4855
assert(x7T =:= TypeRepr.of[7 @TestAnnotation])
49-
assert(x8T =:= TypeRepr.of[List[8] match { case List[t] => t }])
56+
assert(x8T =:= TypeRepr.of[List[8] match { case List[t] => t ; case Int => Int }])
5057

5158
'{
5259
println("Ok")

0 commit comments

Comments
 (0)