rustc flags the wrong line for an ambiguous `.into`
Open
Nobody has claimed this yet.
A-diagnostics
A-inference
D-confusing
D-terse
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
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
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
Reproduce the diagnostic from src/main.rs using the supplied example and compare it with the output from the Rust Playground or rustc versions listed. Trace how the compiler chooses the span for E0282; done means the ambiguous .into() expression is highlighted instead of x.f, with the expected type annotation guidance.
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
- 42/100