Skip to content

Type inference error involving inference from a break; reports an incorrect line number in the diagnostic #141788

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

Open
gbbosak opened this issue May 30, 2025 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gbbosak
Copy link
Contributor

gbbosak commented May 30, 2025

Code

#[tokio::main]
async fn main() {
    let res = async{
        loop {
            if "".to_string() == "" {
                break;
            }
            return Some(5);
        }
    };
   
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:8:20
  |
8 |             return Some(5);
  |                    ^^^^^^^ expected `()`, found `Option<{integer}>`
  |
  = note: expected unit type `()`
                  found enum `Option<{integer}>`
note: return type inferred to be `()` here
 --> src/main.rs:8:20
  |
8 |             return Some(5);
  |                    ^^^^^^^

For more information about this error, try `rustc --explain E0308`.

Desired output

error[E0308]: mismatched types
 --> src/main.rs:8:20
  |
8 |             return Some(5);
  |                    ^^^^^^^ expected `()`, found `Option<{integer}>`
  |
  = note: expected unit type `()`
                  found enum `Option<{integer}>`
note: return type inferred to be `()` here
 --> src/main.rs:9:9
  |
9 |             }
  |             ^

Return type of loop inferred to be `()` here
  --> src/main.rs:6:17
   |
17 |.      break;
   |.      ^^^^^^
For more information about this error, try `rustc --explain E0308`.

Rationale and extra context

The diagnostic makes it look like the conflicting line (8) in this case, is the same line that it conflicts with (also 8).

Other cases

Rust Version

rustc --version --verbose
rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: aarch64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Anything else?

No response

@gbbosak gbbosak added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 30, 2025
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 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

1 participant