Skip to content

Commit 95c0e5c

Browse files
committed
Remove Rvalue::CheckedBinaryOp
1 parent 8e78d16 commit 95c0e5c

File tree

59 files changed

+209
-212
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+209
-212
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,8 +1312,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13121312
);
13131313
}
13141314

1315-
Rvalue::BinaryOp(_bin_op, box (operand1, operand2))
1316-
| Rvalue::CheckedBinaryOp(_bin_op, box (operand1, operand2)) => {
1315+
Rvalue::BinaryOp(_bin_op, box (operand1, operand2)) => {
13171316
self.consume_operand(location, (operand1, span), flow_state);
13181317
self.consume_operand(location, (operand2, span), flow_state);
13191318
}

compiler/rustc_borrowck/src/polonius/loan_invalidations.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ impl<'cx, 'tcx> LoanInvalidationsGenerator<'cx, 'tcx> {
302302
);
303303
}
304304

305-
Rvalue::BinaryOp(_bin_op, box (operand1, operand2))
306-
| Rvalue::CheckedBinaryOp(_bin_op, box (operand1, operand2)) => {
305+
Rvalue::BinaryOp(_bin_op, box (operand1, operand2)) => {
307306
self.consume_operand(location, operand1);
308307
self.consume_operand(location, operand2);
309308
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,8 +2417,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24172417
self.check_operand(op, location);
24182418
}
24192419

2420-
Rvalue::BinaryOp(_, box (left, right))
2421-
| Rvalue::CheckedBinaryOp(_, box (left, right)) => {
2420+
Rvalue::BinaryOp(_, box (left, right)) => {
24222421
self.check_operand(left, location);
24232422
self.check_operand(right, location);
24242423
}
@@ -2445,7 +2444,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24452444
| Rvalue::Cast(..)
24462445
| Rvalue::ShallowInitBox(..)
24472446
| Rvalue::BinaryOp(..)
2448-
| Rvalue::CheckedBinaryOp(..)
24492447
| Rvalue::NullaryOp(..)
24502448
| Rvalue::CopyForDeref(..)
24512449
| Rvalue::UnaryOp(..)

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -609,14 +609,11 @@ fn codegen_stmt<'tcx>(
609609
let lhs = codegen_operand(fx, &lhs_rhs.0);
610610
let rhs = codegen_operand(fx, &lhs_rhs.1);
611611

612-
let res = crate::num::codegen_binop(fx, bin_op, lhs, rhs);
613-
lval.write_cvalue(fx, res);
614-
}
615-
Rvalue::CheckedBinaryOp(bin_op, ref lhs_rhs) => {
616-
let lhs = codegen_operand(fx, &lhs_rhs.0);
617-
let rhs = codegen_operand(fx, &lhs_rhs.1);
618-
619-
let res = crate::num::codegen_checked_int_binop(fx, bin_op, lhs, rhs);
612+
let res = if let Some(bin_op) = bin_op.overflowing_to_wrapping() {
613+
crate::num::codegen_checked_int_binop(fx, bin_op, lhs, rhs)
614+
} else {
615+
crate::num::codegen_binop(fx, bin_op, lhs, rhs)
616+
};
620617
lval.write_cvalue(fx, res);
621618
}
622619
Rvalue::UnaryOp(un_op, ref operand) => {

compiler/rustc_codegen_cranelift/src/codegen_i128.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub(crate) fn maybe_codegen<'tcx>(
7070
}
7171
BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne | BinOp::Cmp => None,
7272
BinOp::Shl | BinOp::ShlUnchecked | BinOp::Shr | BinOp::ShrUnchecked => None,
73+
BinOp::AddWithOverflow | BinOp::SubWithOverflow | BinOp::MulWithOverflow => unreachable!(),
7374
}
7475
}
7576

@@ -132,6 +133,7 @@ pub(crate) fn maybe_codegen_checked<'tcx>(
132133
Some(out_place.to_cvalue(fx))
133134
}
134135
BinOp::AddUnchecked | BinOp::SubUnchecked | BinOp::MulUnchecked => unreachable!(),
136+
BinOp::AddWithOverflow | BinOp::SubWithOverflow | BinOp::MulWithOverflow => unreachable!(),
135137
BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
136138
BinOp::Div | BinOp::Rem => unreachable!(),
137139
BinOp::Cmp => unreachable!(),

compiler/rustc_codegen_cranelift/src/num.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ pub(crate) fn codegen_int_binop<'tcx>(
179179
}
180180
}
181181
BinOp::Offset => unreachable!("Offset is not an integer operation"),
182+
BinOp::AddWithOverflow | BinOp::SubWithOverflow | BinOp::MulWithOverflow => {
183+
unreachable!("Overflow binops handled by `codegen_checked_int_binop`")
184+
}
182185
// Compare binops handles by `codegen_binop`.
183186
BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge | BinOp::Cmp => {
184187
unreachable!("{:?}({:?}, {:?})", bin_op, in_lhs.layout().ty, in_rhs.layout().ty);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,22 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
572572
}
573573
}
574574

575+
mir::Rvalue::BinaryOp(op_with_overflow, box (ref lhs, ref rhs))
576+
if let Some(op) = op_with_overflow.overflowing_to_wrapping() =>
577+
{
578+
let lhs = self.codegen_operand(bx, lhs);
579+
let rhs = self.codegen_operand(bx, rhs);
580+
let result = self.codegen_scalar_checked_binop(
581+
bx,
582+
op,
583+
lhs.immediate(),
584+
rhs.immediate(),
585+
lhs.layout.ty,
586+
);
587+
let val_ty = op.ty(bx.tcx(), lhs.layout.ty, rhs.layout.ty);
588+
let operand_ty = Ty::new_tup(bx.tcx(), &[val_ty, bx.tcx().types.bool]);
589+
OperandRef { val: result, layout: bx.cx().layout_of(operand_ty) }
590+
}
575591
mir::Rvalue::BinaryOp(op, box (ref lhs, ref rhs)) => {
576592
let lhs = self.codegen_operand(bx, lhs);
577593
let rhs = self.codegen_operand(bx, rhs);
@@ -600,20 +616,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
600616
layout: bx.cx().layout_of(op.ty(bx.tcx(), lhs.layout.ty, rhs.layout.ty)),
601617
}
602618
}
603-
mir::Rvalue::CheckedBinaryOp(op, box (ref lhs, ref rhs)) => {
604-
let lhs = self.codegen_operand(bx, lhs);
605-
let rhs = self.codegen_operand(bx, rhs);
606-
let result = self.codegen_scalar_checked_binop(
607-
bx,
608-
op,
609-
lhs.immediate(),
610-
rhs.immediate(),
611-
lhs.layout.ty,
612-
);
613-
let val_ty = op.ty(bx.tcx(), lhs.layout.ty, rhs.layout.ty);
614-
let operand_ty = Ty::new_tup(bx.tcx(), &[val_ty, bx.tcx().types.bool]);
615-
OperandRef { val: result, layout: bx.cx().layout_of(operand_ty) }
616-
}
617619

618620
mir::Rvalue::UnaryOp(op, ref operand) => {
619621
let operand = self.codegen_operand(bx, operand);
@@ -924,6 +926,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
924926
bx.select(is_lt, bx.cx().const_i8(Ordering::Less as i8), ge)
925927
}
926928
}
929+
mir::BinOp::AddWithOverflow
930+
| mir::BinOp::SubWithOverflow
931+
| mir::BinOp::MulWithOverflow => {
932+
bug!("{op:?} needs to return a pair, so call codegen_scalar_checked_binop instead")
933+
}
927934
}
928935
}
929936

@@ -1036,7 +1043,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10361043
mir::Rvalue::Cast(..) | // (*)
10371044
mir::Rvalue::ShallowInitBox(..) | // (*)
10381045
mir::Rvalue::BinaryOp(..) |
1039-
mir::Rvalue::CheckedBinaryOp(..) |
10401046
mir::Rvalue::UnaryOp(..) |
10411047
mir::Rvalue::Discriminant(..) |
10421048
mir::Rvalue::NullaryOp(..) |

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
167167
let left = self.read_immediate(&self.eval_operand(left, layout)?)?;
168168
let layout = util::binop_right_homogeneous(bin_op).then_some(left.layout);
169169
let right = self.read_immediate(&self.eval_operand(right, layout)?)?;
170-
self.binop_ignore_overflow(bin_op, &left, &right, &dest)?;
171-
}
172-
173-
CheckedBinaryOp(bin_op, box (ref left, ref right)) => {
174-
// Due to the extra boolean in the result, we can never reuse the `dest.layout`.
175-
let left = self.read_immediate(&self.eval_operand(left, None)?)?;
176-
let layout = util::binop_right_homogeneous(bin_op).then_some(left.layout);
177-
let right = self.read_immediate(&self.eval_operand(right, layout)?)?;
178-
self.binop_with_overflow(bin_op, &left, &right, &dest)?;
170+
if let Some(bin_op) = bin_op.overflowing_to_wrapping() {
171+
self.binop_with_overflow(bin_op, &left, &right, &dest)?;
172+
} else {
173+
self.binop_ignore_overflow(bin_op, &left, &right, &dest)?;
174+
}
179175
}
180176

181177
UnaryOp(un_op, ref operand) => {

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
580580
}
581581
}
582582

583-
Rvalue::BinaryOp(op, box (lhs, rhs)) | Rvalue::CheckedBinaryOp(op, box (lhs, rhs)) => {
583+
Rvalue::BinaryOp(op, box (lhs, rhs)) => {
584584
let lhs_ty = lhs.ty(self.body, self.tcx);
585585
let rhs_ty = rhs.ty(self.body, self.tcx);
586586

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ where
261261
| Rvalue::Cast(_, operand, _)
262262
| Rvalue::ShallowInitBox(operand, _) => in_operand::<Q, _>(cx, in_local, operand),
263263

264-
Rvalue::BinaryOp(_, box (lhs, rhs)) | Rvalue::CheckedBinaryOp(_, box (lhs, rhs)) => {
264+
Rvalue::BinaryOp(_, box (lhs, rhs)) => {
265265
in_operand::<Q, _>(cx, in_local, lhs) || in_operand::<Q, _>(cx, in_local, rhs)
266266
}
267267

0 commit comments

Comments
 (0)