rust-lang / rust-lang/rust

Expected/Found does not point to point where type parameter was inferred

Open
#134,091 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
fn foo<'a, T>(b: &'a T) -> &'a T { b }

fn main() {
    let f = foo;
    
    f(&String::new());
    f(&Vec::new());
}
Current output
error[E0308]: mismatched types
 --> src/main.rs:7:7
  |
7 |     f(&Vec::new());
  |     - ^^^^^^^^^^^ expected `&String`, found `&Vec<_>`
  |     |
  |     arguments to this function are incorrect
  |
  = note: expected reference `&String`
             found reference `&Vec<_>`
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn foo<'a, T>(b: &'a T) -> &'a T { b }
  |    ^^^        --------
Desired output
error[E0308]: mismatched types
 --> src/main.rs:7:7
  |
7 |     f(&Vec::new());
  |     - ^^^^^^^^^^^ expected `&String`, found `&Vec<_>`
  |     |
  |     arguments to this function are incorrect
  |
  = note: expected reference `&String`
             found reference `&Vec<_>`
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn foo<'a, T>(b: &'a T) -> &'a T { b }
  |    ^^^        --------
note: generic argument was inferred to `String` here
 --> src/main.rs:6:7
  |
6 | f(&String::new());
  |    ^^^^^^^^^^^^^
Rationale and extra context

No response

Other cases

Rust Version
playground version:

Build using the Nightly version: 1.85.0-nightly
(2024-12-08 4d669fb34e7db6f3825d)
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

Reproduce the diagnostic using the example in src/main.rs and the listed nightly Rust version. Compare the current and desired output, focusing on the missing inference note and its source location. Done means the diagnostic points to the call where String was inferred as the generic argument.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.