Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ object SpaceEngine {
else NoType
}.filter(_.exists)
parts
case tref: TypeRef if tref.isUpperBoundedAbstract =>
rec(tref.info.hiBound, mixins)
case _ => ListOfNoType
end rec

Expand All @@ -702,6 +704,10 @@ object SpaceEngine {
&& !cls.hasAnonymousChild // can't name anonymous classes as counter-examples
&& cls.children.nonEmpty // can't decompose without children

extension (tref: TypeRef)
def isUpperBoundedAbstract(using Context): Boolean =
tref.symbol.isAbstractOrAliasType && !tref.info.hiBound.isNothingType

val ListOfNoType = List(NoType)
val ListOfTypNoType = ListOfNoType.map(Typ(_, decomposed = true))

Expand Down Expand Up @@ -826,7 +832,11 @@ object SpaceEngine {
classSym.is(Case) && {
if seen.add(classSym) then productSelectorTypes(tpw, sel.srcPos).exists(isCheckable(_))
else true // recursive case class: return true and other members can still fail the check
}
} ||
(tpw.isInstanceOf[TypeRef] && {
val tref = tpw.asInstanceOf[TypeRef]
tref.isUpperBoundedAbstract && isCheckable(tref.info.hiBound)
})

!sel.tpe.hasAnnotation(defn.UncheckedAnnot)
&& {
Expand Down
18 changes: 18 additions & 0 deletions tests/pos/i23620.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
trait Foo
trait Bar

type FooOrBar = FooOrBar.Type
object FooOrBar:
opaque type Type <: (Foo | Bar) = Foo | Bar

def bar: FooOrBar = new Bar {}

trait Buz

@main def main =
val p: FooOrBar | Buz = FooOrBar.bar

p match
case _: Foo => println("foo")
case _: Buz => println("buz")
case _: Bar => println("bar")
24 changes: 24 additions & 0 deletions tests/warn/i23620b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i23620b.scala:20:2 --------------------------------------------
20 | p match // warn
| ^
| match may not be exhaustive.
|
| It would fail on pattern case: _: Bar
|
| longer explanation available when compiling with `-explain`
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i23620b.scala:23:2 --------------------------------------------
23 | p2 match { // warn
| ^^
| match may not be exhaustive.
|
| It would fail on pattern case: _: Bar
|
| longer explanation available when compiling with `-explain`
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i23620b.scala:37:2 --------------------------------------------
37 | x match // warn
| ^
| match may not be exhaustive.
|
| It would fail on pattern case: B
|
| longer explanation available when compiling with `-explain`
38 changes: 38 additions & 0 deletions tests/warn/i23620b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
trait Foo
trait Bar

type FooOrBar = FooOrBar.Type
object FooOrBar:
opaque type Type <: (Foo | Bar) = Foo | Bar

def bar: FooOrBar = new Bar {}

type OnlyFoo = OnlyFoo.Type
object OnlyFoo:
opaque type Type <: (Foo | Bar) = Foo

def foo: OnlyFoo = new Foo {}

@main def main =
val p: FooOrBar= FooOrBar.bar
val p2: OnlyFoo = OnlyFoo.foo

p match // warn
case _: Foo => println("foo")

p2 match { // warn
case _: Foo => println("foo")
}

sealed trait S
trait Z

case object A extends S, Z
case object B extends S, Z

trait HasT:
type T <: S & Z

def nonExhaustive(h: HasT, x: h.T) =
x match // warn
case A => ()
8 changes: 8 additions & 0 deletions tests/warn/i24246.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i24246.scala:8:2 ----------------------------------------------
8 | x match { // warn
| ^
| match may not be exhaustive.
|
| It would fail on pattern case: ZZ
|
| longer explanation available when compiling with `-explain`
10 changes: 10 additions & 0 deletions tests/warn/i24246.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
trait X

sealed trait Y
case object YY extends Y, X
case object ZZ extends Y, X

def foo[A <: X & Y](x: A): Unit =
x match { // warn
case YY => ()
}
Loading