Skip to content

Extend E0308 inference blame span machinery to detect vec.push(val) as a cause #106355

Closed
@estebank

Description

@estebank

Given the following code:

fn bar(_: Vec<i32>) {}
fn main() {
    let v: Vec<i32> = vec![1, 2, 3, 4, 5];
    let mut foo = vec![];
    for i in &v {
        foo.push(i);
    }
    bar(foo);
}

The current output is:

error[E0308]: mismatched types
 --> src/main.rs:8:9
  |
8 |     bar(foo);
  |     --- ^^^ expected `i32`, found `&i32`
  |     |
  |     arguments to this function are incorrect
  |
  = note: expected struct `Vec<i32>`
             found struct `Vec<&i32>`
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn bar(_: Vec<i32>) {}
  |    ^^^ -----------

The error should point at line 6, where the type of foo is resolved to be Vec<&i32>, along the lines of

error[E0308]: mismatched types
 --> src/main.rs:8:9
  |
8 |     bar(foo);
  |     --- ^^^ expected `i32`, found `&i32`
  |     |
  |     arguments to this function are incorrect
  |
  = note: expected struct `Vec<i32>`
             found struct `Vec<&i32>`
note: 
  |
4 |     let mut foo = vec![];
  |             --- `foo` is of type `Vec<_>` here
5 |     for i in &v {
6 |         foo.push(i);
  |         ^^^      - `i` is of type `&i32` here
  |         |
  |         `foo` is of type `Vec<&i32>` here
  |
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn bar(_: Vec<i32>) {}
  |    ^^^ -----------

https://hachyderm.io/@jameydev/109615629656869242

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions