Skip to content

Commit 9c29d78

Browse files
Fix scala#9171: treat _ as _:Any in isMatchTypeShaped
1 parent 7b8b0d2 commit 9c29d78

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
@@ -4734,6 +4734,14 @@ object Types {
47344734
object MatchType {
47354735
def apply(bound: Type, scrutinee: Type, cases: List[Type])(using Context): MatchType =
47364736
unique(new CachedMatchType(bound, scrutinee, cases))
4737+
4738+
/** Extractor for `case _ =>` match type patterns */
4739+
object WildcardPattern {
4740+
def unapply(tp: Type)(using Context): Option[Type] = tp match {
4741+
case HKTypeLambda(LambdaParam(tl1, 0) :: Nil, defn.MatchCase(TypeParamRef(tl2, 0), bodyTp)) => Some(bodyTp)
4742+
case _ => None
4743+
}
4744+
}
47374745
}
47384746

47394747
// ------ 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
@@ -2616,7 +2616,6 @@ object Parsers {
26162616
case USCORE if in.lookahead.isArrow =>
26172617
val start = in.skipToken()
26182618
typeBounds().withSpan(Span(start, in.lastOffset, start))
2619-
26202619
case _ =>
26212620
infixType()
26222621
}

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)