Skip to content

Commit e848c97

Browse files
Fix scala#9171: treat _ as _:Any in isMatchTypeShaped
1 parent 7ce59e9 commit e848c97

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4735,6 +4735,14 @@ object Types {
47354735
object MatchType {
47364736
def apply(bound: Type, scrutinee: Type, cases: List[Type])(using Context): MatchType =
47374737
unique(new CachedMatchType(bound, scrutinee, cases))
4738+
4739+
/** Extractor for `case _ =>` match type patterns */
4740+
object WildcardPattern {
4741+
def unapply(tp: Type)(using Context): Option[Type] = tp match {
4742+
case HKTypeLambda(LambdaParam(tl1, 0) :: Nil, defn.MatchCase(TypeParamRef(tl2, 0), bodyTp)) => Some(bodyTp)
4743+
case _ => None
4744+
}
4745+
}
47384746
}
47394747

47404748
// ------ ClassInfo, Type Bounds --------------------------------------------------

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2615,7 +2615,6 @@ object Parsers {
26152615
case USCORE if in.lookahead.isArrow =>
26162616
val start = in.skipToken()
26172617
typeBounds().withSpan(Span(start, in.lastOffset, start))
2618-
26192618
case _ =>
26202619
infixType()
26212620
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,13 @@ class Typer extends Namer
15181518
val Typed(_, tpt) = tpd.unbind(tpd.unsplice(pat1))
15191519
instantiateMatchTypeProto(pat1, pt) match {
15201520
case defn.MatchCase(patternTp, _) => tpt.tpe frozen_=:= patternTp
1521+
case MatchType.WildcardPattern(_) => tpt.tpe frozen_=:= defn.AnyType
1522+
case _ => false
1523+
}
1524+
case (id @ Ident(nme.WILDCARD), pt) =>
1525+
pt match {
1526+
case defn.MatchCase(patternTp, _) => defn.AnyType frozen_=:= patternTp
1527+
case MatchType.WildcardPattern(_) => true
15211528
case _ => false
15221529
}
15231530
case _ => false
@@ -1629,6 +1636,7 @@ class Typer extends Namer
16291636
def caseRest(pat: Tree)(using Context) = {
16301637
val pt1 = instantiateMatchTypeProto(pat, pt) match {
16311638
case defn.MatchCase(_, bodyPt) => bodyPt
1639+
case MatchType.WildcardPattern(bodyPt) => bodyPt
16321640
case pt => pt
16331641
}
16341642
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)