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

Misleading compile error message when checking match arm types. #78124

Closed
supermank17 opened this issue Oct 20, 2020 · 0 comments · Fixed by #114819
Closed

Misleading compile error message when checking match arm types. #78124

supermank17 opened this issue Oct 20, 2020 · 0 comments · Fixed by #114819
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@supermank17
Copy link

supermank17 commented Oct 20, 2020

I ran into a case where the compiler is complaining that match arms have incompatible types, as seen in this code snippet below.
However, the line it is pointing to is not necessarily incorrect: the real issue is that the outer "shouldwe" Some => match-arm is not returning a u32 as expected. (In other words, this can be made to compile by adding a return after the match shouldwe2 statement).
I eventually realized the actual issue, but this could possibly be clarified by pointing at the outer shouldwe match instead of the inner shouldwe2 match.

#![allow(unused)]

fn test(shouldwe: Option<u32>, shouldwe2: Option<u32>) -> u32 {
    match shouldwe {
        Some(val) => {
            match shouldwe2 {
                Some(val) => {
                    return val;
                }
                None => (),
            }
        }
        None => return 12,
    }
}

fn main() {
    println!("returned {}", test(None, Some(5)));
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0308]: `match` arms have incompatible types
  --> src/main.rs:10:25
   |
6  | /             match shouldwe2 {
7  | |                 Some(val) => {
8  | |                     return val;
   | |                     ----------- this is found to be of type `u32`
9  | |                 }
10 | |                 None => (),
   | |                         ^^ expected `u32`, found `()`
11 | |             }
   | |_____________- `match` arms have incompatible types

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 20, 2020
@JohnTitor JohnTitor added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Oct 20, 2020
@estebank estebank added the D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. label Aug 3, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 15, 2023
…rrors

Point at return type when it influences non-first `match` arm

When encountering code like

```rust
fn foo() -> i32 {
    match 0 {
        1 => return 0,
        2 => "",
        _ => 1,
    }
}
```

Point at the return type and not at the prior arm, as that arm has type `!` which isn't influencing the arm corresponding to arm `2`.

Fix rust-lang#78124.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 15, 2023
…rrors

Point at return type when it influences non-first `match` arm

When encountering code like

```rust
fn foo() -> i32 {
    match 0 {
        1 => return 0,
        2 => "",
        _ => 1,
    }
}
```

Point at the return type and not at the prior arm, as that arm has type `!` which isn't influencing the arm corresponding to arm `2`.

Fix rust-lang#78124.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 15, 2023
…rrors

Point at return type when it influences non-first `match` arm

When encountering code like

```rust
fn foo() -> i32 {
    match 0 {
        1 => return 0,
        2 => "",
        _ => 1,
    }
}
```

Point at the return type and not at the prior arm, as that arm has type `!` which isn't influencing the arm corresponding to arm `2`.

Fix rust-lang#78124.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 15, 2023
…rrors

Point at return type when it influences non-first `match` arm

When encountering code like

```rust
fn foo() -> i32 {
    match 0 {
        1 => return 0,
        2 => "",
        _ => 1,
    }
}
```

Point at the return type and not at the prior arm, as that arm has type `!` which isn't influencing the arm corresponding to arm `2`.

Fix rust-lang#78124.
@bors bors closed this as completed in 55f8c66 Aug 15, 2023
flip1995 pushed a commit to flip1995/rust that referenced this issue Aug 24, 2023
…rrors

Point at return type when it influences non-first `match` arm

When encountering code like

```rust
fn foo() -> i32 {
    match 0 {
        1 => return 0,
        2 => "",
        _ => 1,
    }
}
```

Point at the return type and not at the prior arm, as that arm has type `!` which isn't influencing the arm corresponding to arm `2`.

Fix rust-lang#78124.
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-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants