Skip to content

Commit b56da91

Browse files
committed
Fix a bundle of patmat issues
Some of the issues are legitimate
1 parent 5994ea7 commit b56da91

File tree

13 files changed

+158
-12
lines changed

13 files changed

+158
-12
lines changed

Diff for: compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

+19-12
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,7 @@ object SpaceEngine {
528528
// force type inference to infer a narrower type: could be singleton
529529
// see tests/patmat/i4227.scala
530530
mt.paramInfos(0) <:< scrutineeTp
531-
instantiateSelected(mt, tvars)
532-
isFullyDefined(mt, ForceDegree.all)
531+
maximizeType(mt.paramInfos(0), Spans.NoSpan)
533532
mt
534533
}
535534

@@ -543,7 +542,7 @@ object SpaceEngine {
543542
// Case unapplySeq:
544543
// 1. return the type `List[T]` where `T` is the element type of the unapplySeq return type `Seq[T]`
545544

546-
val resTp = ctx.typeAssigner.safeSubstMethodParams(mt, scrutineeTp :: Nil).finalResultType
545+
val resTp = wildApprox(ctx.typeAssigner.safeSubstMethodParams(mt, scrutineeTp :: Nil).finalResultType)
547546

548547
val sig =
549548
if (resTp.isRef(defn.BooleanClass))
@@ -564,20 +563,14 @@ object SpaceEngine {
564563
if (arity > 0)
565564
productSelectorTypes(resTp, unappSym.srcPos)
566565
else {
567-
val getTp = resTp.select(nme.get).finalResultType match
568-
case tp: TermRef if !tp.isOverloaded =>
569-
// Like widenTermRefExpr, except not recursively.
570-
// For example, in i17184 widen Option[foo.type]#get
571-
// to Option[foo.type] instead of Option[Int].
572-
tp.underlying.widenExpr
573-
case tp => tp
566+
val getTp = extractorMemberType(resTp, nme.get, unappSym.srcPos)
574567
if (argLen == 1) getTp :: Nil
575568
else productSelectorTypes(getTp, unappSym.srcPos)
576569
}
577570
}
578571
}
579572

580-
sig.map(_.annotatedToRepeated)
573+
sig.map { case tp: WildcardType => tp.bounds.hi case tp => tp }
581574
}
582575

583576
/** Whether the extractor covers the given type */
@@ -623,7 +616,21 @@ object SpaceEngine {
623616
// For instance, from i15029, `decompose((X | Y).Field[T]) = [X.Field[T], Y.Field[T]]`.
624617
parts.map(tp.derivedAppliedType(_, targs))
625618

626-
case tp if tp.isDecomposableToChildren =>
619+
case tpOriginal if tpOriginal.isDecomposableToChildren =>
620+
// isDecomposableToChildren uses .classSymbol.is(Sealed)
621+
// But that classSymbol could be from an AppliedType
622+
// where the type constructor is a non-class type
623+
// E.g. t11620 where `?1.AA[X]` returns as "sealed"
624+
// but using that we're not going to infer A1[X] and A2[X]
625+
// but end up with A1[<?>] and A2[<?>].
626+
// So we widen (like AppliedType superType does) away
627+
// non-class type constructors.
628+
def getAppliedClass(tp: Type): Type = tp match
629+
case tp @ AppliedType(_: HKTypeLambda, _) => tp
630+
case tp @ AppliedType(tycon: TypeRef, _) if tycon.symbol.isClass => tp
631+
case tp @ AppliedType(tycon: TypeProxy, _) => getAppliedClass(tycon.superType.applyIfParameterized(tp.args))
632+
case tp => tp
633+
val tp = getAppliedClass(tpOriginal)
627634
def getChildren(sym: Symbol): List[Symbol] =
628635
sym.children.flatMap { child =>
629636
if child eq sym then List(sym) // i3145: sealed trait Baz, val x = new Baz {}, Baz.children returns Baz...

Diff for: tests/warn/i20121.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sealed trait T_A[A, B]
2+
type X = T_A[Byte, Byte]
3+
4+
case class CC_B[A](a: A) extends T_A[A, X]
5+
6+
val v_a: T_A[X, X] = CC_B(null)
7+
val v_b = v_a match
8+
case CC_B(_) => 0 // warn: unreachable
9+
case _ => 1
10+
// for CC_B[A] to match T_A[X, X]
11+
// A := X
12+
// so require X, aka T_A[Byte, Byte]
13+
// which isn't instantiable, outside of null

Diff for: tests/warn/i20122.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
sealed trait T_B[C, D]
2+
3+
case class CC_A()
4+
case class CC_B[A, C](a: A) extends T_B[C, CC_A]
5+
case class CC_C[C, D](a: T_B[C, D]) extends T_B[Int, CC_A]
6+
case class CC_E(a: CC_C[Char, Byte])
7+
8+
val v_a: T_B[Int, CC_A] = CC_B(CC_E(CC_C(null)))
9+
val v_b = v_a match
10+
case CC_B(CC_E(CC_C(_))) => 0 // warn: unreachable
11+
case _ => 1
12+
// for CC_B[A, C] to match T_B[C, CC_A]
13+
// C <: Int, ok
14+
// A <: CC_E, ok
15+
// but you need a CC_C[Char, Byte]
16+
// which requires a T_B[Char, Byte]
17+
// which isn't instantiable, outside of null

Diff for: tests/warn/i20123.scala

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sealed trait T_A[A, B]
2+
sealed trait T_B[C]
3+
4+
case class CC_D[A, C]() extends T_A[A, C]
5+
case class CC_E() extends T_B[Nothing]
6+
case class CC_G[A, C](c: C) extends T_A[A, C]
7+
8+
val v_a: T_A[Boolean, T_B[Boolean]] = CC_G(null)
9+
val v_b = v_a match {
10+
case CC_D() => 0
11+
case CC_G(_) => 1 // warn: unreachable
12+
// for CC_G[A, C] to match T_A[Boolean, T_B[Boolean]]
13+
// A := Boolean, which is ok
14+
// C := T_B[Boolean],
15+
// which isn't instantiable, outside of null
16+
}

Diff for: tests/warn/i20128.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sealed trait T_A[A]
2+
case class CC_B[A](a: T_A[A]) extends T_A[Byte]
3+
case class CC_E[A](b: T_A[A]) extends T_A[Byte]
4+
5+
val v_a: T_A[Byte] = CC_E(CC_B(null))
6+
val v_b: Int = v_a match { // warn: not exhaustive
7+
case CC_E(CC_E(_)) => 0
8+
case CC_B(_) => 1
9+
}

Diff for: tests/warn/i20129.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sealed trait T_A[A]
2+
case class CC_B[A](a: T_A[A], c: T_A[A]) extends T_A[Char]
3+
case class CC_C[A]() extends T_A[A]
4+
case class CC_G() extends T_A[Char]
5+
6+
val v_a: T_A[Char] = CC_B(CC_G(), CC_C())
7+
val v_b: Int = v_a match { // warn: not exhaustive
8+
case CC_C() => 0
9+
case CC_G() => 1
10+
case CC_B(CC_B(_, _), CC_C()) => 2
11+
case CC_B(CC_C(), CC_C()) => 3
12+
case CC_B(_, CC_G()) => 4
13+
case CC_B(_, CC_B(_, _)) => 5
14+
}

Diff for: tests/warn/i20130.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sealed trait T_A[B]
2+
sealed trait T_B[C]
3+
case class CC_B[C]() extends T_A[T_B[C]]
4+
case class CC_C[B, C](c: T_A[B], d: T_B[C]) extends T_B[C]
5+
case class CC_E[C]() extends T_B[C]
6+
7+
val v_a: T_B[Int] = CC_C(null, CC_E())
8+
val v_b: Int = v_a match { // warn: not exhaustive
9+
case CC_C(_, CC_C(_, _)) => 0
10+
case CC_E() => 5
11+
}

Diff for: tests/warn/i20131.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
sealed trait Foo
2+
case class Foo1() extends Foo
3+
case class Foo2[A, B]() extends Foo
4+
5+
sealed trait Bar[A, B]
6+
case class Bar1[A, C, D](a: Bar[C, D]) extends Bar[A, Bar[C, D]]
7+
case class Bar2[ C, D](b: Bar[C, D], c: Foo) extends Bar[Bar1[Int, Byte, Int], Bar[C, D]]
8+
9+
class Test:
10+
def m1(bar: Bar[Bar1[Int, Byte, Int], Bar[Char, Char]]): Int = bar match
11+
case Bar1(_) => 0
12+
case Bar2(_, Foo2()) => 1
13+
def t1 = m1(Bar2(null, Foo1()))
14+
// for Bar2[C, D] to match the scrutinee
15+
// C := Char and D := Char
16+
// which requires a Bar[Char, Char]
17+
// which isn't instantiable, outside of null

Diff for: tests/warn/i20132.alt.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sealed trait Foo[A]
2+
case class Bar[C](x: Foo[C]) extends Foo[C]
3+
case class End[B]() extends Foo[B]
4+
class Test:
5+
def m1[M](foo: Foo[M]): Int = foo match // warn: not exhaustive
6+
case End() => 0
7+
case Bar(End()) => 1
8+
def t1 = m1[Int](Bar[Int](Bar[Int](End[Int]())))

Diff for: tests/warn/i20132.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sealed trait Foo[A]
2+
case class Bar[C](x: Foo[C]) extends Foo[Int]
3+
case class End[B]() extends Foo[B]
4+
class Test:
5+
def m1[M](foo: Foo[M]): Int = foo match // warn: not exhaustive
6+
case End() => 0
7+
case Bar(End()) => 1
8+
def t1 = m1[Int](Bar[Int](Bar[Int](End[Int]())))

Diff for: tests/warn/i20132.wo.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sealed trait Foo[A]
2+
case class Bar[C](x: Foo[C]) extends Foo[Int]
3+
case class End[B]() extends Foo[B]
4+
class Test:
5+
def m1[M](foo: Foo[M]): Int = foo match
6+
case End() => 0
7+
case Bar(_) => 1
8+
def t1 = m1[Int](Bar[Int](Bar[Int](End[Int]())))

Diff for: tests/warn/i5422.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sealed trait Foo[A[_]]
2+
3+
case class Bar[C[_], X](x: C[X]) extends Foo[C]
4+
case class End[B[_]]() extends Foo[B]
5+
6+
class Test:
7+
def foo[M[_]](foo: Foo[M]): Int = foo match
8+
case End() => 0
9+
case Bar(_) => 1

Diff for: tests/warn/t11620.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sealed trait A[+T0]
2+
case class A1[+T1](t1: T1) extends A[T1]
3+
case class A2[+T2](t2: T2) extends A[T2]
4+
sealed trait B[+T3] { type AA[+U] <: A[U] ; def a: AA[T3] }
5+
object B { def unapply[T4](b: B[T4]): Some[b.AA[T4]] = Some(b.a) }
6+
class Test:
7+
def m1[X](b: B[X]): X = b match
8+
case B(A1(v1)) => v1
9+
case B(A2(v2)) => v2

0 commit comments

Comments
 (0)