rust-lang / rust-lang/rust

error[E0282]: type annotations needed but should be inferred automatically

Open
#139,168 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference C-enhancement T-compiler T-types
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);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=59137d08e7fcf67e6379022640676ebe

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.