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

fix: add deref suggestion for type mismatch binary op #112961

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 15 additions & 0 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

if let Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Binary(_, left, right),
..
})) = self.tcx.hir().find_parent(expr.hir_id) && mutability.is_mut() {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this needs special-casing for mutable references.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean mutability.is_mut()? It does to prevent this problem:

let a = String::new();
let b = String::new();
let _ = a + b // should suggestion a + &b, rather than *a + b

Copy link
Member

Choose a reason for hiding this comment

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

Well what we really should be doing is checking whether the obligation Lhs: BinOp<Rhs> holds after dereffing the left-hand side. Not really sure if that's possible here, so I guess maybe give that a try or else I can approve..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

checking whether the obligation Lhs: BinOp holds after dereffing the left-hand side

I haven't found a definitive solution yet. Do you have any thoughts or suggestions?

let deref_expr = if expr.hir_id == left.hir_id { right } else { left };
let sugg = vec![(deref_expr.span.shrink_to_lo(), "*".to_string())];
return Some((
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason why I didn't think it needed to be wrapped in parentheses is that I thought it would throw an error beforehand.

sugg,
format!("consider dereferencing here"),
Applicability::MachineApplicable,
true,
false
))
}

let sugg = mutability.ref_prefix_str();
let (sugg, verbose) = if needs_parens {
(
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/suggestions/issue-112958.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let a = &mut 0;
let b = 1;
let _ = a < b;
//~^ ERROR mismatched types
//~| HELP consider dereferencing here
//~| SUGGESTION *
}
16 changes: 16 additions & 0 deletions tests/ui/suggestions/issue-112958.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0308]: mismatched types
--> $DIR/issue-112958.rs:4:17
|
LL | let _ = a < b;
| ^ expected `&mut _`, found integer
|
= note: expected mutable reference `&mut _`
found type `{integer}`
help: consider dereferencing here
|
LL | let _ = *a < b;
| +

error: aborting due to previous error

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