Skip to content

Commit 9f933b5

Browse files
Hack to suppress bad labels in type mismatch inference deduction code
1 parent bed3bb5 commit 9f933b5

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6161
|| self.suggest_into(err, expr, expr_ty, expected)
6262
|| self.suggest_floating_point_literal(err, expr, expected);
6363
if !suggested {
64-
self.point_at_expr_source_of_inferred_type(err, expr, expr_ty, expected);
64+
self.point_at_expr_source_of_inferred_type(err, expr, expr_ty, expected, expr.span);
6565
}
6666
}
6767

@@ -222,6 +222,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
222222
expr: &hir::Expr<'_>,
223223
found: Ty<'tcx>,
224224
expected: Ty<'tcx>,
225+
mismatch_span: Span,
225226
) -> bool {
226227
let map = self.tcx.hir();
227228

@@ -281,7 +282,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
281282
},
282283
};
283284
let mut prev = eraser.fold_ty(ty);
284-
let mut prev_span = None;
285+
let mut prev_span: Option<Span> = None;
285286

286287
for binding in expr_finder.uses {
287288
// In every expression where the binding is referenced, we will look at that
@@ -334,13 +335,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
334335
let arg = &args[i];
335336
let arg_ty = self.node_ty(arg.hir_id);
336337
if !arg.span.overlaps(mismatch_span) {
337-
err.span_label(
338-
arg.span,
339-
&format!(
340-
"this is of type `{arg_ty}`, which causes `{ident}` to be \
341-
inferred as `{ty}`",
342-
),
343-
);
338+
err.span_label(
339+
arg.span,
340+
&format!(
341+
"this is of type `{arg_ty}`, which causes `{ident}` to be \
342+
inferred as `{ty}`",
343+
),
344+
);
344345
}
345346
param_args.insert(param_ty, (arg, arg_ty));
346347
}
@@ -384,12 +385,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
384385
&& self.can_eq(self.param_env, ty, found).is_ok()
385386
{
386387
// We only point at the first place where the found type was inferred.
388+
if !segment.ident.span.overlaps(mismatch_span) {
387389
err.span_label(
388390
segment.ident.span,
389391
with_forced_trimmed_paths!(format!(
390392
"here the type of `{ident}` is inferred to be `{ty}`",
391393
)),
392-
);
394+
);}
393395
break;
394396
} else if !param_args.is_empty() {
395397
break;
@@ -408,12 +410,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
408410
// We use the *previous* span because if the type is known *here* it means
409411
// it was *evaluated earlier*. We don't do this for method calls because we
410412
// evaluate the method's self type eagerly, but not in any other case.
411-
err.span_label(
412-
span,
413-
with_forced_trimmed_paths!(format!(
414-
"here the type of `{ident}` is inferred to be `{ty}`",
415-
)),
416-
);
413+
if !span.overlaps(mismatch_span) {
414+
err.span_label(
415+
span,
416+
with_forced_trimmed_paths!(format!(
417+
"here the type of `{ident}` is inferred to be `{ty}`",
418+
)),
419+
);
420+
}
417421
break;
418422
}
419423
prev = ty;

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
808808
kind: TypeVariableOriginKind::MiscVariable,
809809
span: full_call_span,
810810
});
811-
self.point_at_expr_source_of_inferred_type(&mut err, rcvr, expected, callee_ty);
811+
self.point_at_expr_source_of_inferred_type(
812+
&mut err,
813+
rcvr,
814+
expected,
815+
callee_ty,
816+
provided_arg_span,
817+
);
812818
}
813819
// Call out where the function is defined
814820
self.label_fn_like(

tests/ui/typeck/bad-type-in-vec-push.rs

+6
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ fn main() {
1212
//~^ ERROR mismatched types
1313
// So it thinks that the type of `result` is constrained here.
1414
}
15+
16+
fn example2() {
17+
let mut x = vec![1];
18+
x.push("");
19+
//~^ ERROR mismatched types
20+
}

tests/ui/typeck/bad-type-in-vec-push.stderr

+14-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@ error[E0308]: mismatched types
44
LL | vector.sort();
55
| ------ here the type of `vector` is inferred to be `Vec<_>`
66
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}>`
7+
| ---- ^^^^^^ expected integer, found struct `Vec`
8+
| |
119
| arguments to this method are incorrect
1210
|
1311
= note: expected type `{integer}`
1412
found struct `Vec<_>`
1513
note: associated function defined here
1614
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
1715

18-
error: aborting due to previous error
16+
error[E0308]: mismatched types
17+
--> $DIR/bad-type-in-vec-push.rs:18:12
18+
|
19+
LL | x.push("");
20+
| ---- ^^ expected integer, found `&str`
21+
| |
22+
| arguments to this method are incorrect
23+
|
24+
note: associated function defined here
25+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
26+
27+
error: aborting due to 2 previous errors
1928

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

0 commit comments

Comments
 (0)