Skip to content

Commit b17c259

Browse files
Fix #9171: treat _ as _:Any in isMatchTypeShaped
1 parent 0fa10c6 commit b17c259

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
@@ -4650,6 +4650,14 @@ object Types {
46504650
object MatchType {
46514651
def apply(bound: Type, scrutinee: Type, cases: List[Type])(using Context): MatchType =
46524652
unique(new CachedMatchType(bound, scrutinee, cases))
4653+
4654+
/** Extractor for `case _ =>` match type patterns */
4655+
object WildcardPattern {
4656+
def unapply(tp: Type)(using Context): Option[Type] = tp match {
4657+
case HKTypeLambda(LambdaParam(tl1, 0) :: Nil, defn.MatchCase(TypeParamRef(tl2, 0), bodyTp)) => Some(bodyTp)
4658+
case _ => None
4659+
}
4660+
}
46534661
}
46544662

46554663
// ------ 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
@@ -2614,7 +2614,6 @@ object Parsers {
26142614
case USCORE if in.lookahead.isArrow =>
26152615
val start = in.skipToken()
26162616
typeBounds().withSpan(Span(start, in.lastOffset, start))
2617-
26182617
case _ =>
26192618
infixType()
26202619
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,13 @@ class Typer extends Namer
15031503
val Typed(_, tpt) = tpd.unbind(tpd.unsplice(pat1))
15041504
instantiateMatchTypeProto(pat1, pt) match {
15051505
case defn.MatchCase(patternTp, _) => tpt.tpe frozen_=:= patternTp
1506+
case MatchType.WildcardPattern(_) => tpt.tpe frozen_=:= defn.AnyType
1507+
case _ => false
1508+
}
1509+
case (id @ Ident(nme.WILDCARD), pt) =>
1510+
pt match {
1511+
case defn.MatchCase(patternTp, _) => defn.AnyType frozen_=:= patternTp
1512+
case MatchType.WildcardPattern(_) => true
15061513
case _ => false
15071514
}
15081515
case _ => false
@@ -1614,6 +1621,7 @@ class Typer extends Namer
16141621
def caseRest(pat: Tree)(using Context) = {
16151622
val pt1 = instantiateMatchTypeProto(pat, pt) match {
16161623
case defn.MatchCase(_, bodyPt) => bodyPt
1624+
case MatchType.WildcardPattern(bodyPt) => bodyPt
16171625
case pt => pt
16181626
}
16191627
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)