Skip to content

Commit

Permalink
Hack to suppress bad labels in type mismatch inference deduction code
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jan 23, 2023
1 parent bed3bb5 commit 9f933b5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
36 changes: 20 additions & 16 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|| self.suggest_into(err, expr, expr_ty, expected)
|| self.suggest_floating_point_literal(err, expr, expected);
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);
}
}

Expand Down Expand Up @@ -222,6 +222,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr: &hir::Expr<'_>,
found: Ty<'tcx>,
expected: Ty<'tcx>,
mismatch_span: Span,
) -> bool {
let map = self.tcx.hir();

Expand Down Expand Up @@ -281,7 +282,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
},
};
let mut prev = eraser.fold_ty(ty);
let mut prev_span = None;
let mut prev_span: Option<Span> = None;

for binding in expr_finder.uses {
// In every expression where the binding is referenced, we will look at that
Expand Down Expand Up @@ -334,13 +335,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
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}`",
),
);
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 Expand Up @@ -384,12 +385,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&& self.can_eq(self.param_env, ty, found).is_ok()
{
// We only point at the first place where the found type was inferred.
if !segment.ident.span.overlaps(mismatch_span) {
err.span_label(
segment.ident.span,
with_forced_trimmed_paths!(format!(
"here the type of `{ident}` is inferred to be `{ty}`",
)),
);
);}
break;
} else if !param_args.is_empty() {
break;
Expand All @@ -408,12 +410,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// We use the *previous* span because if the type is known *here* it means
// it was *evaluated earlier*. We don't do this for method calls because we
// evaluate the method's self type eagerly, but not in any other case.
err.span_label(
span,
with_forced_trimmed_paths!(format!(
"here the type of `{ident}` is inferred to be `{ty}`",
)),
);
if !span.overlaps(mismatch_span) {
err.span_label(
span,
with_forced_trimmed_paths!(format!(
"here the type of `{ident}` is inferred to be `{ty}`",
)),
);
}
break;
}
prev = ty;
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
kind: TypeVariableOriginKind::MiscVariable,
span: full_call_span,
});
self.point_at_expr_source_of_inferred_type(&mut err, rcvr, expected, callee_ty);
self.point_at_expr_source_of_inferred_type(
&mut err,
rcvr,
expected,
callee_ty,
provided_arg_span,
);
}
// Call out where the function is defined
self.label_fn_like(
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/typeck/bad-type-in-vec-push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ fn main() {
//~^ ERROR mismatched types
// So it thinks that the type of `result` is constrained here.
}

fn example2() {
let mut x = vec![1];
x.push("");
//~^ ERROR mismatched types
}
19 changes: 14 additions & 5 deletions tests/ui/typeck/bad-type-in-vec-push.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ error[E0308]: mismatched types
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}>`
| ---- ^^^^^^ expected integer, found struct `Vec`
| |
| 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
error[E0308]: mismatched types
--> $DIR/bad-type-in-vec-push.rs:18:12
|
LL | x.push("");
| ---- ^^ expected integer, found `&str`
| |
| arguments to this method are incorrect
|
note: associated function defined here
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error: aborting due to 2 previous errors

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

0 comments on commit 9f933b5

Please sign in to comment.