Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't delay invalid LHS bug unless it will be covered by an error in check_overloaded_binop #101388

Merged
merged 2 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions compiler/rustc_typeck/src/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
.is_ok()
{
// Suppress this error, since we already emitted
// a deref suggestion in check_overloaded_binop
err.downgrade_to_delayed_bug();
// If LHS += RHS is an error, but *LHS += RHS is successful, then we will have
// emitted a better suggestion during error handling in check_overloaded_binop.
if self
.lookup_op_method(
lhs_ty,
Some(rhs_ty),
Some(rhs),
Op::Binary(op, IsAssign::Yes),
expected,
)
.is_err()
{
err.downgrade_to_delayed_bug();
} else {
// Otherwise, it's valid to suggest dereferencing the LHS here.
err.span_suggestion_verbose(
lhs.span.shrink_to_lo(),
"consider dereferencing the left-hand side of this operation",
"*",
Applicability::MaybeIncorrect,
);
}
}
}
});
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/typeck/assign-non-lval-needs-deref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// issue #101376

use std::ops::AddAssign;
struct Foo;

impl AddAssign<()> for Foo {
fn add_assign(&mut self, _: ()) {}
}

impl AddAssign<()> for &mut Foo {
fn add_assign(&mut self, _: ()) {}
}

fn main() {
(&mut Foo) += ();
//~^ ERROR invalid left-hand side of assignment
//~| NOTE cannot assign to this expression
//~| HELP consider dereferencing the left-hand side of this operation
}
Comment on lines +3 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please link this to #101376?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

16 changes: 16 additions & 0 deletions src/test/ui/typeck/assign-non-lval-needs-deref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0067]: invalid left-hand side of assignment
--> $DIR/assign-non-lval-needs-deref.rs:15:16
|
LL | (&mut Foo) += ();
| ---------- ^^
| |
| cannot assign to this expression
|
help: consider dereferencing the left-hand side of this operation
|
LL | *(&mut Foo) += ();
Copy link
Member Author

@compiler-errors compiler-errors Sep 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion may be a bit strange here in this minimized example, but it makes sense if &mut Foo is coming from a function argument or something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, it looks like a function with a return type of &mut Foo can also trigger this ICE. foo() += ();

| +

error: aborting due to previous error

For more information about this error, try `rustc --explain E0067`.