Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
estebank opened this issue Jan 1, 2023 · 0 comments · Fixed by #106400
Closed

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

estebank opened this issue Jan 1, 2023 · 0 comments · Fixed by #106400
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jan 1, 2023

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

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 1, 2023
estebank added a commit to estebank/rust that referenced this issue Jan 5, 2023
compiler-errors added a commit to compiler-errors/rust that referenced this issue Jan 5, 2023
Point at expressions where inference refines an unexpected type

Fix rust-lang#106355. Fix rust-lang#14007. (!)

```
error[E0308]: mismatched types
  --> src/test/ui/type/type-check/point-at-inference.rs:12:9
   |
9  |         foo.push(i);
   |                  - this is of type `&{integer}`, which makes `foo` to be inferred as `Vec<&{integer}>`
...
12 |     bar(foo);
   |     --- ^^^ expected `i32`, found `&{integer}`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected struct `Vec<i32>`
              found struct `Vec<&{integer}>`
note: function defined here
  --> src/test/ui/type/type-check/point-at-inference.rs:2:4
   |
2  | fn bar(_: Vec<i32>) {}
   |    ^^^ -----------
help: consider dereferencing the borrow
   |
9  |         foo.push(*i);
   |                  +
```
estebank added a commit to estebank/rust that referenced this issue Jan 5, 2023
@bors bors closed this as completed in 6ae0f08 Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant