Skip to content

Commit f23275b

Browse files
committed
Use MatchCase instead of FunctionOf for match cases
That way, we can control that the variance of a case is 0.
1 parent 8d6389a commit f23275b

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,9 @@ class Definitions {
759759

760760
lazy val TypeBox_CAP: TypeSymbol = TypeBoxType.symbol.requiredType(tpnme.CAP)
761761

762+
lazy val MatchCaseType: TypeRef = ctx.requiredClassRef("scala.internal.MatchCase")
763+
def MatchCaseClass(implicit ctx: Context): ClassSymbol = MatchCaseType.symbol.asClass
764+
762765
lazy val NotType: TypeRef = ctx.requiredClassRef("scala.implicits.Not")
763766
def NotClass(implicit ctx: Context): ClassSymbol = NotType.symbol.asClass
764767
def NotModule(implicit ctx: Context): Symbol = NotClass.companionModule
@@ -931,6 +934,17 @@ class Definitions {
931934
}
932935
}
933936

937+
object MatchCase {
938+
def apply(pat: Type, body: Type)(implicit ctx: Context): Type =
939+
MatchCaseType.appliedTo(pat, body)
940+
def unapply(tp: Type)(implicit ctx: Context): Option[(Type, Type)] = tp match {
941+
case AppliedType(tycon, pat :: body :: Nil) if tycon.isRef(MatchCaseClass) =>
942+
Some((pat, body))
943+
case _ =>
944+
None
945+
}
946+
}
947+
934948
/** An extractor for multi-dimensional arrays.
935949
* Note that this will also extract the high bound if an
936950
* element type is a wildcard. E.g.

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,7 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
21482148
case _ =>
21492149
cas
21502150
}
2151-
val defn.FunctionOf(pat :: Nil, body, _, _) = cas1
2151+
val defn.MatchCase(pat, body) = cas1
21522152
if (isSubType(scrut, pat))
21532153
// `scrut` is a subtype of `pat`: *It's a Match!*
21542154
Some {

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3756,7 +3756,7 @@ object Types {
37563756

37573757
def caseType(tp: Type)(implicit ctx: Context): Type = tp match {
37583758
case tp: HKTypeLambda => caseType(tp.resType)
3759-
case defn.FunctionOf(_, restpe, _, _) => restpe
3759+
case defn.MatchCase(_, body) => body
37603760
}
37613761

37623762
def alternatives(implicit ctx: Context): List[Type] = cases.map(caseType)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ trait TypeAssigner {
487487
}
488488
HKTypeLambda.fromParams(
489489
params(new mutable.ListBuffer[TypeSymbol](), pat).toList,
490-
defn.FunctionOf(pat.tpe :: Nil, body.tpe))
490+
defn.MatchCase(pat.tpe, body.tpe))
491491
}
492492
else body.tpe
493493
tree.withType(ownType)

0 commit comments

Comments
 (0)