Skip to content

Commit 19fa5b3

Browse files
committed
suggest dereferencing receiver arguments properly
fix a stderr
1 parent aa5b179 commit 19fa5b3

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
@@ -694,7 +694,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
694694
trait_pred: ty::PolyTraitPredicate<'tcx>,
695695
) -> bool {
696696
// It only make sense when suggesting dereferences for arguments
697-
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code()
697+
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, call_hir_id, .. } = obligation.cause.code()
698698
else { return false; };
699699
let Some(typeck_results) = &self.typeck_results
700700
else { return false; };
@@ -773,12 +773,33 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
773773
real_trait_pred_and_base_ty,
774774
);
775775
if self.predicate_may_hold(&obligation) {
776-
err.span_suggestion_verbose(
777-
span.shrink_to_lo(),
778-
"consider dereferencing here",
779-
"*",
780-
Applicability::MachineApplicable,
776+
let call_node = self.tcx.hir().get(*call_hir_id);
777+
let msg = "consider dereferencing here";
778+
let is_receiver = matches!(
779+
call_node,
780+
Node::Expr(hir::Expr {
781+
kind: hir::ExprKind::MethodCall(_, receiver_expr, ..),
782+
..
783+
})
784+
if receiver_expr.hir_id == *arg_hir_id
781785
);
786+
if is_receiver {
787+
err.multipart_suggestion_verbose(
788+
msg,
789+
vec![
790+
(span.shrink_to_lo(), "(*".to_string()),
791+
(span.shrink_to_hi(), ")".to_string()),
792+
],
793+
Applicability::MachineApplicable,
794+
)
795+
} else {
796+
err.span_suggestion_verbose(
797+
span.shrink_to_lo(),
798+
msg,
799+
'*',
800+
Applicability::MachineApplicable,
801+
)
802+
};
782803
return true;
783804
}
784805
}
@@ -2857,6 +2878,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
28572878
arg_hir_id,
28582879
call_hir_id,
28592880
ref parent_code,
2881+
..
28602882
} => {
28612883
self.function_argument_obligation(
28622884
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)