1
Fork 0

Auto merge of #125359 - RalfJung:interpret-overflowing-ops, r=oli-obk

interpret: make overflowing binops just normal binops

Follow-up to https://github.com/rust-lang/rust/pull/125173 (Cc `@scottmcm)`
This commit is contained in:
bors 2024-05-23 04:03:14 +00:00
commit 5293c6adb7
40 changed files with 329 additions and 350 deletions

View file

@ -1,19 +1,22 @@
use super::{AllocId, AllocRange, ConstAllocation, Pointer, Scalar};
use std::borrow::Cow;
use std::{any::Any, backtrace::Backtrace, fmt};
use crate::error;
use crate::mir::{ConstAlloc, ConstValue};
use crate::ty::{self, layout, tls, Ty, TyCtxt, ValTree};
use either::Either;
use rustc_ast_ir::Mutability;
use rustc_data_structures::sync::Lock;
use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage, ErrorGuaranteed, IntoDiagArg};
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
use rustc_session::CtfeBacktrace;
use rustc_span::Symbol;
use rustc_span::{def_id::DefId, Span, DUMMY_SP};
use rustc_target::abi::{call, Align, Size, VariantIdx, WrappingRange};
use std::borrow::Cow;
use std::{any::Any, backtrace::Backtrace, fmt};
use super::{AllocId, AllocRange, ConstAllocation, Pointer, Scalar};
use crate::error;
use crate::mir::{ConstAlloc, ConstValue};
use crate::ty::{self, layout, tls, Ty, TyCtxt, ValTree};
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
pub enum ErrorHandled {
@ -310,6 +313,10 @@ pub enum UndefinedBehaviorInfo<'tcx> {
RemainderOverflow,
/// Overflowing inbounds pointer arithmetic.
PointerArithOverflow,
/// Overflow in arithmetic that may not overflow.
ArithOverflow { intrinsic: Symbol },
/// Shift by too much.
ShiftOverflow { intrinsic: Symbol, shift_amount: Either<u128, i128> },
/// Invalid metadata in a wide pointer
InvalidMeta(InvalidMetaKind),
/// Reading a C string that does not end within its allocation.

View file

@ -297,9 +297,11 @@ impl BorrowKind {
impl BinOp {
pub(crate) fn to_hir_binop(self) -> hir::BinOpKind {
match self {
BinOp::Add => hir::BinOpKind::Add,
BinOp::Sub => hir::BinOpKind::Sub,
BinOp::Mul => hir::BinOpKind::Mul,
// HIR `+`/`-`/`*` can map to either of these MIR BinOp, depending
// on whether overflow checks are enabled or not.
BinOp::Add | BinOp::AddWithOverflow => hir::BinOpKind::Add,
BinOp::Sub | BinOp::SubWithOverflow => hir::BinOpKind::Sub,
BinOp::Mul | BinOp::MulWithOverflow => hir::BinOpKind::Mul,
BinOp::Div => hir::BinOpKind::Div,
BinOp::Rem => hir::BinOpKind::Rem,
BinOp::BitXor => hir::BinOpKind::BitXor,
@ -313,10 +315,8 @@ impl BinOp {
BinOp::Gt => hir::BinOpKind::Gt,
BinOp::Le => hir::BinOpKind::Le,
BinOp::Ge => hir::BinOpKind::Ge,
// We don't have HIR syntax for these.
BinOp::Cmp
| BinOp::AddWithOverflow
| BinOp::SubWithOverflow
| BinOp::MulWithOverflow
| BinOp::AddUnchecked
| BinOp::SubUnchecked
| BinOp::MulUnchecked
@ -338,6 +338,11 @@ impl BinOp {
})
}
/// Returns whether this is a `FooWithOverflow`
pub fn is_overflowing(self) -> bool {
self.overflowing_to_wrapping().is_some()
}
/// If this is a `Foo`, return `Some(FooWithOverflow)`.
pub fn wrapping_to_overflowing(self) -> Option<BinOp> {
Some(match self {