Skip to content

Commit

Permalink
Rollup merge of rust-lang#102765 - TaKO8Ki:follow-up-to-102708, r=com…
Browse files Browse the repository at this point in the history
…piler-errors

Suggest `==` to the first expr which has `ExprKind::Assign` kind

follow-up to rust-lang#102708

[playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=4241dc33ed8af02e1ef530d6b14903fd)
  • Loading branch information
Dylan-DPC authored Oct 13, 2022
2 parents fa0ca78 + 57d0278 commit ad45dd1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
12 changes: 12 additions & 0 deletions compiler/rustc_hir_analysis/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
rhs_expr,
) = lhs.kind
{
// if x == 1 && y == 2 { .. }
// +
let actual_lhs_ty = self.check_expr(&rhs_expr);
(Applicability::MaybeIncorrect, self.can_coerce(rhs_ty, actual_lhs_ty))
} else if let ExprKind::Binary(
Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
lhs_expr,
_,
) = rhs.kind
{
// if x == 1 && y == 2 { .. }
// +
let actual_rhs_ty = self.check_expr(&lhs_expr);
(Applicability::MaybeIncorrect, self.can_coerce(actual_rhs_ty, lhs_ty))
} else {
(Applicability::MaybeIncorrect, false)
};
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/type/type-check/assignment-in-if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ fn main() {
//~| ERROR mismatched types
println!("{}", x);
}

if x = 1 && x == 1 {
//~^ ERROR mismatched types
//~| ERROR mismatched types
println!("{}", x);
}
}
19 changes: 18 additions & 1 deletion src/test/ui/type/type-check/assignment-in-if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ help: you might have meant to compare for equality
LL | if x == x && x == x && x == x {
| +

error: aborting due to 11 previous errors
error[E0308]: mismatched types
--> $DIR/assignment-in-if.rs:57:12
|
LL | if x = 1 && x == 1 {
| ^ expected `bool`, found integer

error[E0308]: mismatched types
--> $DIR/assignment-in-if.rs:57:8
|
LL | if x = 1 && x == 1 {
| ^^^^^^^^^^^^^^^ expected `bool`, found `()`
|
help: you might have meant to compare for equality
|
LL | if x == 1 && x == 1 {
| +

error: aborting due to 13 previous errors

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

0 comments on commit ad45dd1

Please sign in to comment.