Skip to content

Commit 314c190

Browse files
Fix scala#9171: treat _ as _:Any in isMatchTypeShaped
1 parent 0f127da commit 314c190

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
@@ -4630,6 +4630,14 @@ object Types {
46304630
object MatchType {
46314631
def apply(bound: Type, scrutinee: Type, cases: List[Type])(using Context): MatchType =
46324632
unique(new CachedMatchType(bound, scrutinee, cases))
4633+
4634+
/** Extractor for `case _ =>` match type patterns */
4635+
object WildcardPattern {
4636+
def unapply(tp: Type)(using Context): Option[Type] = tp match {
4637+
case HKTypeLambda(LambdaParam(tl1, 0) :: Nil, defn.MatchCase(TypeParamRef(tl2, 0), bodyTp)) => Some(bodyTp)
4638+
case _ => None
4639+
}
4640+
}
46334641
}
46344642

46354643
// ------ 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
@@ -2603,7 +2603,6 @@ object Parsers {
26032603
case USCORE if in.lookahead.isArrow =>
26042604
val start = in.skipToken()
26052605
typeBounds().withSpan(Span(start, in.lastOffset, start))
2606-
26072606
case _ =>
26082607
infixType()
26092608
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,13 @@ class Typer extends Namer
14991499
val Typed(_, tpt) = tpd.unbind(tpd.unsplice(pat1))
15001500
instantiateMatchTypeProto(pat1, pt) match {
15011501
case defn.MatchCase(patternTp, _) => tpt.tpe frozen_=:= patternTp
1502+
case MatchType.WildcardPattern(_) => tpt.tpe frozen_=:= defn.AnyType
1503+
case _ => false
1504+
}
1505+
case (id @ Ident(nme.WILDCARD), pt) =>
1506+
pt match {
1507+
case defn.MatchCase(patternTp, _) => defn.AnyType frozen_=:= patternTp
1508+
case MatchType.WildcardPattern(_) => true
15021509
case _ => false
15031510
}
15041511
case _ => false
@@ -1610,6 +1617,7 @@ class Typer extends Namer
16101617
def caseRest(pat: Tree)(using Context) = {
16111618
val pt1 = instantiateMatchTypeProto(pat, pt) match {
16121619
case defn.MatchCase(_, bodyPt) => bodyPt
1620+
case MatchType.WildcardPattern(bodyPt) => bodyPt
16131621
case pt => pt
16141622
}
16151623
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)