-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceArea: Type inferenceD-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Code
struct Foo {
f: Option<i32>,
}
fn main() {
let v = vec![Foo { f: Some(1) }];
if let Some(x) = v.get(0_i16.into()) {
if let Some(f) = x.f {
println!("{}", f);
} else {
println!("not present");
}
} else {
println!("bad index");
}
}
Current output
error[E0282]: type annotations needed
--> src/main.rs:7:26
|
7 | if let Some(f) = x.f {
| ^^^ cannot infer type
For more information about this error, try `rustc --explain E0282`.
Desired output
if let Some(x) = v.get(0_i16.into()) {
---- type must be known at this point
Rationale and extra context
Changing the .into()
into an as usize
makes the code compile; see https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=495a6978e412895bf96d37d20efa8697
The access to x.f
is not the root cause of the problem, and should not be flagged as such.
Other cases
Rust Version
1.91.0-nightly (2025-09-01 7aef4bec4bec16cb6204)
reproduces on playground and locally:
rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: aarch64-apple-darwin
release: 1.89.0
LLVM version: 20.1.7
Anything else?
No response
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceArea: Type inferenceD-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.