mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 17:24:06 +00:00
Properly note source of arg mismatch
This commit is contained in:
parent
29aee6a125
commit
5a71029dd3
@ -62,7 +62,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
|| self.suggest_coercing_result_via_try_operator(err, expr, expected, expr_ty);
|
||||
|
||||
if !suggested {
|
||||
self.note_source_of_type_mismatch_constraint(err, expr, expected);
|
||||
self.note_source_of_type_mismatch_constraint(
|
||||
err,
|
||||
expr,
|
||||
TypeMismatchSource::Ty(expected),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -222,7 +226,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
&self,
|
||||
err: &mut Diagnostic,
|
||||
expr: &hir::Expr<'_>,
|
||||
expected_ty: Ty<'tcx>,
|
||||
source: TypeMismatchSource<'tcx>,
|
||||
) -> bool {
|
||||
let hir = self.tcx.hir();
|
||||
|
||||
@ -295,6 +299,46 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
},
|
||||
};
|
||||
|
||||
let expected_ty = match source {
|
||||
TypeMismatchSource::Ty(expected_ty) => expected_ty,
|
||||
TypeMismatchSource::Arg(call_expr, idx) => {
|
||||
let hir::ExprKind::MethodCall(segment, _, args, _) = call_expr.kind else {
|
||||
return false;
|
||||
};
|
||||
let Some(arg_ty) = self.node_ty_opt(args[idx].hir_id) else {
|
||||
return false;
|
||||
};
|
||||
let possible_rcvr_ty = expr_finder.uses.iter().find_map(|binding| {
|
||||
let possible_rcvr_ty = self.node_ty_opt(binding.hir_id)?;
|
||||
let possible_rcvr_ty = possible_rcvr_ty.fold_with(&mut fudger);
|
||||
let method = self
|
||||
.lookup_method(
|
||||
possible_rcvr_ty,
|
||||
segment,
|
||||
DUMMY_SP,
|
||||
call_expr,
|
||||
binding,
|
||||
args,
|
||||
)
|
||||
.ok()?;
|
||||
let _ = self
|
||||
.at(&ObligationCause::dummy(), self.param_env)
|
||||
.eq(DefineOpaqueTypes::No, method.sig.inputs()[idx + 1], arg_ty)
|
||||
.ok()?;
|
||||
self.select_obligations_where_possible(|errs| {
|
||||
// Yeet the errors, we're already reporting errors.
|
||||
errs.clear();
|
||||
});
|
||||
Some(self.resolve_vars_if_possible(possible_rcvr_ty))
|
||||
});
|
||||
if let Some(rcvr_ty) = possible_rcvr_ty {
|
||||
rcvr_ty
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if !self.can_eq(self.param_env, expected_ty, init_ty.fold_with(&mut fudger)) {
|
||||
return false;
|
||||
}
|
||||
@ -360,7 +404,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
"... which constrains `{ident}` to have type `{next_use_ty}`"
|
||||
),
|
||||
);
|
||||
if let Ok(ideal_method_sig) = ideal_method_sig {
|
||||
if matches!(source, TypeMismatchSource::Ty(_))
|
||||
&& let Ok(ideal_method_sig) = ideal_method_sig
|
||||
{
|
||||
self.emit_type_mismatch_suggestions(
|
||||
err,
|
||||
arg_expr,
|
||||
@ -2044,3 +2090,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum TypeMismatchSource<'tcx> {
|
||||
Ty(Ty<'tcx>),
|
||||
Arg(&'tcx hir::Expr<'tcx>, usize),
|
||||
}
|
||||
|
@ -472,7 +472,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
err_code: &str,
|
||||
fn_def_id: Option<DefId>,
|
||||
call_span: Span,
|
||||
call_expr: &hir::Expr<'tcx>,
|
||||
call_expr: &'tcx hir::Expr<'tcx>,
|
||||
) {
|
||||
// Next, let's construct the error
|
||||
let (error_span, full_call_span, call_name, is_method) = match &call_expr.kind {
|
||||
@ -808,8 +808,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
format!("arguments to this {} are incorrect", call_name),
|
||||
);
|
||||
|
||||
// TODO: We would like to point out when the rcvr was constrained
|
||||
// such that the arg mismatch occurs.
|
||||
if let hir::ExprKind::MethodCall(_, rcvr, _, _) = call_expr.kind
|
||||
&& provided_idx.as_usize() == expected_idx.as_usize()
|
||||
{
|
||||
self.note_source_of_type_mismatch_constraint(
|
||||
&mut err,
|
||||
rcvr,
|
||||
crate::demand::TypeMismatchSource::Arg(call_expr, provided_idx.as_usize()),
|
||||
);
|
||||
}
|
||||
|
||||
// Call out where the function is defined
|
||||
self.label_fn_like(
|
||||
|
@ -2,6 +2,8 @@
|
||||
fn main() {
|
||||
let mut v = Vec::new();
|
||||
v.push(0i32);
|
||||
//~^ NOTE this argument has type `i32`...
|
||||
//~| NOTE ... which causes `v` to have type `Vec<i32>`
|
||||
v.push(0);
|
||||
v.push(1i32); //~ ERROR mismatched types
|
||||
//~^ NOTE expected `i32`, found `u32`
|
||||
|
@ -2,6 +2,8 @@
|
||||
fn main() {
|
||||
let mut v = Vec::new();
|
||||
v.push(0i32);
|
||||
//~^ NOTE this argument has type `i32`...
|
||||
//~| NOTE ... which causes `v` to have type `Vec<i32>`
|
||||
v.push(0);
|
||||
v.push(1u32); //~ ERROR mismatched types
|
||||
//~^ NOTE expected `i32`, found `u32`
|
||||
|
@ -1,6 +1,11 @@
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/point-at-inference-3.rs:6:12
|
||||
--> $DIR/point-at-inference-3.rs:8:12
|
||||
|
|
||||
LL | v.push(0i32);
|
||||
| - ---- this argument has type `i32`...
|
||||
| |
|
||||
| ... which causes `v` to have type `Vec<i32>`
|
||||
...
|
||||
LL | v.push(1u32);
|
||||
| ---- ^^^^ expected `i32`, found `u32`
|
||||
| |
|
||||
|
Loading…
Reference in New Issue
Block a user