Type inference error involving inference from a break; reports an incorrect line number in the diagnostic
Open
Nobody has claimed this yet.
A-diagnostics
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by compiling the provided Rust reproducer with rustc 1.87.0 and compare the current diagnostic with the desired output. Trace how the loop's break and inferred return type determine the diagnostic spans, then add coverage for this case and verify that the conflict points to the break and loop locations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100