Skip to content

Update Tasty Reflect match type code #10690

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

Closed
Closed
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
41 changes: 41 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,47 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end extension
end MatchTypeMethods

type MatchTypeCase = dotc.core.Types.Type

object MatchTypeCaseTypeTest extends TypeTest[TypeRepr, MatchTypeCase]:
def unapply(x: TypeRepr): Option[MatchTypeCase & x.type] =
MatchTypeCase.unapply(x.asInstanceOf[MatchTypeCase]).map(_ => x.asInstanceOf[MatchTypeCase & x.type])
end MatchTypeCaseTypeTest

object MatchTypeCase extends MatchTypeCaseModule:
def apply(pattern: TypeRepr, body: TypeRepr): MatchTypeCase =
Types.AppliedType(ctx.definitions.MatchCaseClass.typeRef, List(pattern, body))

def apply(
paramNames: List[String],
boundsFn: TypeLambda => List[TypeBounds],
patternFn: TypeLambda => TypeRepr,
bodyFn: TypeLambda => TypeRepr,
): MatchTypeCase =
reflect.TypeLambda(paramNames, boundsFn, tl => MatchTypeCase(patternFn(tl), bodyFn(tl)))

def unapply(x: MatchTypeCase): Option[(List[String], List[TypeBounds], TypeRepr, TypeRepr)] = x match
case AppliedType(tycon, Seq(from, to)) if tycon.isRef(ctx.definitions.MatchCaseClass) =>
Some((Nil, Nil, from, to))
case TypeLambda(paramNames, paramBounds, AppliedType(tycon, Seq(from, to))) if tycon.isRef(ctx.definitions.MatchCaseClass) =>
Some((paramNames.map(_.toString), paramBounds, from, to))
case _ =>
None
end MatchTypeCase

given MatchTypeCaseMethods: MatchTypeCaseMethods with
extension (self: MatchTypeCase):
def paramNames: List[String] = unwrap(self)(0)
def paramBounds: List[TypeBounds] = unwrap(self)(1)
def pattern: TypeRepr = unwrap(self)(2)
def body: TypeRepr = unwrap(self)(3)
end extension

private def unwrap(x: MatchTypeCase) =
// should never throw within limits of the API
MatchTypeCase.unapply(x).get
end MatchTypeCaseMethods

type ByNameType = dotc.core.Types.ExprType

object ByNameTypeTypeTest extends TypeTest[TypeRepr, ByNameType]:
Expand Down
40 changes: 38 additions & 2 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2442,7 +2442,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Methods of the module object `val MatchType` */
trait MatchTypeModule { this: MatchType.type =>
def apply(bound: TypeRepr, scrutinee: TypeRepr, cases: List[TypeRepr]): MatchType
def unapply(x: MatchType): Option[(TypeRepr, TypeRepr, List[TypeRepr])]
def unapply(x: MatchType): Option[(TypeRepr, TypeRepr, List[MatchTypeCase])]
}

/** Makes extension methods on `MatchType` available without any imports */
Expand All @@ -2453,10 +2453,46 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
extension (self: MatchType):
def bound: TypeRepr
def scrutinee: TypeRepr
def cases: List[TypeRepr]
def cases: List[MatchTypeCase]
end extension
end MatchTypeMethods

/** Case of a match type `case U => S`. */
type MatchTypeCase <: TypeRepr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep the abstractions aligned with what is in TASTy and not add new ones.


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

/** Module object of `case U => S` */
val MatchTypeCase: MatchTypeCaseModule

/** Methods of the module object of `case U => S` */
trait MatchTypeCaseModule { this: MatchTypeCase.type =>
def apply(pattern: TypeRepr, body: TypeRepr): MatchTypeCase

def apply(
paramNames: List[String],
boundsFn: TypeLambda => List[TypeBounds],
patternFn: TypeLambda => TypeRepr,
bodyFn: TypeLambda => TypeRepr,
): MatchTypeCase

def unapply(x: MatchTypeCase): Option[(List[String], List[TypeBounds], TypeRepr, TypeRepr)]
}

/** Makes extension methods on `MatchTypeCase` available without any imports */
given MatchTypeCaseMethods: MatchTypeCaseMethods

/** Extension methods of `MatchTypeCase` */
trait MatchTypeCaseMethods:
extension (self: MatchTypeCase):
def paramNames: List[String]
def paramBounds: List[TypeBounds]
def pattern: TypeRepr
def body: TypeRepr
end extension
end MatchTypeCaseMethods

/** Type of a by by name parameter */
type ByNameType <: TypeRepr

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*/ =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these reflect extractor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was code that we used in the doctool to take apart match types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indicates that MatchType are already fully supported. It is just a matter of knowing what needs to go there. This will need some documentation.

Some((from, to))
case TypeLambda(paramNames, paramTypes, AppliedType(t, Seq(from, to))) /*if t == MatchCaseType*/ =>
Some((from, to))
case _ =>
None
2 changes: 1 addition & 1 deletion scala3doc/src/dotty/dokka/tasty/TypesSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ trait TypesSupport:

case MatchType(bond, sc, cases) =>
val casesTexts = cases.flatMap {
case MatchTypeCase(from, to) =>
case MatchTypeCase(_, _, from, to) =>
texts(" case ") ++ inner(from) ++ texts(" => ") ++ inner(to) ++ texts("\n")
}
inner(sc) ++ texts(" match {\n") ++ casesTexts ++ texts("}")
Expand Down
16 changes: 13 additions & 3 deletions tests/run-macros/tasty-construct-types/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ object Macros {
TypeRepr.of[Int],
TypeRepr.of[List[8]],
List(
TypeLambda(
MatchTypeCase(
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 => TypeRepr.of[List].appliedTo(tl.param(0)),
tl => tl.param(0),
),
MatchTypeCase(
TypeRepr.of[Int],
TypeRepr.of[Int],
)
)
)

assert(x1T =:= TypeRepr.of[1])
Expand All @@ -46,7 +53,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