Skip to content

Custom unapply methods can produce misleading error messages #3004

Closed
@abgruszecki

Description

@abgruszecki

As a simple example, the following code:

object O {
         sealed trait Fruit
         object Apple extends Fruit
         object Banana extends Fruit
         sealed class C(f1: Fruit, f2: Fruit)
         object C {
           def unapply(c: C): Some[Banana.type] = Some(Banana)
         }
         def m(c: C) = c match { case C(b) => b }
}

produces the following message:

-- [E028] Pattern Match Exhaustivity Warning: <console>:12:16 ------------------
12 |  def m(c: C) = c match { case C(b) => b }
   |                ^
   |                match may not be exhaustive.
   |
   |                It would fail on: C(Apple, _)

The pattern match is, in fact, exhaustive and the message is somewhat puzzling.

The underlying issue is the interaction between this code in the project method:
https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala#L430-L431
and the places in rest of the code which "project" Typ(tp) as Kon(tp, signature(tp).map(Typ(_)):
https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala#L267
When a Kon space resulting from projecting C.unapply is subtracted from Typ(C), "inconsistent" Kon spaces are created, which then in turn causes this issue.

/cc @liufengyun

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions