rust-lang / rust-lang/rust

Suggest not using method call syntax when type can't be inferred

Open
#118,804 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-inference A-suggestion-diagnostics D-papercut T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct S<T> {
    f: fn(T) -> T,
}

fn main() {
    let mut n = 0i32;
    let s = S {
        f: |x| x.clone(),
    };
    n = (s.f)(n);
}
Current output
error[E0282]: type annotations needed
 --> src/main.rs:8:13
  |
8 |         f: |x| x.clone(),
  |             ^  - type must be known at this point
  |
help: consider giving this closure parameter an explicit type
  |
8 |         f: |x: /* Type */| x.clone(),
  |              ++++++++++++
Desired output
error[E0282]: type annotations needed
 --> src/main.rs:8:13
  |
8 |         f: |x| x.clone(),
  |             ^  - type must be known at this point
  |
help: consider giving this closure parameter an explicit type
  |
8 |         f: |x: /* Type */| x.clone(),
  |              ++++++++++++
  |
help: consider calling the function directly
  |
8 |         f: |x| Clone::clone(&x),
Rationale and extra context

Calling the function directly gets rid of the "type annotations needed" error

Other cases

No response

Anything else?

No response

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 E0282 diagnostic with the Rust code in the issue and compare the current output with the desired output. Trace the compiler diagnostic handling for this inference failure; done means the additional suggestion to call the function directly appears without changing the existing help.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.