# Trouble with 2.13.4 exhaustivity checking being too strict

**URL:** <https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817>\
**Category:** Language Design\
**Created:** [January 16, 2021, 2:04pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817 "2021-01-16T14:04:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghik](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ghik/32/1954_2.png) [@ghik](https://contributors.scala-lang.org/u/ghik)\
**Post date:** [January 16, 2021, 2:04pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/1 "2021-01-16T14:04:10Z")

</div>

Hi there,

I’m trying to migrate some of our company’s projects to Scala 2.13.4 and I’m hitting some problems with the new [exhaustivity checking](https://github.com/scala/scala/pull/9140) algorithm. While this is obviously a very good change, it becomes a problem in some cases where the compiler cannot infer that a type is effectively sealed.

For example, we are using [`Opt`](https://github.com/AVSystem/scala-commons/blob/master/commons-core/src/main/scala/com/avsystem/commons/misc/Opt.scala) - a value class version of `Option`. Its usage looks like this:

```scala
val someString: Opt[String] = Opt("foo")
val noneString: Opt[String] = Opt.Empty

someString match {
  case Opt(str) => println(s"some: $str")
  case Opt.Empty => println("none")
}

```

Unfortunately, the compiler doesn’t know that `Opt.apply` and `Opt.Empty` are the only possible cases and thinks that this pattern match is non exhaustive. Since `Opt` is an unsealed type (as seen by the compiler), I can work around that problem by opting out of `strict-unsealed-patmat`.

However, the problem resurfaces when `Opt` is wrapped into a sealed type, e.g. `Try`:

```scala
val tryOpt: Try[Opt[String]] = Success(Opt("foo"))

tryOpt match {
  case Success(Opt(str)) => println(s"some $str")
  case Success(Opt.Empty) => println("none")
  case Failure(cause) => cause.printStackTrace()
}

```

Even with the `strict-unsealed-patmat` turned off, the compiler complains that:

```auto
match may not be exhaustive.
It would fail on the following input: Success((x: com.avsystem.commons.misc.Opt[?] forSome x not in Empty))

```

Another example is with our enum implementation, [`ValueEnum`](https://github.com/AVSystem/scala-commons/blob/master/commons-core/src/main/scala/com/avsystem/commons/misc/ValueEnum.scala):

```scala
final class Weekday(implicit enumCtx: EnumCtx) extends AbstractValueEnum
object Weekday extends AbstractValueEnumCompanion[Weekday] {
  final val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday: Value = new Weekday
}

val tryWeekday: Try[Weekday] = Success(Weekday.Monday)

tryWeekday match {
  case Success(Weekday.Monday | Weekday.Tuesday | Weekday.Wednesday) => println("first half")
  case Success(Weekday.Thursday) => println("middle")
  case Success(Weekday.Friday | Weekday.Saturday | Weekday.Sunday) => println("second half")
  case Failure(cause) => cause.printStackTrace()
}

```

The compiler has no idea that `Monday`, `Tuesday`, etc. are _all_ possible values for `Weekday` - this is guaranteed by the `ValueEnum` machinery. So this time I get this warning:

```auto
match may not be exhaustive.
It would fail on the following input: Success((x: Weekday forSome x not in (Friday, Monday, Saturday, Sunday, Thursday, Tuesday, Wednesday)))

```

What are the possible solutions? There are some that come to my mind:

1. Opting out of `strict-unsealed-patmat` should also work when the unsealed type is wrapped in a sealed type (like `Try[Weekday]`)
2. There should be a way to explicitly exempt some types from exhaustivity checking, e.g. with an annotation.
3. There should be a way to tell the compiler about possible values and constructors of an _effectively sealed_ type. Even if this was as low level as some API available to compiler plugins, I would be happy.

The no. 3 option would be absolutely the best because this would allow full integration of types like `Opt` into the new exhaustivity checking algorithm.

---

<div class="post-metadata">

**Author:** ![viktorklang](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/viktorklang/32/722_2.png) [@viktorklang](https://contributors.scala-lang.org/u/viktorklang)\
**Post date:** [January 16, 2021, 3:59pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/2 "2021-01-16T15:59:11Z")

</div>

Doesn’t ”@unchecked” work?–

---

<div class="post-metadata">

**Author:** ![ghik](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ghik/32/1954_2.png) [@ghik](https://contributors.scala-lang.org/u/ghik)\
**Post date:** [January 16, 2021, 4:21pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/3 "2021-01-16T16:21:18Z")

</div>

I does but I don’t consider it a solution. In case of `Opt`, I’d ideally like to use it as a drop-in replacement for `Option`, without additional ceremony.

Also, I don’t want to spark a discussion about `Opt` itself and whether its worth/justified to introduce types like this. It’s just an example of a more general problem.

---

<div class="post-metadata">

**Author:** ![viktorklang](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/viktorklang/32/722_2.png) [@viktorklang](https://contributors.scala-lang.org/u/viktorklang)\
**Post date:** [January 16, 2021, 5:25pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/4 "2021-01-16T17:25:39Z")

</div>

In this case, dropping in an unsealed type in place of a sealed type will not be a drop-in replacement.–

---

<div class="post-metadata">

**Author:** ![ghik](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ghik/32/1954_2.png) [@ghik](https://contributors.scala-lang.org/u/ghik)\
**Post date:** [January 16, 2021, 5:55pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/5 "2021-01-16T17:55:10Z")

</div>

Proper exhaustivity checking is the only feature that it’s missing as compared to `Option`. With Scala 2.13.4 this became a bigger problem because lack of warnings has turned into _false_ warnings. As a result, I can’t comfortably upgrade to 2.13.4.

For these reasons, I proposed solutions that would both solve my immediate problem and would potentially be useful in general. I want to know if there is some more general interest in what I proposed or at least if someone can help me with the immediate trouble that I face. If not, I will have no choice but to ditch types like `Opt`. This is a last resort for me and I would be really unhappy if I had to do it because it’s a lot of work and saying goodbye to some nice features that I’ve been relying on for the last few years.

---

<div class="post-metadata">

**Author:** ![som-snytt](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/som-snytt/32/595_2.png) [@som-snytt](https://contributors.scala-lang.org/u/som-snytt)\
**Post date:** [January 17, 2021, 3:03am UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/6 "2021-01-17T03:03:08Z")

</div>

`-Wconf` is the mechanism to “silence” rando warnings. It can match a warning by message, category, site.

`scalac -Wconf:help` is the new one-question FAQ.

---

<div class="post-metadata">

**Author:** ![ghik](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ghik/32/1954_2.png) [@ghik](https://contributors.scala-lang.org/u/ghik)\
**Post date:** [January 17, 2021, 10:57am UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/7 "2021-01-17T10:57:45Z")

</div>

Thanks, ~~that might indeed be sufficient with `Opt`~~ but unfortunately not with enums because they are created left and right and can’t all be covered with a single message pattern.

EDIT: I spoke too soon. I have cases where the warning message related to `Opt` looks like this:

```auto
match may not be exhaustive.
It would fail on the following input: Success((_, _))

```

How do I suppress this without suppressing practically every exhaustive checking warning?

---

<div class="post-metadata">

**Author:** ![martijnhoekstra](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/martijnhoekstra/32/608_2.png) [@martijnhoekstra](https://contributors.scala-lang.org/u/martijnhoekstra)\
**Post date:** [January 17, 2021, 1:20pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/8 "2021-01-17T13:20:39Z")

</div>

Unsealed patmat is a red herring: `Opt` is not unsealed at all. In fact, it’s final. The problem is that the extractor `Opt.unapply` can’t be proven to be exhaustive in combination with `Opt.Empty`.

To get the compiler to assume your guards and extractors are exhaustive like it was before, you can get the old behaviour where the compiler assumes guards and extractors are true back by setting `-Xnon-strict-patmat-analysis`.

---

<div class="post-metadata">

**Author:** ![ghik](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ghik/32/1954_2.png) [@ghik](https://contributors.scala-lang.org/u/ghik)\
**Post date:** [January 17, 2021, 1:52pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/9 "2021-01-17T13:52:40Z")

</div>

If it’s not unsealed then why disabling `strict-unsealed-patmat` suppresses the warning in the code below?

```auto
val someString: Opt[String] = Opt("foo")
someString match {
  case Opt(str) =>
  case Opt.Empty =>
}

```

Thanks for the `-Xnon-strict-patmat-analysis` option, it should be enough for now but it’s sad that I have to disable the new algorithm entirely.

---

<div class="post-metadata">

**Author:** ![ghik](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ghik/32/1954_2.png) [@ghik](https://contributors.scala-lang.org/u/ghik)\
**Post date:** [January 17, 2021, 2:07pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/10 "2021-01-17T14:07:35Z")

</div>

Looking at the [implementation](https://github.com/scala/scala/blob/2.13.x/src/compiler/scala/tools/nsc/transform/patmat/MatchAnalysis.scala#L510), the compiler does not consider final classes sealed.

I filed an [issue](https://github.com/scala/bug/issues/12314) related to this discussion.

---

<div class="post-metadata">

**Author:** ![martijnhoekstra](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/martijnhoekstra/32/608_2.png) [@martijnhoekstra](https://contributors.scala-lang.org/u/martijnhoekstra)\
**Post date:** [January 17, 2021, 2:23pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/11 "2021-01-17T14:23:40Z")

</div>

That’s a good question. It totally shouldn’t.

---

<div class="post-metadata">

**Author:** ![dwijnand](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/dwijnand/32/796_2.png) [@dwijnand](https://contributors.scala-lang.org/u/dwijnand)\
**Post date:** [January 17, 2021, 4:09pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/12 "2021-01-17T16:09:28Z")

</div>

Then it starts warning for other classes like String and Int…

---

<div class="post-metadata">

**Author:** ![martijnhoekstra](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/martijnhoekstra/32/608_2.png) [@martijnhoekstra](https://contributors.scala-lang.org/u/martijnhoekstra)\
**Post date:** [January 17, 2021, 4:14pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/13 "2021-01-17T16:14:00Z")

</div>

It should warn for mutually exhaustive extractors on Int or on String.

A hypothetical

```auto
(i: Int) match {
  case Zero => ...
  case NonZero(nz) => ...
}

```

Should not be affected by unsealed-patmat either, should it? As far as I can see, that’s entirely equivalent.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jasper-m/32/1192_2.png) [@Jasper-M](https://contributors.scala-lang.org/u/Jasper-M)\
**Post date:** [January 17, 2021, 7:15pm UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/14 "2021-01-17T19:15:17Z")

</div>

I think it would definitely be valuable if there was a mechanism to indicate to the compiler that a set of extractors together are exhaustive. I.e. `Opt` and `Opt.Empty`, or `Zero` and `NonZero`.

---

<div class="post-metadata">

**Author:** ![liufengyun](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/liufengyun/32/1169_2.png) [@liufengyun](https://contributors.scala-lang.org/u/liufengyun)\
**Post date:** [January 26, 2021, 9:57am UTC](https://contributors.scala-lang.org/t/trouble-with-2-13-4-exhaustivity-checking-being-too-strict/4817/15 "2021-01-26T09:57:51Z")

</div>

I made a proposal [#11186](https://github.com/lampepfl/dotty/pull/11186), which might potentially solve the problem.
