Skip to content

Add reflect MatchCase TypeRepr #10735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler

object AppliedTypeTypeTest extends TypeTest[TypeRepr, AppliedType]:
def unapply(x: TypeRepr): Option[AppliedType & x.type] = x match
case tpe: (Types.AppliedType & x.type) => Some(tpe)
case tpe: (Types.AppliedType & x.type) if !tpe.tycon.isRef(dotc.core.Symbols.defn.MatchCaseClass) => Some(tpe)
case _ => None
end AppliedTypeTypeTest

Expand Down Expand Up @@ -2051,6 +2051,27 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end extension
end TypeLambdaMethods

type MatchCase = dotc.core.Types.AppliedType

given MatchCaseTypeTest: TypeTest[TypeRepr, MatchCase] with
def unapply(x: TypeRepr): Option[MatchCase & x.type] = x match
case x: (Types.AppliedType & x.type) if x.tycon.isRef(dotc.core.Symbols.defn.MatchCaseClass) => Some(x)
case _ => None
end MatchCaseTypeTest

object MatchCase extends MatchCaseModule:
def apply(pattern: TypeRepr, rhs: TypeRepr): MatchCase =
Types.AppliedType(dotc.core.Symbols.defn.MatchCaseClass.typeRef, List(pattern, rhs))
def unapply(x: MatchCase): (TypeRepr, TypeRepr) = (x.pattern, x.rhs)
end MatchCase

given MatchCaseMethods: MatchCaseMethods with
extension (self: MatchCase)
def pattern: TypeRepr = self.args(0)
def rhs: TypeRepr = self.args(1)
end extension
end MatchCaseMethods

type TypeBounds = dotc.core.Types.TypeBounds

object TypeBoundsTypeTest extends TypeTest[TypeRepr, TypeBounds]:
Expand Down
35 changes: 35 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
* +- LambdaType -+- MethodOrPoly -+- MethodType
* | | +- PolyType
* | +- TypeLambda
* +- MatchCase
* +- TypeBounds
* +- NoPrefix
*
Expand Down Expand Up @@ -2678,6 +2679,40 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
end extension
end TypeLambdaMethods

/** Case of a `MatchType` containing pattern `case P => R`.
*
* Note: cases with type bindings are represented nested in a `TypeLambda`.
*/
type MatchCase <: TypeRepr

/** `TypeTest` that allows testing at runtime in a pattern match if a `TypeRepr` is a `MatchCase` */
given MatchCaseTypeTest: TypeTest[TypeRepr, MatchCase]

/** Module object of `type MatchCase` */
val MatchCase: MatchCaseModule

/** Methods of the module object `val MatchCase` */
trait MatchCaseModule { this: MatchCase.type =>
/* Create match type case `case <pattern> => <rhs>` */
def apply(pattern: TypeRepr, rhs: TypeRepr): MatchCase
/* Matches a match type case `case <pattern> => <rhs>` */
def unapply(x: MatchCase): (TypeRepr, TypeRepr)
}

/** Makes extension methods on `MatchCase` available without any imports */
given MatchCaseMethods: MatchCaseMethods

/** Extension methods of `MatchCase` */
trait MatchCaseMethods:
extension (self: MatchCase)
/** Pattern `P` of `case P => R` in a `MatchType` */
def pattern: TypeRepr
/** RHS `R` of `case P => R` in a `MatchType` */
def rhs: TypeRepr
end extension
end MatchCaseMethods


// ----- TypeBounds -----------------------------------------------

/** Type bounds */
Expand Down
10 changes: 0 additions & 10 deletions scala3doc/src/dotty/dokka/tasty/SyntheticSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,3 @@ trait SyntheticsSupport:
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
val cSym = c.symbol.asInstanceOf[dotc.core.Symbols.Symbol]
cSym.typeRef.appliedTo(cSym.typeParams.map(_.typeRef)).asInstanceOf[TypeRepr]

object MatchTypeCase:
def unapply(tpe: TypeRepr): Option[(TypeRepr, TypeRepr)] =
tpe match
case AppliedType(t, Seq(from, to)) /*if t == MatchCaseType*/ =>
Some((from, to))
case TypeLambda(paramNames, paramTypes, AppliedType(t, Seq(from, to))) /*if t == MatchCaseType*/ =>
Some((from, to))
case _ =>
None
4 changes: 3 additions & 1 deletion scala3doc/src/dotty/dokka/tasty/TypesSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ trait TypesSupport:

case MatchType(bond, sc, cases) =>
val casesTexts = cases.flatMap {
case MatchTypeCase(from, to) =>
case MatchCase(from, to) =>
texts(" case ") ++ inner(from) ++ texts(" => ") ++ inner(to) ++ texts("\n")
case TypeLambda(_, _, MatchCase(from, to)) =>
texts(" case ") ++ inner(from) ++ texts(" => ") ++ inner(to) ++ texts("\n")
}
inner(sc) ++ texts(" match {\n") ++ casesTexts ++ texts("}")
Expand Down
9 changes: 7 additions & 2 deletions tests/run-macros/tasty-construct-types/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ object Macros {
TypeLambda(
List("t"),
_ => List(TypeBounds(TypeRepr.of[Nothing], TypeRepr.of[Any])),
tl => TypeRepr.of[scala.runtime.MatchCase].appliedTo(List(TypeRepr.of[List].appliedTo(tl.param(0)), tl.param(0)))))
tl => MatchCase(TypeRepr.of[List].appliedTo(tl.param(0)), tl.param(0))),
MatchCase(TypeRepr.of[Int], TypeRepr.of[Int])
)
)

assert(x1T =:= TypeRepr.of[1])
Expand All @@ -46,7 +48,10 @@ object Macros {
assert(x5T =:= TypeRepr.of[RefineMe { type T = Int }])
assert(x6T =:= TypeRepr.of[List[Int]])
assert(x7T =:= TypeRepr.of[7 @TestAnnotation])
assert(x8T =:= TypeRepr.of[List[8] match { case List[t] => t }])
assert(x8T =:= TypeRepr.of[List[8] match {
case List[t] => t
case Int => Int
}])

'{
println("Ok")
Expand Down