Skip to content

Commit bed3bb5

Browse files
Don't resolve type var roots in point_at_expr_source_of_inferred_type
1 parent c8e6a9e commit bed3bb5

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
270270
lt_op: |_| self.tcx.lifetimes.re_erased,
271271
ct_op: |c| c,
272272
ty_op: |t| match *t.kind() {
273-
ty::Infer(ty::TyVar(vid)) => self.tcx.mk_ty_infer(ty::TyVar(self.root_var(vid))),
273+
ty::Infer(ty::TyVar(_)) => self.tcx.mk_ty_var(ty::TyVid::from_u32(0)),
274274
ty::Infer(ty::IntVar(_)) => {
275275
self.tcx.mk_ty_infer(ty::IntVar(ty::IntVid { index: 0 }))
276276
}
@@ -333,13 +333,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
333333
// inferred in this method call.
334334
let arg = &args[i];
335335
let arg_ty = self.node_ty(arg.hir_id);
336+
if !arg.span.overlaps(mismatch_span) {
336337
err.span_label(
337338
arg.span,
338339
&format!(
339340
"this is of type `{arg_ty}`, which causes `{ident}` to be \
340341
inferred as `{ty}`",
341342
),
342343
);
344+
}
343345
param_args.insert(param_ty, (arg, arg_ty));
344346
}
345347
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// The error message here still is pretty confusing.
2+
3+
fn main() {
4+
let mut result = vec![1];
5+
// The type of `result` is constrained to be `Vec<{integer}>` here.
6+
// But the logic we use to find what expression constrains a type
7+
// is not sophisticated enough to know this.
8+
9+
let mut vector = Vec::new();
10+
vector.sort();
11+
result.push(vector);
12+
//~^ ERROR mismatched types
13+
// So it thinks that the type of `result` is constrained here.
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/bad-type-in-vec-push.rs:11:17
3+
|
4+
LL | vector.sort();
5+
| ------ here the type of `vector` is inferred to be `Vec<_>`
6+
LL | result.push(vector);
7+
| ---- ^^^^^^
8+
| | |
9+
| | expected integer, found struct `Vec`
10+
| | this is of type `Vec<_>`, which causes `result` to be inferred as `Vec<{integer}>`
11+
| arguments to this method are incorrect
12+
|
13+
= note: expected type `{integer}`
14+
found struct `Vec<_>`
15+
note: associated function defined here
16+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)