Skip to content

Commit

Permalink
Rollup merge of #105595 - TaKO8Ki:suggest-dereferencing-receiver-argu…
Browse files Browse the repository at this point in the history
…ment, r=compiler-errors

Suggest dereferencing receiver arguments properly

Fixes #105429
  • Loading branch information
matthiaskrgr authored Dec 14, 2022
2 parents e5fde96 + 19fa5b3 commit 939880a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
trait_pred: ty::PolyTraitPredicate<'tcx>,
) -> bool {
// 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; };
let Some(typeck_results) = &self.typeck_results
else { return false; };
Expand Down Expand Up @@ -775,12 +775,33 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
real_trait_pred_and_base_ty,
);
if self.predicate_may_hold(&obligation) {
err.span_suggestion_verbose(
span.shrink_to_lo(),
"consider dereferencing here",
"*",
Applicability::MachineApplicable,
let call_node = self.tcx.hir().get(*call_hir_id);
let msg = "consider dereferencing here";
let is_receiver = matches!(
call_node,
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;
}
}
Expand Down Expand Up @@ -2854,6 +2875,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
arg_hir_id,
call_hir_id,
ref parent_code,
..
} => {
self.function_argument_obligation(
arg_hir_id,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 939880a

Please sign in to comment.