Skip to content

Commit 939880a

Browse files
authored
Rollup merge of #105595 - TaKO8Ki:suggest-dereferencing-receiver-argument, r=compiler-errors
Suggest dereferencing receiver arguments properly Fixes #105429
2 parents e5fde96 + 19fa5b3 commit 939880a

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
696696
trait_pred: ty::PolyTraitPredicate<'tcx>,
697697
) -> bool {
698698
// It only make sense when suggesting dereferences for arguments
699-
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code()
699+
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, call_hir_id, .. } = obligation.cause.code()
700700
else { return false; };
701701
let Some(typeck_results) = &self.typeck_results
702702
else { return false; };
@@ -775,12 +775,33 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
775775
real_trait_pred_and_base_ty,
776776
);
777777
if self.predicate_may_hold(&obligation) {
778-
err.span_suggestion_verbose(
779-
span.shrink_to_lo(),
780-
"consider dereferencing here",
781-
"*",
782-
Applicability::MachineApplicable,
778+
let call_node = self.tcx.hir().get(*call_hir_id);
779+
let msg = "consider dereferencing here";
780+
let is_receiver = matches!(
781+
call_node,
782+
Node::Expr(hir::Expr {
783+
kind: hir::ExprKind::MethodCall(_, receiver_expr, ..),
784+
..
785+
})
786+
if receiver_expr.hir_id == *arg_hir_id
783787
);
788+
if is_receiver {
789+
err.multipart_suggestion_verbose(
790+
msg,
791+
vec![
792+
(span.shrink_to_lo(), "(*".to_string()),
793+
(span.shrink_to_hi(), ")".to_string()),
794+
],
795+
Applicability::MachineApplicable,
796+
)
797+
} else {
798+
err.span_suggestion_verbose(
799+
span.shrink_to_lo(),
800+
msg,
801+
'*',
802+
Applicability::MachineApplicable,
803+
)
804+
};
784805
return true;
785806
}
786807
}
@@ -2854,6 +2875,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
28542875
arg_hir_id,
28552876
call_hir_id,
28562877
ref parent_code,
2878+
..
28572879
} => {
28582880
self.function_argument_obligation(
28592881
arg_hir_id,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
struct TargetStruct;
4+
5+
impl From<usize> for TargetStruct {
6+
fn from(_unchecked: usize) -> Self {
7+
TargetStruct
8+
}
9+
}
10+
11+
fn main() {
12+
let a = &3;
13+
let _b: TargetStruct = (*a).into(); //~ ERROR the trait bound `TargetStruct: From<&{integer}>` is not satisfied
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
struct TargetStruct;
4+
5+
impl From<usize> for TargetStruct {
6+
fn from(_unchecked: usize) -> Self {
7+
TargetStruct
8+
}
9+
}
10+
11+
fn main() {
12+
let a = &3;
13+
let _b: TargetStruct = a.into(); //~ ERROR the trait bound `TargetStruct: From<&{integer}>` is not satisfied
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0277]: the trait bound `TargetStruct: From<&{integer}>` is not satisfied
2+
--> $DIR/suggest-dereferencing-receiver-argument.rs:13:30
3+
|
4+
LL | let _b: TargetStruct = a.into();
5+
| ^^^^ the trait `From<&{integer}>` is not implemented for `TargetStruct`
6+
|
7+
= note: required for `&{integer}` to implement `Into<TargetStruct>`
8+
help: consider dereferencing here
9+
|
10+
LL | let _b: TargetStruct = (*a).into();
11+
| ++ +
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)