Skip to content

Incorrect "match may not be exhaustive" warning matching on Seq with -Xlint:strict-unsealed-patmat #12754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
timw opened this issue Mar 16, 2023 · 2 comments

Comments

@timw
Copy link

timw commented Mar 16, 2023

Reproduction steps

Scala version: 2.13.10

Compile with -Xlint:strict-unsealed-patmat

object SeqMatchTest {

  def main(args: Array[String]): Unit = {

    def test(seq: Seq[_]): Unit = seq match {
      case Seq()  => println(s"$seq is empty")
      case Seq(_, _ @_*) => println(s"$seq has one or more elements")
//      case _      => println(s"$seq is never matched")
    }

    test(Seq())
    test(Seq(1))
    test(Seq(1, 2))
    test(Seq(1, 2, 3))
  }
}

Problem

When compiled with no lints, the above code compiles with no warnings and runs correctly.
When compiled with -Xlint:strict-unsealed-patmat it produces the warning:

match may not be exhaustive.
It would fail on the following input: (x: Seq[?] forSome x not in Nil)
def test(seq: Seq[_]): Unit = seq match {

Using case _ +: _ exhibits the same problem. Uncommenting the wildcard case allows compilation with no warnings.

This seems incorrect, in that it's evident when run that all the possible Seq cases are matched.
The same test case using List with the same patterns also fails, but classic List cons patterns do not exhibit the problem.

@timw
Copy link
Author

timw commented Mar 16, 2023

On reflection, I might be expecting too much of the compiler here, as it probably lacks enough information to know that these cases are exhaustive - the warning is "may not be exhaustive" after all, but the fact that there's a case that seems to obviously match the proposed failure pattern seems slightly jarring.

@som-snytt
Copy link

IIUC, #12240 is why case Seq(_@_*) => correctly does not warn, and this ticket is a duplicate of #12252

There is discussion via linked tickets.

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants