Skip to content
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

Explicit match on uninhabited type shouldn't warn #55116

Closed
RalfJung opened this issue Oct 16, 2018 · 2 comments
Closed

Explicit match on uninhabited type shouldn't warn #55116

RalfJung opened this issue Oct 16, 2018 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@RalfJung
Copy link
Member

So say I have a function that takes an uninhabited type (common e.g. when implementing a trait for !). I would like to make it explicit that I am using "ex falso quod libet" reasoning, so I write

fn foo(x: !) -> bool {
  match x {}
}

Unfortunately, now Rust warns:

warning: unreachable expression
 --> src/lib.rs:4:3
  |
4 |   match x {}
  |   ^^^^^^^^^^
  |
  = note: #[warn(unreachable_code)] on by default

It seems it wants me to instead write

fn foo(x: !) -> bool {
    x
}

I think that's bad style, now I have to think much harder to figure out what is going on ("oh, there's a coercion from uninhabited types to bool"). The original code I wrote uses a construct which only works for uninhabited types, explicitly documenting intent.

I think the compiler should not print a warning for that code. Empty matches are a useful, explicit and constructive way of saying that we are dealing with uninhabited data, we shouldn't discourage them in favor of implicit coercions that require way more context to comprehend.

@RalfJung RalfJung changed the title Explicit match on uninhabited typre shouldn't warn Explicit match on uninhabited type shouldn't warn Oct 16, 2018
@nikomatsakis
Copy link
Contributor

(I agree)

@estebank estebank added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-diagnostics Area: Messages for errors, warnings, and lints labels Oct 16, 2018
@estebank
Copy link
Contributor

CC #55123

bors added a commit that referenced this issue Oct 20, 2018
Allow explicit matches on ! without warning

It's now possible to explicitly match on `!` without an unreachable code warning. This seems desirable as promoting explicitness.

Fixes #55116.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

3 participants