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.

False “match may not be exhaustive warning”

See original GitHub issue

Minimized code

def f(xs: List[Int]) =
  xs match
    case init :+ last => ()
    case Nil => ()

@main def run = f(List(1, 2))

https://scastie.scala-lang.org/NVdH00JGS36HLzTs7F8vhw

Output

[warn] 60 |    case init :+ last =>
[warn]    |    ^
[warn]    |    match may not be exhaustive.
[warn]    |
[warn]    |    It would fail on pattern case: List(_, _: _*)
[warn] one warning found

Expectation

There is no warning.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
liufengyuncommented, Jan 4, 2021

@dwijnand Dotty currently has some support for irrefutable unapplySeq:

object SeqExtractor {
  def unapplySeq(x: Int): Some[List[Int]] = Some(List(1))
}

@main
def Test = (1: String | Int)  match {
 case _: String              => "hello"
 case SeqExtractor()         => "zero"
 case SeqExtractor(_)        => "one"
 case SeqExtractor(_, _, _*) => "two or more"
}

https://scastie.scala-lang.org/H6VaTJyeSVuvZ7Y8kz4Qag

1reaction
liufengyuncommented, Jan 2, 2021

But in this case we have two extractors which are both refutable but together cover the scrutinee type completely. How can we express that?

It can be specified like the following (with the hypothetical @irrefutableFor[T]):

type ParamClause = List[ValDef] | List[TypeDef]

object ValDefs:
  @irrefutableFor[::[ValDef] | Nil.type]
  def unapply(pc: ParamClause): Option[List[ValDef]] = ... // matches empty list and all lists of ValDefs

object TypeDefs:
  @irrefutableFor[::[TypeDef]]
  def unapply(pc: ParamClause): Option[List[TypeDef]] = ... // matches non-empty lists of TypeDefs

Or, lie a little bit:

type ParamClause = List[ValDef] | List[TypeDef]

object ValDefs:
  @irrefutableFor[List[ValDef]]
  def unapply(pc: ParamClause): Option[List[ValDef]] = ... // matches empty list and all lists of ValDefs

object TypeDefs:
  @irrefutableFor[List[TypeDef]]
  def unapply(pc: ParamClause): Option[List[TypeDef]] = ... // matches non-empty lists of TypeDefs
Read more comments on GitHub >

github_iconTop Results From Across the Web

Scala warning match may not be exhaustive - Stack Overflow
Match may not be exhaustive warning is incorrect · 2 · pattern match weird behavior with scala 2.13.4 · Hot Network Questions.
Read more >
False "match may not be exhaustive" warning in pattern ...
False "match may not be exhaustive" warning in pattern matching #12377 ... It's interesting to see that Scala 3 also (falsely) warns, ...
Read more >
[Solved]-Warning Match may not be exhaustive-scala
Why does Scala 2.10 give 'match may not be exhaustive' warning when matching on singleton types? ... How should I handle 'match may...
Read more >
Scala Enumerations hell. Motivation | by Yurii Gorbylov | Medium
... but if you want to use scala.Enumeration in pattern matching, you won't get a “match may not be exhaustive” warning: ...
Read more >
User danielnixon - Stack Exchange
Match may not be exhaustive warning is incorrect · stackoverflow.com · 5 · How to get sbt to stop trying to pull latest...
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