Skip to content

Commit

Permalink
Don't resolve type var roots in point_at_expr_source_of_inferred_type
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jan 23, 2023
1 parent c8e6a9e commit bed3bb5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
lt_op: |_| self.tcx.lifetimes.re_erased,
ct_op: |c| c,
ty_op: |t| match *t.kind() {
ty::Infer(ty::TyVar(vid)) => self.tcx.mk_ty_infer(ty::TyVar(self.root_var(vid))),
ty::Infer(ty::TyVar(_)) => self.tcx.mk_ty_var(ty::TyVid::from_u32(0)),
ty::Infer(ty::IntVar(_)) => {
self.tcx.mk_ty_infer(ty::IntVar(ty::IntVid { index: 0 }))
}
Expand Down Expand Up @@ -333,13 +333,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// inferred in this method call.
let arg = &args[i];
let arg_ty = self.node_ty(arg.hir_id);
if !arg.span.overlaps(mismatch_span) {
err.span_label(
arg.span,
&format!(
"this is of type `{arg_ty}`, which causes `{ident}` to be \
inferred as `{ty}`",
),
);
}
param_args.insert(param_ty, (arg, arg_ty));
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/typeck/bad-type-in-vec-push.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// The error message here still is pretty confusing.

fn main() {
let mut result = vec![1];
// The type of `result` is constrained to be `Vec<{integer}>` here.
// But the logic we use to find what expression constrains a type
// is not sophisticated enough to know this.

let mut vector = Vec::new();
vector.sort();
result.push(vector);
//~^ ERROR mismatched types
// So it thinks that the type of `result` is constrained here.
}
20 changes: 20 additions & 0 deletions tests/ui/typeck/bad-type-in-vec-push.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0308]: mismatched types
--> $DIR/bad-type-in-vec-push.rs:11:17
|
LL | vector.sort();
| ------ here the type of `vector` is inferred to be `Vec<_>`
LL | result.push(vector);
| ---- ^^^^^^
| | |
| | expected integer, found struct `Vec`
| | this is of type `Vec<_>`, which causes `result` to be inferred as `Vec<{integer}>`
| arguments to this method are incorrect
|
= note: expected type `{integer}`
found struct `Vec<_>`
note: associated function defined here
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit bed3bb5

Please sign in to comment.