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

Subpar diagnostics: unreachable_code lint fires before match arm coverage check #46426

Closed
pnkfelix opened this issue Dec 1, 2017 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Dec 1, 2017

When you do #![deny(unreachable_code)] (which is implied by #![deny(warnings)]), the corresponding lint runs and aborts compilation before the match arm coverage check gets a chance to report.

This leads to somewhat confusing messages.

  • at least, they are confusing depending on what the user's mental model is of how match expressions should hypothetically behave if they were not forced to cover the domain of the input expression completely

Example:

#![deny(unreachable_code)]

fn main() {
    let v = vec![1, 2, 3];
    let mut i = v.iter().peekable();
    'label: while let Some(e1) = i.next() {
        
        match e1 {
            &1 => {
                continue 'label;
            }
        }
        println!("e1: {:?}", e1);
    }
}

yields the error message (playpen)

error: unreachable statement
  --> src/main.rs:13:9
   |
13 |         println!("e1: {:?}", e1);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: lint level defined here
  --> src/main.rs:1:9
   |
1  | #![deny(unreachable_code)]
   |         ^^^^^^^^^^^^^^^^
   = note: this error originates in a macro outside of the current crate

error: aborting due to previous error

which, when I first look at the code, I think "that println! is clearly reachable", because in my head, execution is going to continue past the match expression after that first arm is not matched.

If I change the deny to warn, I get both the warning as above as well as an error about the non-exhaustive patterns in the match expression, saying that &_ is not covered.

@pnkfelix pnkfelix added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 1, 2017
@XAMPPRocky XAMPPRocky added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Feb 26, 2018
@estebank
Copy link
Contributor

Current output:

error: unreachable statement
  --> src/main.rs:13:9
   |
8  | /         match e1 {
9  | |             &1 => {
10 | |                 continue 'label;
11 | |             }
12 | |         }
   | |_________- any code following this `match` expression is unreachable, as all arms diverge
13 |           println!("e1: {:?}", e1);
   |           ^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
   |
note: lint level defined here
  --> src/main.rs:1:9
   |
1  | #![deny(unreachable_code)]
   |         ^^^^^^^^^^^^^^^^
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

@cjgillot
Copy link
Contributor

cjgillot commented Mar 5, 2022

Current output:

error: unreachable statement
  --> src/main.rs:13:9
   |
8  | /         match e1 {
9  | |             &1 => {
10 | |                 continue 'label;
11 | |             }
12 | |         }
   | |_________- any code following this `match` expression is unreachable, as all arms diverge
13 |           println!("e1: {:?}", e1);
   |           ^^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
   |
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![deny(unreachable_code)]
   |         ^^^^^^^^^^^^^^^^
   = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0004]: non-exhaustive patterns: `&i32::MIN..=0_i32` and `&2_i32..=i32::MAX` not covered
 --> src/main.rs:8:15
  |
8 |         match e1 {
  |               ^^ patterns `&i32::MIN..=0_i32` and `&2_i32..=i32::MAX` not covered
  |
  = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
  = note: the matched value is of type `&i32`

@cjgillot cjgillot closed this as completed Mar 5, 2022
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 C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants