Skip to content

Commit d57f91d

Browse files
committed
Enable exhaustivity and reachability checks for opaque types
1 parent c4c48e3 commit d57f91d

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ object SpaceEngine {
708708
else NoType
709709
}.filter(_.exists)
710710
parts
711+
case tref: TypeRef if tref.symbol.isOpaqueAlias && !tref.info.hiBound.isNothingType =>
712+
rec(tref.info.hiBound, mixins)
711713
case _ => ListOfNoType
712714
end rec
713715

@@ -853,7 +855,11 @@ object SpaceEngine {
853855
}) ||
854856
tpw.isRef(defn.BooleanClass) ||
855857
classSym.isAllOf(JavaEnum) ||
856-
classSym.is(Case)
858+
classSym.is(Case) ||
859+
(tpw.isInstanceOf[TypeRef] && {
860+
val tref = tpw.asInstanceOf[TypeRef]
861+
tref.symbol.isOpaqueAlias && !tref.info.hiBound.isNothingType
862+
})
857863

858864
!sel.tpe.hasAnnotation(defn.UncheckedAnnot)
859865
&& !sel.tpe.hasAnnotation(defn.RuntimeCheckedAnnot)

tests/pos/i23620.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
trait Foo
2+
trait Bar
3+
4+
type FooOrBar = FooOrBar.Type
5+
object FooOrBar:
6+
opaque type Type <: (Foo | Bar) = Foo | Bar
7+
8+
def bar: FooOrBar = new Bar {}
9+
10+
trait Buz
11+
12+
@main def main =
13+
val p: FooOrBar | Buz = FooOrBar.bar
14+
15+
p match
16+
case _: Foo => println("foo")
17+
case _: Buz => println("buz")
18+
case _: Bar => println("bar")

tests/warn/i23620b.check

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i23620b.scala:20:2 --------------------------------------------
2+
20 | p match // warn
3+
| ^
4+
| match may not be exhaustive.
5+
|
6+
| It would fail on pattern case: _: Bar
7+
|
8+
| longer explanation available when compiling with `-explain`
9+
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i23620b.scala:23:2 --------------------------------------------
10+
23 | p2 match { //warn
11+
| ^^
12+
| match may not be exhaustive.
13+
|
14+
| It would fail on pattern case: _: Bar
15+
|
16+
| longer explanation available when compiling with `-explain`

tests/warn/i23620b.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
trait Foo
2+
trait Bar
3+
4+
type FooOrBar = FooOrBar.Type
5+
object FooOrBar:
6+
opaque type Type <: (Foo | Bar) = Foo | Bar
7+
8+
def bar: FooOrBar = new Bar {}
9+
10+
type OnlyFoo = OnlyFoo.Type
11+
object OnlyFoo:
12+
opaque type Type <: (Foo | Bar) = Foo
13+
14+
def foo: OnlyFoo = new Foo {}
15+
16+
@main def main =
17+
val p: FooOrBar= FooOrBar.bar
18+
val p2: OnlyFoo = OnlyFoo.foo
19+
20+
p match // warn
21+
case _: Foo => println("foo")
22+
23+
p2 match { //warn
24+
case _: Foo => println("foo")
25+
}

0 commit comments

Comments
 (0)