Skip to content

Commit 0ef4026

Browse files
Fix #9171: treat _ as _:Any in isMatchTypeShaped
1 parent 741e17e commit 0ef4026

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
@@ -4755,6 +4755,14 @@ object Types {
47554755
object MatchType {
47564756
def apply(bound: Type, scrutinee: Type, cases: List[Type])(using Context): MatchType =
47574757
unique(new CachedMatchType(bound, scrutinee, cases))
4758+
4759+
/** Extractor for `case _ =>` match type patterns */
4760+
object WildcardPattern {
4761+
def unapply(tp: Type)(using Context): Option[Type] = tp match {
4762+
case HKTypeLambda(LambdaParam(tl1, 0) :: Nil, defn.MatchCase(TypeParamRef(tl2, 0), bodyTp)) => Some(bodyTp)
4763+
case _ => None
4764+
}
4765+
}
47584766
}
47594767

47604768
// ------ 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
@@ -1546,6 +1546,13 @@ class Typer extends Namer
15461546
val Typed(_, tpt) = tpd.unbind(tpd.unsplice(pat1))
15471547
instantiateMatchTypeProto(pat1, pt) match {
15481548
case defn.MatchCase(patternTp, _) => tpt.tpe frozen_=:= patternTp
1549+
case MatchType.WildcardPattern(_) => tpt.tpe frozen_=:= defn.AnyType
1550+
case _ => false
1551+
}
1552+
case (id @ Ident(nme.WILDCARD), pt) =>
1553+
pt match {
1554+
case defn.MatchCase(patternTp, _) => defn.AnyType frozen_=:= patternTp
1555+
case MatchType.WildcardPattern(_) => true
15491556
case _ => false
15501557
}
15511558
case _ => false
@@ -1657,6 +1664,7 @@ class Typer extends Namer
16571664
def caseRest(pat: Tree)(using Context) = {
16581665
val pt1 = instantiateMatchTypeProto(pat, pt) match {
16591666
case defn.MatchCase(_, bodyPt) => bodyPt
1667+
case MatchType.WildcardPattern(bodyPt) => bodyPt
16601668
case pt => pt
16611669
}
16621670
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)