mirror of https://github.com/rust-lang/rust.git
Rollup merge of #105595 - TaKO8Ki:suggest-dereferencing-receiver-argument, r=compiler-errors
Suggest dereferencing receiver arguments properly Fixes #105429
This commit is contained in:
commit
939880ab72
|
@ -696,7 +696,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||||
trait_pred: ty::PolyTraitPredicate<'tcx>,
|
trait_pred: ty::PolyTraitPredicate<'tcx>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
// It only make sense when suggesting dereferences for arguments
|
// It only make sense when suggesting dereferences for arguments
|
||||||
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code()
|
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, call_hir_id, .. } = obligation.cause.code()
|
||||||
else { return false; };
|
else { return false; };
|
||||||
let Some(typeck_results) = &self.typeck_results
|
let Some(typeck_results) = &self.typeck_results
|
||||||
else { return false; };
|
else { return false; };
|
||||||
|
@ -775,12 +775,33 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||||
real_trait_pred_and_base_ty,
|
real_trait_pred_and_base_ty,
|
||||||
);
|
);
|
||||||
if self.predicate_may_hold(&obligation) {
|
if self.predicate_may_hold(&obligation) {
|
||||||
err.span_suggestion_verbose(
|
let call_node = self.tcx.hir().get(*call_hir_id);
|
||||||
span.shrink_to_lo(),
|
let msg = "consider dereferencing here";
|
||||||
"consider dereferencing here",
|
let is_receiver = matches!(
|
||||||
"*",
|
call_node,
|
||||||
Applicability::MachineApplicable,
|
Node::Expr(hir::Expr {
|
||||||
|
kind: hir::ExprKind::MethodCall(_, receiver_expr, ..),
|
||||||
|
..
|
||||||
|
})
|
||||||
|
if receiver_expr.hir_id == *arg_hir_id
|
||||||
);
|
);
|
||||||
|
if is_receiver {
|
||||||
|
err.multipart_suggestion_verbose(
|
||||||
|
msg,
|
||||||
|
vec![
|
||||||
|
(span.shrink_to_lo(), "(*".to_string()),
|
||||||
|
(span.shrink_to_hi(), ")".to_string()),
|
||||||
|
],
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
err.span_suggestion_verbose(
|
||||||
|
span.shrink_to_lo(),
|
||||||
|
msg,
|
||||||
|
'*',
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
)
|
||||||
|
};
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2854,6 +2875,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||||
arg_hir_id,
|
arg_hir_id,
|
||||||
call_hir_id,
|
call_hir_id,
|
||||||
ref parent_code,
|
ref parent_code,
|
||||||
|
..
|
||||||
} => {
|
} => {
|
||||||
self.function_argument_obligation(
|
self.function_argument_obligation(
|
||||||
arg_hir_id,
|
arg_hir_id,
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
struct TargetStruct;
|
||||||
|
|
||||||
|
impl From<usize> for TargetStruct {
|
||||||
|
fn from(_unchecked: usize) -> Self {
|
||||||
|
TargetStruct
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = &3;
|
||||||
|
let _b: TargetStruct = (*a).into(); //~ ERROR the trait bound `TargetStruct: From<&{integer}>` is not satisfied
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
struct TargetStruct;
|
||||||
|
|
||||||
|
impl From<usize> for TargetStruct {
|
||||||
|
fn from(_unchecked: usize) -> Self {
|
||||||
|
TargetStruct
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = &3;
|
||||||
|
let _b: TargetStruct = a.into(); //~ ERROR the trait bound `TargetStruct: From<&{integer}>` is not satisfied
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
error[E0277]: the trait bound `TargetStruct: From<&{integer}>` is not satisfied
|
||||||
|
--> $DIR/suggest-dereferencing-receiver-argument.rs:13:30
|
||||||
|
|
|
||||||
|
LL | let _b: TargetStruct = a.into();
|
||||||
|
| ^^^^ the trait `From<&{integer}>` is not implemented for `TargetStruct`
|
||||||
|
|
|
||||||
|
= note: required for `&{integer}` to implement `Into<TargetStruct>`
|
||||||
|
help: consider dereferencing here
|
||||||
|
|
|
||||||
|
LL | let _b: TargetStruct = (*a).into();
|
||||||
|
| ++ +
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0277`.
|
Loading…
Reference in New Issue