Skip to content

Commit 8fed4e7

Browse files
Fix #9171: treat _ as _:Any in isMatchTypeShaped
1 parent a8f1587 commit 8fed4e7

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4586,6 +4586,14 @@ object Types {
45864586
object MatchType {
45874587
def apply(bound: Type, scrutinee: Type, cases: List[Type])(using Context): MatchType =
45884588
unique(new CachedMatchType(bound, scrutinee, cases))
4589+
4590+
/** Extractor for `case _ =>` match type patterns */
4591+
object WildcardPattern {
4592+
def unapply(tp: Type)(using Context): Option[Type] = tp match {
4593+
case HKTypeLambda(LambdaParam(tl1, 0) :: Nil, defn.MatchCase(TypeParamRef(tl2, 0), bodyTp)) => Some(bodyTp)
4594+
case _ => None
4595+
}
4596+
}
45894597
}
45904598

45914599
// ------ ClassInfo, Type Bounds --------------------------------------------------

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,9 +2581,8 @@ object Parsers {
25812581
accept(CASE)
25822582
in.token match {
25832583
case USCORE if in.lookahead.isArrow =>
2584-
val start = in.skipToken()
2585-
typeBounds().withSpan(Span(start, in.lastOffset, start))
2586-
2584+
in.skipToken()
2585+
scalaAny
25872586
case _ =>
25882587
infixType()
25892588
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,13 @@ class Typer extends Namer
14821482
val Typed(_, tpt) = tpd.unbind(tpd.unsplice(pat1))
14831483
instantiateMatchTypeProto(pat1, pt) match {
14841484
case defn.MatchCase(patternTp, _) => tpt.tpe frozen_=:= patternTp
1485+
case MatchType.WildcardPattern(_) => tpt.tpe frozen_=:= defn.AnyType
1486+
case _ => false
1487+
}
1488+
case (id @ Ident(nme.WILDCARD), pt) =>
1489+
pt match {
1490+
case defn.MatchCase(patternTp, _) => defn.AnyType frozen_=:= patternTp
1491+
case MatchType.WildcardPattern(_) => true
14851492
case _ => false
14861493
}
14871494
case _ => false
@@ -1593,6 +1600,7 @@ class Typer extends Namer
15931600
def caseRest(pat: Tree)(using Context) = {
15941601
val pt1 = instantiateMatchTypeProto(pat, pt) match {
15951602
case defn.MatchCase(_, bodyPt) => bodyPt
1603+
case MatchType.WildcardPattern(bodyPt) => bodyPt
15961604
case pt => pt
15971605
}
15981606
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)