Skip to content

Commit 3a87a18

Browse files
committed
Make zeroed return an Option
1 parent 936b567 commit 3a87a18

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

compiler/rustc_middle/src/mir/interpret/allocation.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ pub trait AllocBytes:
4242

4343
/// Create a zeroed `AllocBytes` of the specified size and alignment;
4444
/// call the callback error handler if there is an error in allocating the memory.
45-
fn zeroed<'tcx, F: Fn() -> InterpError<'tcx>>(
45+
fn zeroed(
4646
size: Size,
4747
_align: Align,
48-
handle_alloc_fail: F,
49-
) -> Result<Self, InterpError<'tcx>>;
48+
) -> Option<Self>;
5049
}
5150

5251
// Default `bytes` for `Allocation` is a `Box<[u8]>`.
@@ -59,16 +58,14 @@ impl AllocBytes for Box<[u8]> {
5958
Box::<[u8]>::from(slice.into())
6059
}
6160

62-
fn zeroed<'tcx, F: Fn() -> InterpError<'tcx>>(
61+
fn zeroed(
6362
size: Size,
6463
_align: Align,
65-
handle_alloc_fail: F,
66-
) -> Result<Self, InterpError<'tcx>> {
67-
let bytes = Box::<[u8]>::try_new_zeroed_slice(size.bytes_usize())
68-
.map_err(|_| handle_alloc_fail())?;
64+
) -> Option<Self> {
65+
let bytes = Box::<[u8]>::try_new_zeroed_slice(size.bytes_usize()).ok()?;
6966
// SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
7067
let bytes = unsafe { bytes.assume_init() };
71-
Ok(bytes)
68+
Some(bytes)
7269
}
7370
}
7471

@@ -304,7 +301,7 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
304301
///
305302
/// If `panic_on_fail` is true, this will never return `Err`.
306303
pub fn uninit<'tcx>(size: Size, align: Align, panic_on_fail: bool) -> InterpResult<'tcx, Self> {
307-
let handle_alloc_fail = || -> InterpError<'tcx> {
304+
let bytes = Bytes::zeroed(size, align).ok_or_else(|| {
308305
// This results in an error that can happen non-deterministically, since the memory
309306
// available to the compiler can change between runs. Normally queries are always
310307
// deterministic. However, we can be non-deterministic here because all uses of const
@@ -317,9 +314,7 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
317314
tcx.sess.delay_span_bug(DUMMY_SP, "exhausted memory during interpretation")
318315
});
319316
InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)
320-
};
321-
322-
let bytes = Bytes::zeroed(size, align, handle_alloc_fail)?;
317+
})?;
323318

324319
Ok(Allocation {
325320
bytes,

0 commit comments

Comments
 (0)