BinOpAssign always returns unit

This commit is contained in:
Oli Scherer
2026-04-22 12:08:58 +02:00
parent 9ab01ae53c
commit 24b1634898
4 changed files with 28 additions and 13 deletions
+4 -9
View File
@@ -34,19 +34,14 @@ pub(crate) fn check_expr_assign_op(
rhs: &'tcx Expr<'tcx>,
expected: Expectation<'tcx>,
) -> Ty<'tcx> {
let (lhs_ty, rhs_ty, return_ty) =
let (lhs_ty, rhs_ty, _return_ty) =
self.check_overloaded_binop(expr, lhs, rhs, Op::AssignOp(op), expected);
let category = BinOpCategory::from(op.node);
let ty = if !lhs_ty.is_ty_var()
&& !rhs_ty.is_ty_var()
&& is_builtin_binop(lhs_ty, rhs_ty, category)
if !lhs_ty.is_ty_var() && !rhs_ty.is_ty_var() && is_builtin_binop(lhs_ty, rhs_ty, category)
{
self.enforce_builtin_binop_types(lhs.span, lhs_ty, rhs.span, rhs_ty, category);
self.tcx.types.unit
} else {
return_ty
};
}
self.check_lhs_assignable(lhs, E0067, op.span, |err| {
if let Some(lhs_deref_ty) = self.deref_once_mutably_for_diagnostic(lhs_ty) {
@@ -86,7 +81,7 @@ pub(crate) fn check_expr_assign_op(
}
});
ty
self.tcx.types.unit
}
/// Checks a potentially overloaded binary operator.
@@ -15,6 +15,8 @@ fn test_where_left_is_not_let() {
//~| ERROR binary assignment operation `+=` cannot be used in a let chain
//~| NOTE cannot use `+=` in a let chain
//~| HELP you might have meant to compare with `==` instead of assigning with `+=`
//~| ERROR mismatched types
//~| NOTE expected `bool`, found `()`
}
fn test_where_left_is_let() {
@@ -28,6 +30,8 @@ fn test_where_left_is_let() {
//~| ERROR binary assignment operation `+=` cannot be used in a let chain
//~| NOTE cannot use `+=` in a let chain
//~| HELP you might have meant to compare with `==` instead of assigning with `+=`
//~| ERROR mismatched types
//~| NOTE expected `bool`, found `()`
}
fn main() {
@@ -15,6 +15,8 @@ fn test_where_left_is_not_let() {
//~| ERROR binary assignment operation `+=` cannot be used in a let chain
//~| NOTE cannot use `+=` in a let chain
//~| HELP you might have meant to compare with `==` instead of assigning with `+=`
//~| ERROR mismatched types
//~| NOTE expected `bool`, found `()`
}
fn test_where_left_is_let() {
@@ -28,6 +30,8 @@ fn test_where_left_is_let() {
//~| ERROR binary assignment operation `+=` cannot be used in a let chain
//~| NOTE cannot use `+=` in a let chain
//~| HELP you might have meant to compare with `==` instead of assigning with `+=`
//~| ERROR mismatched types
//~| NOTE expected `bool`, found `()`
}
fn main() {
@@ -7,7 +7,7 @@ LL | if let _ = 1 && true && y += 2 {};
= note: only supported directly in conditions of `if` and `while` expressions
error: expected expression, found `let` statement
--> $DIR/let-chains-assign-add-incorrect.rs:22:8
--> $DIR/let-chains-assign-add-incorrect.rs:24:8
|
LL | if let _ = 1 && y += 2 {};
| ^^^^^^^^^
@@ -37,13 +37,19 @@ LL + if let _ = 1 && true && y == 2 {};
|
error[E0308]: mismatched types
--> $DIR/let-chains-assign-add-incorrect.rs:22:21
--> $DIR/let-chains-assign-add-incorrect.rs:8:8
|
LL | if let _ = 1 && true && y += 2 {};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
error[E0308]: mismatched types
--> $DIR/let-chains-assign-add-incorrect.rs:24:21
|
LL | if let _ = 1 && y += 2 {};
| ^ expected `bool`, found integer
error: binary assignment operation `+=` cannot be used in a let chain
--> $DIR/let-chains-assign-add-incorrect.rs:22:23
--> $DIR/let-chains-assign-add-incorrect.rs:24:23
|
LL | if let _ = 1 && y += 2 {};
| -------------- ^^ cannot use `+=` in a let chain
@@ -56,6 +62,12 @@ LL - if let _ = 1 && y += 2 {};
LL + if let _ = 1 && y == 2 {};
|
error: aborting due to 6 previous errors
error[E0308]: mismatched types
--> $DIR/let-chains-assign-add-incorrect.rs:24:8
|
LL | if let _ = 1 && y += 2 {};
| ^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
error: aborting due to 8 previous errors
For more information about this error, try `rustc --explain E0308`.