question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spurious non-exhaustivity warning

See original GitHub issue

Compiler version

3.2.0 with -Ycheck-all-patmat

Minimized code

sealed trait Nat
case class Zero() extends Nat
case class Succ[N <: Nat](n: N) extends Nat

type PredOrZeroOption[N <: Nat] <: Option[Nat] = N match
  case Zero => None.type
  case Succ[predN] => Some[predN]

trait A[N <: Nat]:
  def foo: PredOrZeroOption[N]

object A:
  def unapply[N <: Nat](a: A[N]): PredOrZeroOption[N] = a.foo

class B[PredN <: Nat](n: PredN) extends A[Succ[PredN]]:
  override def foo: Some[PredN] = Some(n)

@main def main(): Unit =
  val a = B(Zero())
  a.foo match
    case Some(pred) => println(pred)
  a match
    case A(pred) => println(pred)

Output

match may not be exhaustive.

It would fail on pattern case: _: B[Zero]

Expectation

No warning.

Note there is no warning for the a.foo match, which is equivalent.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Bersiercommented, Sep 16, 2022

@dwijnand Thank you! Makes sense.

One thing that might improve upon the current state of affairs would be to have two different types of warnings:

  1. when the compiler is sure that the match is not exhaustive
  2. when the compiler is not sure whether the match is exhaustive

The latter warnings could be shown only when somehow enabled.

P.S.: The latter warning could also be split further into two sub-cases:

  1. The compiler knows it will never be able to prove whether the match is exhaustive, because of non-sealed types or because it’s uncomputable.
  2. The compiler doesn’t know whether it’s provable, and a future version of the compiler might know.
0reactions
dwijnandcommented, Sep 16, 2022

@dwijnand So are all flags mentioned here unreliable and not to be used? I use a lot of them:

Yeah, that’s what private means…

What are the alternatives?

Mostly there aren’t. Things like explicit nulls and safe-init will become on by default. But debug pos I don’t even understand why you use it, but I also don’t know what it does. But it sounds like an always private flag. In general they need a ticket, a reason and an effort to become stable, non-private feature or on by default.

For this flag specifically: how can I get a guarantee that all my matches are exhaustive otherwise?

There’s no (public) feature for that. But I would also want it, so I would back a proposal to add that. I’m fairly certain we had a flag like that in Scala 2.

Even if it’s not a bug for Scala 3 in general, isn’t it still a bug for this flag that should be tracked somewhere? It says in the document that it’s “used for testing the algorithm”. Doesn’t this spurious non-exhaustivity warning indicate some issue?

Sure, sort of. For some code we don’t check for exhaustivity, it doesn’t seem to calculate the right thing. It’s not worth keeping a ticket open for that - IMO.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spurious 'non-exhaustive patterns' error · Issue #15080 · rust-lang ...
It looks like #14731 introduced a regression. The following used to work: fn main() { let mut x = &[1, 2, 3, 4];...
Read more >
Spurious non-exhaustive pattern match warnings are given ...
Spurious non-exhaustive pattern match warnings are given using GADTs ; Resolution, Unresolved ; Component, Compiler ; Test case ; Differential ...
Read more >
rust match in match: "spurious" non exhaustive error on simple ...
I understand this error, and I know how to "brutally" fix it (similarly to Non-exhaustive patterns - Rust match expressions by adding a:...
Read more >
Trouble with 2.13.4 exhaustivity checking being too strict
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 ......
Read more >
GADTs meet their match (Extended Version) - KU Leuven
day's compilers generate bogus warnings when the programmer ... it is non-empty, the clauses are not exhaustive, and a warning should be generated....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found