Skip to content

Warning "match may not be exhaustive" with Queue() and EmptyQueue on pattern matching with a Queue #12697

@azarzadavila

Description

@azarzadavila

Reproduction steps

Scala version: 2.13.10

import scala.collection.immutable.Queue

object Main {
  def main(args: Array[String]) = {
    val x: Queue[Int] = Queue()
    x match {
      case _ :+ _ => println("something")
      case Queue() => println("empty")
    }
  }
}

Problem

At compilation with sbt, the following warning appears:

match may not be exhaustive.
[warn] It would fail on the following inputs: EmptyQueue, Queue()
[warn]     x match {
[warn]     ^

I would expect no warning as the matching should be complete having tested both the non-empty and empty Queue. I tested the execution with both val x: Queue[Int] = Queue() and val: Queue[Int] = Queue.empty[Int] and both work fine.

Activity

azarzadavila

azarzadavila commented on Nov 30, 2022

@azarzadavila
Author

This is probably linked with #12252

som-snytt

som-snytt commented on Nov 30, 2022

@som-snytt

collection.:+ has unapply that returns Option[(C, A)] for (init, last).

It is refutable. The question is whether to use "special knowledge" about this extractor when emitting warnings because it is so visible.

I wonder if there is a FAQ about Seq-extraction.

som-snytt

som-snytt commented on Nov 30, 2022

@som-snytt

Maybe they need irrefutable :+_! ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @SethTisue@dwijnand@som-snytt@azarzadavila

        Issue actions

          Warning "match may not be exhaustive" with Queue() and EmptyQueue on pattern matching with a Queue · Issue #12697 · scala/bug