error[E0282]: type annotations needed but should be inferred automatically
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Hi, I tried to search for a similar issue but couldn't find one. I hope I haven't missed any. Though this issue seems quite obvious. Thanks a lot for your time and work!
I tried this code: (minimal reproducible)
#[derive(Debug,Default)]
struct Something {
value: i32,
}
impl Something {
pub fn new() -> Self {
let mut something = des();
something.value = 100; // comment out and everything works just fine
something
}
}
fn des<T: Default>() -> T {
T::default()
}
fn main() {
let deserialized = Something::new();
println!("deserialized = {:?}", deserialized);
}
I expected to see this happen: infer the type automatically and compile successfully, like rust-analyser
Instead, this happened:
error[E0282]: type annotations needed
--> src/main.rs:10:13
|
10 | let mut something = des();
| ^^^^^^^^^^^^^
11 | something.value = 100; // comment out and everything works just fine
| --------- type must be known at this point
|
help: consider giving `something` an explicit type
|
10 | let mut something: /* Type */ = des();
| ++++++++++++
For more information about this error, try `rustc --explain E0282`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Meta
rustc --version --verbose:
rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: aarch64-apple-darwin
release: 1.85.1
LLVM version: 19.1.7
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 reproducing the minimal example with rustc 1.85.1 and compare the behavior when the field assignment is present or removed. Trace the compiler's handling of E0282 and type inference, then verify the intended behavior with a focused regression test for this example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100