Skip to content

Commit 8917894

Browse files
Targeted fixes addressing erroneous suggestions
1 parent 6848ba2 commit 8917894

File tree

7 files changed

+29
-39
lines changed

7 files changed

+29
-39
lines changed

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
671671
trait_pred: ty::PolyTraitPredicate<'tcx>,
672672
) -> bool {
673673
// It only make sense when suggesting dereferences for arguments
674-
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() else {
675-
return false;
676-
};
677-
let param_env = obligation.param_env;
678-
let body_id = obligation.cause.body_id;
674+
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code()
675+
else { return false; };
676+
let Some(typeck_results) = self.in_progress_typeck_results
677+
else { return false; };
678+
let typeck_results = typeck_results.borrow();
679+
let hir::Node::Expr(expr) = self.tcx.hir().get(*arg_hir_id)
680+
else { return false; };
681+
let Some(arg_ty) = typeck_results.expr_ty_adjusted_opt(expr)
682+
else { return false; };
683+
679684
let span = obligation.cause.span;
680685
let mut real_trait_pred = trait_pred;
681686
let mut code = obligation.cause.code();
@@ -687,18 +692,30 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
687692

688693
// Skipping binder here, remapping below
689694
let real_ty = real_trait_pred.self_ty().skip_binder();
695+
if self.can_eq(obligation.param_env, real_ty, arg_ty).is_err() {
696+
continue;
697+
}
690698

691699
if let ty::Ref(region, base_ty, mutbl) = *real_ty.kind() {
692-
let mut autoderef = Autoderef::new(self, param_env, body_id, span, base_ty, span);
700+
let mut autoderef = Autoderef::new(
701+
self,
702+
obligation.param_env,
703+
obligation.cause.body_id,
704+
span,
705+
base_ty,
706+
span,
707+
);
693708
if let Some(steps) = autoderef.find_map(|(ty, steps)| {
694709
// Re-add the `&`
695710
let ty = self.tcx.mk_ref(region, TypeAndMut { ty, mutbl });
696711

697712
// Remapping bound vars here
698713
let real_trait_pred_and_ty =
699714
real_trait_pred.map_bound(|inner_trait_pred| (inner_trait_pred, ty));
700-
let obligation = self
701-
.mk_trait_obligation_with_new_self_ty(param_env, real_trait_pred_and_ty);
715+
let obligation = self.mk_trait_obligation_with_new_self_ty(
716+
obligation.param_env,
717+
real_trait_pred_and_ty,
718+
);
702719
Some(steps).filter(|_| self.predicate_may_hold(&obligation))
703720
}) {
704721
if steps > 0 {
@@ -727,7 +744,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
727744
let real_trait_pred_and_base_ty =
728745
real_trait_pred.map_bound(|inner_trait_pred| (inner_trait_pred, base_ty));
729746
let obligation = self.mk_trait_obligation_with_new_self_ty(
730-
param_env,
747+
obligation.param_env,
731748
real_trait_pred_and_base_ty,
732749
);
733750
if self.predicate_may_hold(&obligation) {
@@ -855,6 +872,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
855872
_ => return false,
856873
};
857874
if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArgumentObligation { .. })
875+
&& obligation.cause.span.can_be_used_for_suggestions()
858876
{
859877
// When the obligation error has been ensured to have been caused by
860878
// an argument, the `obligation.cause.span` points at the expression

src/test/ui/binop/issue-77910-1.stderr

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@ LL | assert_eq!(foo, y);
1919
| ^^^^^^^^^^^^^^^^^^ `for<'r> fn(&'r i32) -> &'r i32 {foo}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
2020
|
2121
= help: the trait `Debug` is not implemented for `for<'r> fn(&'r i32) -> &'r i32 {foo}`
22+
= help: use parentheses to call the function: `foo(s)`
2223
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
23-
help: use parentheses to call the function
24-
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
25-
|
26-
LL | $crate::panicking::assert_failed(kind, &*left_val(s), &*right_val, $crate::option::Option::None);
27-
| +++
2824

2925
error: aborting due to 2 previous errors
3026

src/test/ui/closures/closure-move-sync.stderr

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ note: required by a bound in `spawn`
2222
|
2323
LL | F: Send + 'static,
2424
| ^^^^ required by this bound in `spawn`
25-
help: consider dereferencing here
26-
|
27-
LL | let t = thread::spawn(*|| {
28-
| +
2925

3026
error[E0277]: `Sender<()>` cannot be shared between threads safely
3127
--> $DIR/closure-move-sync.rs:18:19
@@ -47,10 +43,6 @@ note: required by a bound in `spawn`
4743
|
4844
LL | F: Send + 'static,
4945
| ^^^^ required by this bound in `spawn`
50-
help: consider dereferencing here
51-
|
52-
LL | thread::spawn(*|| tx.send(()).unwrap());
53-
| +
5446

5547
error: aborting due to 2 previous errors
5648

src/test/ui/generator/not-send-sync.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ note: required by a bound in `assert_send`
2323
|
2424
LL | fn assert_send<T: Send>(_: T) {}
2525
| ^^^^ required by this bound in `assert_send`
26-
help: consider dereferencing here
27-
|
28-
LL | assert_send(*|| {
29-
| +
3026

3127
error: generator cannot be shared between threads safely
3228
--> $DIR/not-send-sync.rs:9:17

src/test/ui/generator/print/generator-print-verbose-2.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ note: required by a bound in `assert_send`
2323
|
2424
LL | fn assert_send<T: Send>(_: T) {}
2525
| ^^^^ required by this bound in `assert_send`
26-
help: consider dereferencing here
27-
|
28-
LL | assert_send(*|| {
29-
| +
3026

3127
error: generator cannot be shared between threads safely
3228
--> $DIR/generator-print-verbose-2.rs:12:17

src/test/ui/interior-mutability/interior-mutability.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ note: required by a bound in `catch_unwind`
1919
|
2020
LL | pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
2121
| ^^^^^^^^^^ required by this bound in `catch_unwind`
22-
help: consider dereferencing here
23-
|
24-
LL | catch_unwind(*|| { x.set(23); });
25-
| +
2622

2723
error: aborting due to previous error
2824

src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.stderr

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ LL | assert_eq!(a, 0);
3434
| ^^^^^^^^^^^^^^^^ `fn() -> i32 {a}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
3535
|
3636
= help: the trait `Debug` is not implemented for `fn() -> i32 {a}`
37+
= help: use parentheses to call the function: `a()`
3738
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
38-
help: use parentheses to call the function
39-
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
40-
|
41-
LL | $crate::panicking::assert_failed(kind, &*left_val(), &*right_val, $crate::option::Option::None);
42-
| ++
4339

4440
error: aborting due to 3 previous errors
4541

0 commit comments

Comments
 (0)