Skip to content

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

Open
@gbbosak

Description

@gbbosak

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions