Skip to content

Commit 9e8e086

Browse files
Fix scala#9171: treat _ as _:Any in isMatchTypeShaped
1 parent b7217f1 commit 9e8e086

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4740,6 +4740,14 @@ object Types {
47404740
object MatchType {
47414741
def apply(bound: Type, scrutinee: Type, cases: List[Type])(using Context): MatchType =
47424742
unique(new CachedMatchType(bound, scrutinee, cases))
4743+
4744+
/** Extractor for `case _ =>` match type patterns */
4745+
object WildcardPattern {
4746+
def unapply(tp: Type)(using Context): Option[Type] = tp match {
4747+
case HKTypeLambda(LambdaParam(tl1, 0) :: Nil, defn.MatchCase(TypeParamRef(tl2, 0), bodyTp)) => Some(bodyTp)
4748+
case _ => None
4749+
}
4750+
}
47434751
}
47444752

47454753
// ------ ClassInfo, Type Bounds --------------------------------------------------

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,13 @@ class Typer extends Namer
15191519
val Typed(_, tpt) = tpd.unbind(tpd.unsplice(pat1))
15201520
instantiateMatchTypeProto(pat1, pt) match {
15211521
case defn.MatchCase(patternTp, _) => tpt.tpe frozen_=:= patternTp
1522+
case MatchType.WildcardPattern(_) => tpt.tpe frozen_=:= defn.AnyType
1523+
case _ => false
1524+
}
1525+
case (id @ Ident(nme.WILDCARD), pt) =>
1526+
pt match {
1527+
case defn.MatchCase(patternTp, _) => defn.AnyType frozen_=:= patternTp
1528+
case MatchType.WildcardPattern(_) => true
15221529
case _ => false
15231530
}
15241531
case _ => false
@@ -1630,6 +1637,7 @@ class Typer extends Namer
16301637
def caseRest(pat: Tree)(using Context) = {
16311638
val pt1 = instantiateMatchTypeProto(pat, pt) match {
16321639
case defn.MatchCase(_, bodyPt) => bodyPt
1640+
case MatchType.WildcardPattern(bodyPt) => bodyPt
16331641
case pt => pt
16341642
}
16351643
val pat1 = indexPattern(tree).transform(pat)

tests/pos/unify-wildcard-patterns.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@ object Test0 {
77
def m[X](x: X): M[X] = x match { case _: String => 1 case _: Any => "s" }
88
}
99

10+
object Test1 {
11+
type M[X] = X match { case String => Int case Any => String }
12+
def m[X](x: X): M[X] = x match { case _: String => 1 case _ => "s" }
13+
}
14+
1015
object Test2 {
1116
type M[X] = X match { case String => Int case _ => String }
1217
def m[X](x: X): M[X] = x match { case _: String => 1 case _: Any => "s" }
1318
}
19+
20+
object Test3 {
21+
type M[X] = X match { case String => Int case _ => String }
22+
def m[X](x: X): M[X] = x match { case _: String => 1 case _ => "s" }
23+
}

0 commit comments

Comments
 (0)