Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b502efb

Browse files
committedNov 16, 2024
clean up const stability around UB checks
1 parent a1120b3 commit b502efb

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed
 

‎library/core/src/intrinsics/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4013,9 +4013,9 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
40134013
count: usize = count,
40144014
) => {
40154015
let zero_size = count == 0 || size == 0;
4016-
ub_checks::is_aligned_and_not_null(src, align, zero_size)
4017-
&& ub_checks::is_aligned_and_not_null(dst, align, zero_size)
4018-
&& ub_checks::is_nonoverlapping(src, dst, size, count)
4016+
ub_checks::maybe_is_aligned_and_not_null(src, align, zero_size)
4017+
&& ub_checks::maybe_is_aligned_and_not_null(dst, align, zero_size)
4018+
&& ub_checks::maybe_is_nonoverlapping(src, dst, size, count)
40194019
}
40204020
);
40214021

@@ -4119,8 +4119,8 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
41194119
align: usize = align_of::<T>(),
41204120
zero_size: bool = T::IS_ZST || count == 0,
41214121
) =>
4122-
ub_checks::is_aligned_and_not_null(src, align, zero_size)
4123-
&& ub_checks::is_aligned_and_not_null(dst, align, zero_size)
4122+
ub_checks::maybe_is_aligned_and_not_null(src, align, zero_size)
4123+
&& ub_checks::maybe_is_aligned_and_not_null(dst, align, zero_size)
41244124
);
41254125
copy(src, dst, count)
41264126
}
@@ -4201,7 +4201,7 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
42014201
addr: *const () = dst as *const (),
42024202
align: usize = align_of::<T>(),
42034203
zero_size: bool = T::IS_ZST || count == 0,
4204-
) => ub_checks::is_aligned_and_not_null(addr, align, zero_size)
4204+
) => ub_checks::maybe_is_aligned_and_not_null(addr, align, zero_size)
42054205
);
42064206
write_bytes(dst, val, count)
42074207
}

‎library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
// tidy-alphabetical-start
110110
#![cfg_attr(bootstrap, feature(const_exact_div))]
111111
#![cfg_attr(bootstrap, feature(const_fmt_arguments_new))]
112+
#![cfg_attr(bootstrap, feature(const_ub_checks))]
112113
#![feature(array_ptr_get)]
113114
#![feature(asm_experimental_arch)]
114115
#![feature(const_align_of_val)]
@@ -131,7 +132,6 @@
131132
#![feature(const_type_id)]
132133
#![feature(const_type_name)]
133134
#![feature(const_typed_swap)]
134-
#![feature(const_ub_checks)]
135135
#![feature(core_intrinsics)]
136136
#![feature(coverage_attribute)]
137137
#![feature(do_not_recommend)]

‎library/core/src/ptr/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1103,9 +1103,9 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
11031103
count: usize = count,
11041104
) => {
11051105
let zero_size = size == 0 || count == 0;
1106-
ub_checks::is_aligned_and_not_null(x, align, zero_size)
1107-
&& ub_checks::is_aligned_and_not_null(y, align, zero_size)
1108-
&& ub_checks::is_nonoverlapping(x, y, size, count)
1106+
ub_checks::maybe_is_aligned_and_not_null(x, align, zero_size)
1107+
&& ub_checks::maybe_is_aligned_and_not_null(y, align, zero_size)
1108+
&& ub_checks::maybe_is_nonoverlapping(x, y, size, count)
11091109
}
11101110
);
11111111

@@ -1216,7 +1216,7 @@ pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
12161216
addr: *const () = dst as *const (),
12171217
align: usize = align_of::<T>(),
12181218
is_zst: bool = T::IS_ZST,
1219-
) => ub_checks::is_aligned_and_not_null(addr, align, is_zst)
1219+
) => ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
12201220
);
12211221
mem::replace(&mut *dst, src)
12221222
}
@@ -1369,7 +1369,7 @@ pub const unsafe fn read<T>(src: *const T) -> T {
13691369
addr: *const () = src as *const (),
13701370
align: usize = align_of::<T>(),
13711371
is_zst: bool = T::IS_ZST,
1372-
) => ub_checks::is_aligned_and_not_null(addr, align, is_zst)
1372+
) => ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
13731373
);
13741374
crate::intrinsics::read_via_copy(src)
13751375
}
@@ -1573,7 +1573,7 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
15731573
addr: *mut () = dst as *mut (),
15741574
align: usize = align_of::<T>(),
15751575
is_zst: bool = T::IS_ZST,
1576-
) => ub_checks::is_aligned_and_not_null(addr, align, is_zst)
1576+
) => ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
15771577
);
15781578
intrinsics::write_via_move(dst, src)
15791579
}
@@ -1745,7 +1745,7 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
17451745
addr: *const () = src as *const (),
17461746
align: usize = align_of::<T>(),
17471747
is_zst: bool = T::IS_ZST,
1748-
) => ub_checks::is_aligned_and_not_null(addr, align, is_zst)
1748+
) => ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
17491749
);
17501750
intrinsics::volatile_load(src)
17511751
}
@@ -1825,7 +1825,7 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
18251825
addr: *mut () = dst as *mut (),
18261826
align: usize = align_of::<T>(),
18271827
is_zst: bool = T::IS_ZST,
1828-
) => ub_checks::is_aligned_and_not_null(addr, align, is_zst)
1828+
) => ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
18291829
);
18301830
intrinsics::volatile_store(dst, src);
18311831
}

‎library/core/src/slice/raw.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T]
132132
align: usize = align_of::<T>(),
133133
len: usize = len,
134134
) =>
135-
ub_checks::is_aligned_and_not_null(data, align, false)
135+
ub_checks::maybe_is_aligned_and_not_null(data, align, false)
136136
&& ub_checks::is_valid_allocation_size(size, len)
137137
);
138138
&*ptr::slice_from_raw_parts(data, len)
@@ -186,7 +186,7 @@ pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a m
186186
align: usize = align_of::<T>(),
187187
len: usize = len,
188188
) =>
189-
ub_checks::is_aligned_and_not_null(data, align, false)
189+
ub_checks::maybe_is_aligned_and_not_null(data, align, false)
190190
&& ub_checks::is_valid_allocation_size(size, len)
191191
);
192192
&mut *ptr::slice_from_raw_parts_mut(data, len)

‎library/core/src/ub_checks.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ macro_rules! assert_unsafe_precondition {
6464
#[rustc_no_mir_inline]
6565
#[inline]
6666
#[rustc_nounwind]
67-
#[cfg_attr(bootstrap, rustc_const_unstable(feature = "const_ub_checks", issue = "none"))]
68-
#[rustc_allow_const_fn_unstable(const_ub_checks)] // only for UB checks
6967
const fn precondition_check($($name:$ty),*) {
7068
if !$e {
7169
::core::panicking::panic_nounwind(
@@ -116,12 +114,16 @@ pub(crate) const fn check_language_ub() -> bool {
116114
/// for `assert_unsafe_precondition!` with `check_language_ub`, in which case the
117115
/// check is anyway not executed in `const`.
118116
#[inline]
119-
#[rustc_const_unstable(feature = "const_ub_checks", issue = "none")]
120-
pub(crate) const fn is_aligned_and_not_null(ptr: *const (), align: usize, is_zst: bool) -> bool {
117+
#[rustc_allow_const_fn_unstable(const_eval_select)]
118+
pub(crate) const fn maybe_is_aligned_and_not_null(
119+
ptr: *const (),
120+
align: usize,
121+
is_zst: bool,
122+
) -> bool {
121123
// This is just for safety checks so we can const_eval_select.
122124
const_eval_select!(
123125
@capture { ptr: *const (), align: usize, is_zst: bool } -> bool:
124-
if const #[rustc_const_unstable(feature = "const_ub_checks", issue = "none")] {
126+
if const {
125127
is_zst || !ptr.is_null()
126128
} else {
127129
ptr.is_aligned_to(align) && (is_zst || !ptr.is_null())
@@ -141,8 +143,8 @@ pub(crate) const fn is_valid_allocation_size(size: usize, len: usize) -> bool {
141143
/// Note that in const-eval this function just returns `true` and therefore must
142144
/// only be used with `assert_unsafe_precondition!`, similar to `is_aligned_and_not_null`.
143145
#[inline]
144-
#[rustc_const_unstable(feature = "const_ub_checks", issue = "none")]
145-
pub(crate) const fn is_nonoverlapping(
146+
#[rustc_allow_const_fn_unstable(const_eval_select)]
147+
pub(crate) const fn maybe_is_nonoverlapping(
146148
src: *const (),
147149
dst: *const (),
148150
size: usize,

0 commit comments

Comments
 (0)
Failed to load comments.