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

Suggest deref on comparison binop RHS even if type is not Copy #110550

Merged
merged 1 commit into from
Apr 26, 2023
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
6 changes: 6 additions & 0 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// FIXME(compiler-errors): We can actually do this if the checked_ty is
// `steps` layers of boxes, not just one, but this is easier and most likely.
|| (checked_ty.is_box() && steps == 1)
// We can always deref a binop that takes its arguments by ref.
|| matches!(
self.tcx.hir().get_parent(expr.hir_id),
hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(op, ..), .. })
if !op.node.is_by_value()
)
{
let deref_kind = if checked_ty.is_box() {
"unboxing the value"
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/inference/deref-suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ fn main() {
} else {
&0
};

#[derive(PartialEq, Eq)]
struct Foo;
let foo = Foo;
let bar = &Foo;

if foo == bar {
//~^ ERROR mismatched types
}
}
15 changes: 14 additions & 1 deletion tests/ui/inference/deref-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ LL | || };
| |_____`if` and `else` have incompatible types
| expected `i32`, found `&{integer}`

error: aborting due to 13 previous errors
error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:81:15
|
LL | if foo == bar {
| --- ^^^ expected `Foo`, found `&Foo`
| |
| expected because this is `Foo`
|
help: consider dereferencing the borrow
|
LL | if foo == *bar {
| +

error: aborting due to 14 previous errors

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