Skip to content

Commit dd35e2f

Browse files
authored
Rollup merge of #101388 - compiler-errors:issue-101376, r=fee1-dead
Don't delay invalid LHS bug unless it will be covered by an error in `check_overloaded_binop` Fixes #101376
2 parents 8f8a36d + 98f4b20 commit dd35e2f

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

compiler/rustc_typeck/src/check/op.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5757
)
5858
.is_ok()
5959
{
60-
// Suppress this error, since we already emitted
61-
// a deref suggestion in check_overloaded_binop
62-
err.downgrade_to_delayed_bug();
60+
// If LHS += RHS is an error, but *LHS += RHS is successful, then we will have
61+
// emitted a better suggestion during error handling in check_overloaded_binop.
62+
if self
63+
.lookup_op_method(
64+
lhs_ty,
65+
Some(rhs_ty),
66+
Some(rhs),
67+
Op::Binary(op, IsAssign::Yes),
68+
expected,
69+
)
70+
.is_err()
71+
{
72+
err.downgrade_to_delayed_bug();
73+
} else {
74+
// Otherwise, it's valid to suggest dereferencing the LHS here.
75+
err.span_suggestion_verbose(
76+
lhs.span.shrink_to_lo(),
77+
"consider dereferencing the left-hand side of this operation",
78+
"*",
79+
Applicability::MaybeIncorrect,
80+
);
81+
}
6382
}
6483
}
6584
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// issue #101376
2+
3+
use std::ops::AddAssign;
4+
struct Foo;
5+
6+
impl AddAssign<()> for Foo {
7+
fn add_assign(&mut self, _: ()) {}
8+
}
9+
10+
impl AddAssign<()> for &mut Foo {
11+
fn add_assign(&mut self, _: ()) {}
12+
}
13+
14+
fn main() {
15+
(&mut Foo) += ();
16+
//~^ ERROR invalid left-hand side of assignment
17+
//~| NOTE cannot assign to this expression
18+
//~| HELP consider dereferencing the left-hand side of this operation
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0067]: invalid left-hand side of assignment
2+
--> $DIR/assign-non-lval-needs-deref.rs:15:16
3+
|
4+
LL | (&mut Foo) += ();
5+
| ---------- ^^
6+
| |
7+
| cannot assign to this expression
8+
|
9+
help: consider dereferencing the left-hand side of this operation
10+
|
11+
LL | *(&mut Foo) += ();
12+
| +
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0067`.

0 commit comments

Comments
 (0)