1
Fork 0

Rollup merge of #104564 - RalfJung:either, r=oli-obk

interpret: use Either over Result when it is not representing an error condition

r? `@oli-obk`
This commit is contained in:
Matthias Krüger 2022-11-20 18:21:48 +01:00 committed by GitHub
commit 820a41580e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 128 additions and 102 deletions

View file

@ -11,6 +11,8 @@ use std::hash;
use std::ops::Range;
use std::ptr;
use either::{Left, Right};
use rustc_ast::Mutability;
use rustc_data_structures::intern::Interned;
use rustc_span::DUMMY_SP;
@ -503,11 +505,11 @@ impl<Prov: Provenance, Extra> Allocation<Prov, Extra> {
// `to_bits_or_ptr_internal` is the right method because we just want to store this data
// as-is into memory.
let (bytes, provenance) = match val.to_bits_or_ptr_internal(range.size)? {
Err(val) => {
let (provenance, offset) = val.into_parts();
Right(ptr) => {
let (provenance, offset) = ptr.into_parts();
(u128::from(offset.bytes()), Some(provenance))
}
Ok(data) => (data, None),
Left(data) => (data, None),
};
let endian = cx.data_layout().endian;

View file

@ -1,6 +1,8 @@
use std::convert::{TryFrom, TryInto};
use std::fmt;
use either::{Either, Left, Right};
use rustc_apfloat::{
ieee::{Double, Single},
Float,
@ -293,10 +295,10 @@ impl<Prov> Scalar<Prov> {
pub fn to_bits_or_ptr_internal(
self,
target_size: Size,
) -> Result<Result<u128, Pointer<Prov>>, ScalarSizeMismatch> {
) -> Result<Either<u128, Pointer<Prov>>, ScalarSizeMismatch> {
assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
Ok(match self {
Scalar::Int(int) => Ok(int.to_bits(target_size).map_err(|size| {
Scalar::Int(int) => Left(int.to_bits(target_size).map_err(|size| {
ScalarSizeMismatch { target_size: target_size.bytes(), data_size: size.bytes() }
})?),
Scalar::Ptr(ptr, sz) => {
@ -306,7 +308,7 @@ impl<Prov> Scalar<Prov> {
data_size: sz.into(),
});
}
Err(ptr)
Right(ptr)
}
})
}
@ -318,8 +320,8 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
.to_bits_or_ptr_internal(cx.pointer_size())
.map_err(|s| err_ub!(ScalarSizeMismatch(s)))?
{
Err(ptr) => Ok(ptr.into()),
Ok(bits) => {
Right(ptr) => Ok(ptr.into()),
Left(bits) => {
let addr = u64::try_from(bits).unwrap();
Ok(Pointer::from_addr(addr))
}