]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #107175 - compiler-errors:bad-types-in-vec-push, r=estebank
authorMatthias Krüger <matthias.krueger@famsik.de>
Thu, 26 Jan 2023 05:15:26 +0000 (06:15 +0100)
committerGitHub <noreply@github.com>
Thu, 26 Jan 2023 05:15:26 +0000 (06:15 +0100)
Fix escaping inference var ICE in `point_at_expr_source_of_inferred_type`

Fixes #107158

`point_at_expr_source_of_inferred_type` uses `lookup_probe` to adjust the self type of a method receiver -- but that method returns inference variables from inside a probe. That means that the ty vars are no longer valid, so we can't use any infcx methods on them.

Also, pass some extra span info to hack a quick solution to bad labels, resulting in this diagnostic improvement:

```rust
fn example2() {
    let mut x = vec![1];
    x.push("");
}
```

```diff
  error[E0308]: mismatched types
   --> src/main.rs:5:12
    |
  5 |     x.push("");
    |       ---- ^^
    |       |    |
    |       |    expected integer, found `&str`
-   |       |    this is of type `&'static str`, which causes `x` to be inferred as `Vec<{integer}>`
    |       arguments to this method are incorrect
```
(since that "which causes `x` to be inferred as `Vec<{integer}>` part is wrong)

r? `@estebank`

(we really should make this code better in general, cc #106590, but that's a bit bigger issue that needs some more thinking about)

1  2 
compiler/rustc_hir_typeck/src/demand.rs
compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

index 7379e75963f532b5d6642b27d2b025b6cb739b30,8e23ded3c09b4bf7ab024c3b279254c7960c811b..a7b6a5c0331fc9722e126952c4198d309c27e86c
@@@ -59,10 -59,9 +59,10 @@@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> 
              || self.suggest_copied_or_cloned(err, expr, expr_ty, expected)
              || self.suggest_clone_for_ref(err, expr, expr_ty, expected)
              || self.suggest_into(err, expr, expr_ty, expected)
 -            || self.suggest_floating_point_literal(err, expr, expected);
 +            || self.suggest_floating_point_literal(err, expr, expected)
 +            || self.note_result_coercion(err, expr, expected, expr_ty);
          if !suggested {
-             self.point_at_expr_source_of_inferred_type(err, expr, expr_ty, expected);
+             self.point_at_expr_source_of_inferred_type(err, expr, expr_ty, expected, expr.span);
          }
      }