Currently, core::slice::from_raw_parts is not const, since it uses debug_assert! with a non-const check:
|
debug_assert!(is_aligned_and_not_null(data), "attempt to create unaligned or null slice"); |
is_aligned_and_not_null can't be made const, since it involves ptr->int cast to check the alignment:
|
pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool { |
|
!ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0 |
|
} |
Recently const_eval_select intrinsic was implemented, it allows to run different code in CTFE and runtime. This, in turn, allows us to only make the alignment check in runtime and ignore it in the CTFE where it doesn't make much sense.
See also: #67456
cc @rust-lang/lang, @rust-lang/libs and @rust-lang/wg-const-eval (it seems like use of const_eval_select requires approval of all of the above teams)
@rustbot label +T-lang +T-libs +A-const-eval +A-const-fn
Currently,
core::slice::from_raw_partsis notconst, since it usesdebug_assert!with a non-constcheck:rust/library/core/src/slice/raw.rs
Line 89 in 5dab47d
is_aligned_and_not_nullcan't be madeconst, since it involves ptr->int cast to check the alignment:rust/library/core/src/intrinsics.rs
Lines 1950 to 1952 in 5dab47d
Recently
const_eval_selectintrinsic was implemented, it allows to run different code in CTFE and runtime. This, in turn, allows us to only make the alignment check in runtime and ignore it in the CTFE where it doesn't make much sense.See also: #67456
cc @rust-lang/lang, @rust-lang/libs and @rust-lang/wg-const-eval (it seems like use of
const_eval_selectrequires approval of all of the above teams)@rustbot label +T-lang +T-libs +A-const-eval +A-const-fn