Skip to content

Commit b34f5b9

Browse files
committed
fix CI; use assert_unsafe_precondition
1 parent cfd9ece commit b34f5b9

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

library/core/src/mem/valid_align.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::convert::TryFrom;
2+
use crate::intrinsics::assert_unsafe_precondition;
23
use crate::num::NonZeroUsize;
34
use crate::{cmp, fmt, hash, mem, num};
45

@@ -26,11 +27,12 @@ impl ValidAlign {
2627
/// It must *not* be zero.
2728
#[inline]
2829
pub(crate) const unsafe fn new_unchecked(align: usize) -> Self {
29-
debug_assert!(align.is_power_of_two());
30-
3130
// SAFETY: By precondition, this must be a power of two, and
3231
// our variants encompass all possible powers of two.
33-
unsafe { mem::transmute::<usize, ValidAlign>(align) }
32+
unsafe {
33+
assert_unsafe_precondition!(align.is_power_of_two());
34+
mem::transmute::<usize, ValidAlign>(align)
35+
}
3436
}
3537

3638
#[inline]

library/core/src/mem/valid_size.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::convert::TryFrom;
2+
use crate::intrinsics::assert_unsafe_precondition;
23
use crate::{fmt, num};
34

45
/// A type storing a possible object size (in bytes) in the rust abstract machine.
@@ -19,8 +20,6 @@ pub(crate) struct ValidSize(usize);
1920

2021
const MAX_SIZE: usize = isize::MAX as usize;
2122

22-
const _: () = unsafe { ValidSize::new_unchecked(MAX_SIZE); };
23-
2423
impl ValidSize {
2524
/// Creates a `ValidSize` from a `usize` that fits in an `isize`.
2625
///
@@ -31,10 +30,11 @@ impl ValidSize {
3130
/// Equivalently, it must not have its high bit set.
3231
#[inline]
3332
pub(crate) const unsafe fn new_unchecked(size: usize) -> Self {
34-
debug_assert!(size <= MAX_SIZE);
35-
3633
// SAFETY: By precondition, this must be within our validity invariant.
37-
unsafe { ValidSize(size) }
34+
unsafe {
35+
assert_unsafe_precondition!(size <= MAX_SIZE);
36+
ValidSize(size)
37+
}
3838
}
3939

4040
#[inline]

src/test/ui/consts/std/alloc.32bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ LL | const LAYOUT_SIZE_NEGATIVE_ONE: Layout = unsafe { Layout::from_size_align_u
3434
error[E0080]: it is undefined behavior to use this value
3535
--> $DIR/alloc.rs:21:1
3636
|
37-
LL | const LAYOUT_SIZE_HIGH_BIT: Layout = unsafe { Layout::from_size_align_unchecked((isize::MAX as usize) + 1, 1) };
37+
LL | const LAYOUT_SIZE_HIGH_BIT: Layout = unsafe { Layout::from_size_align_unchecked(SIZE_MAX + 1, 1) };
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .size: encountered 2147483648, but expected something less or equal to 2147483647
3939
|
4040
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.

src/test/ui/consts/std/alloc.64bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ LL | const LAYOUT_SIZE_NEGATIVE_ONE: Layout = unsafe { Layout::from_size_align_u
3434
error[E0080]: it is undefined behavior to use this value
3535
--> $DIR/alloc.rs:21:1
3636
|
37-
LL | const LAYOUT_SIZE_HIGH_BIT: Layout = unsafe { Layout::from_size_align_unchecked((isize::MAX as usize) + 1, 1) };
37+
LL | const LAYOUT_SIZE_HIGH_BIT: Layout = unsafe { Layout::from_size_align_unchecked(SIZE_MAX + 1, 1) };
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .size: encountered 9223372036854775808, but expected something less or equal to 9223372036854775807
3939
|
4040
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.

src/test/ui/consts/std/alloc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ const LAYOUT_SIZE_NEGATIVE_ONE: Layout = unsafe { Layout::from_size_align_unchec
1818
//~^ ERROR it is undefined behavior to use this value
1919

2020
// not ok, since size needs to be no more than `isize::MAX`
21-
const LAYOUT_SIZE_HIGH_BIT: Layout = unsafe { Layout::from_size_align_unchecked((isize::MAX as usize) + 1, 1) };
21+
const LAYOUT_SIZE_HIGH_BIT: Layout = unsafe { Layout::from_size_align_unchecked(SIZE_MAX + 1, 1) };
2222
//~^ ERROR it is undefined behavior to use this value
2323

24+
const SIZE_MAX: usize = isize::MAX as usize;
25+
2426
fn main() {}

0 commit comments

Comments
 (0)