Skip to content

non-exhaustive patterns: type ! is non-empty #36011

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Sh4rK opened this issue Aug 26, 2016 · 7 comments
Closed

non-exhaustive patterns: type ! is non-empty #36011

Sh4rK opened this issue Aug 26, 2016 · 7 comments

Comments

@Sh4rK
Copy link
Contributor

Sh4rK commented Aug 26, 2016

Hi!

I'm trying out feature(never_type) and getting an error on the simplest example possible.
Rustc is rustc 1.13.0-nightly (3c5a0fa45 2016-08-22) on x86_64-pc-windows-msvc.

The code:

#![feature(never_type)]

fn main() {
    let never: !;
    match never {};
}

And the error I get:

error[E0002]: non-exhaustive patterns: type ! is non-empty
 --> src\main.rs:5:5
  |
5 |     match never {};
  |     ^^^^^^^^^^^^^^
  |
help: Please ensure that all possible cases are being handled; possibly adding wildcards or more match arms.
 --> src\main.rs:5:5
  |
5 |     match never {};
  |     ^^^^^^^^^^^^^^

Which is quite bad, as the point of ! is that it is empty.

If I replace it with an empty enum, it works. (It errors on the uninitialized variable, but that's not the point.)

@Sh4rK
Copy link
Contributor Author

Sh4rK commented Aug 26, 2016

Related to #35162 (feature(never_type))

@Sh4rK
Copy link
Contributor Author

Sh4rK commented Aug 27, 2016

I'm guessing @canndrew or @eddyb might know what's going on?

@canndrew
Copy link
Contributor

This function here is the one that checks whether a type is empty but it currently only works on empty enums. I tried to extend it to also handle ! (and all other empty types) but @eddyb and @nikomatsakis were worried about that change because the function was originally only written with empty enums in mind. They know a lot more about the compiler than I do but I hope they reconsider (or at least clarify what the potential breakages might be) because ! is kinda broken without it, plus, as it says in the comments, I've tested that change and it seems to work (or at least it doesn't break the compiler test suites).

@Sh4rK
Copy link
Contributor Author

Sh4rK commented Aug 27, 2016

That seems a bit unfortunate, but I understand it might not be a trivial change.

So to safely unwrap a Result<T, !> for example, currently I should do something like this, or is there a better way?

match res {
    Ok(v) => v,
    Err(e) => e,
}

@canndrew
Copy link
Contributor

Yes, that's how you have to do it.

@Mark-Simulacrum
Copy link
Member

I'm not sure whether it's me or the code, but I cannot reproduce this, since I can't figure out a way to get ! in a variable without having either unreachable code or the uninitialized variable warning, which both mask (if it even is still a problem) the error this issue reports, so closing.

error[E0381]: use of possibly uninitialized variable: `never`
 --> test.rs:5:11
  |
5 |     match never {};
  |           ^^^^^ use of possibly uninitialized `never`

error: aborting due to previous error

@glaebhoerl
Copy link
Contributor

@Mark-Simulacrum A way to do it is fn foo(never: !) instead of a let. But the bug does seem to be fixed.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants