Skip to content

Custom unapply methods can produce misleading error messages #3004

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
abgruszecki opened this issue Aug 21, 2017 · 1 comment
Closed

Custom unapply methods can produce misleading error messages #3004

abgruszecki opened this issue Aug 21, 2017 · 1 comment

Comments

@abgruszecki
Copy link
Contributor

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

@liufengyun
Copy link
Contributor

Thanks for the diagnosis @AleksanderBG !

@liufengyun liufengyun self-assigned this Aug 21, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 23, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 23, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 25, 2017
liufengyun added a commit that referenced this issue Aug 25, 2017
Fix #3004 and simplify exhaustivity check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants