mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +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);
|
|| self.suggest_coercing_result_via_try_operator(err, expr, expected, expr_ty);
|
||||||
|
|
||||||
if !suggested {
|
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,
|
&self,
|
||||||
err: &mut Diagnostic,
|
err: &mut Diagnostic,
|
||||||
expr: &hir::Expr<'_>,
|
expr: &hir::Expr<'_>,
|
||||||
expected_ty: Ty<'tcx>,
|
source: TypeMismatchSource<'tcx>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let hir = self.tcx.hir();
|
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)) {
|
if !self.can_eq(self.param_env, expected_ty, init_ty.fold_with(&mut fudger)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -360,7 +404,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
"... which constrains `{ident}` to have type `{next_use_ty}`"
|
"... 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(
|
self.emit_type_mismatch_suggestions(
|
||||||
err,
|
err,
|
||||||
arg_expr,
|
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,
|
err_code: &str,
|
||||||
fn_def_id: Option<DefId>,
|
fn_def_id: Option<DefId>,
|
||||||
call_span: Span,
|
call_span: Span,
|
||||||
call_expr: &hir::Expr<'tcx>,
|
call_expr: &'tcx hir::Expr<'tcx>,
|
||||||
) {
|
) {
|
||||||
// Next, let's construct the error
|
// Next, let's construct the error
|
||||||
let (error_span, full_call_span, call_name, is_method) = match &call_expr.kind {
|
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),
|
format!("arguments to this {} are incorrect", call_name),
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: We would like to point out when the rcvr was constrained
|
if let hir::ExprKind::MethodCall(_, rcvr, _, _) = call_expr.kind
|
||||||
// such that the arg mismatch occurs.
|
&& 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
|
// Call out where the function is defined
|
||||||
self.label_fn_like(
|
self.label_fn_like(
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
v.push(0i32);
|
v.push(0i32);
|
||||||
|
//~^ NOTE this argument has type `i32`...
|
||||||
|
//~| NOTE ... which causes `v` to have type `Vec<i32>`
|
||||||
v.push(0);
|
v.push(0);
|
||||||
v.push(1i32); //~ ERROR mismatched types
|
v.push(1i32); //~ ERROR mismatched types
|
||||||
//~^ NOTE expected `i32`, found `u32`
|
//~^ NOTE expected `i32`, found `u32`
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
v.push(0i32);
|
v.push(0i32);
|
||||||
|
//~^ NOTE this argument has type `i32`...
|
||||||
|
//~| NOTE ... which causes `v` to have type `Vec<i32>`
|
||||||
v.push(0);
|
v.push(0);
|
||||||
v.push(1u32); //~ ERROR mismatched types
|
v.push(1u32); //~ ERROR mismatched types
|
||||||
//~^ NOTE expected `i32`, found `u32`
|
//~^ NOTE expected `i32`, found `u32`
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
error[E0308]: mismatched types
|
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);
|
LL | v.push(1u32);
|
||||||
| ---- ^^^^ expected `i32`, found `u32`
|
| ---- ^^^^ expected `i32`, found `u32`
|
||||||
| |
|
| |
|
||||||
|
Loading…
Reference in New Issue
Block a user