Skip to content

Add and modify safety descriptions in some of intrinsics APIs #135334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,10 @@ pub fn ptr_mask<T>(_ptr: *const T, _mask: usize) -> *const T {
/// The volatile parameter is set to `true`, so it will not be optimized out
/// unless size is equal to zero.
///
/// # Safety
///
/// The safety concerns are the same with [`copy_nonoverlapping`].
///
/// This intrinsic does not have a stable counterpart.
#[rustc_intrinsic]
#[rustc_intrinsic_must_be_overridden]
Expand Down Expand Up @@ -2042,6 +2046,18 @@ pub unsafe fn volatile_copy_memory<T>(_dst: *mut T, _src: *const T, _count: usiz
/// The volatile parameter is set to `true`, so it will not be optimized out
/// unless size is equal to zero.
///
/// # Safety
Copy link
Member

@RalfJung RalfJung Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be much more important do clarify here is whether both the read and the write are volatile, or only one of them. Also, "the volatile parameter is set to true" makes no sense, that's talking about a parameter inside the codegen backend that shouldn't show up in such docs. Same for llvm.memset.p0i8; maybe it's useful to mention it but the first summary should not refer to LLVM-specific concepts.

///
/// Behavior is undefined if any of the following conditions are violated:
///
/// * `_dst` must be [valid] for writes of `_count * size_of::<T>()` bytes.
///
/// * `_dst` must be properly aligned.
///
/// Note that even if `T` has size `0`, the pointer must be properly aligned.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just point at write_bytes here, the safety requirements are the same.

Except of course they aren't really -- these are volatile operations which can be used in ways that would be invalid for write_bytes. The same applies to copy_nonoverlapping vs volatile_copy_nonoverlapping_memory.

///
/// [valid]: crate::ptr#safety
///
/// This intrinsic does not have a stable counterpart.
#[rustc_intrinsic]
#[rustc_intrinsic_must_be_overridden]
Expand Down Expand Up @@ -3965,8 +3981,16 @@ pub const fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
/// The stabilized form of this intrinsic is [`crate::mem::swap`].
///
/// # Safety
/// Behavior is undefined if any of the following conditions are violated:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incomplete. The operation is typed, so the pointers must point to data that is valid at the given type.

///
/// `x` and `y` are readable and writable as `T`, and non-overlapping.
/// * Both `x` and `y` must be [valid] for both reads and writes.
///
/// * Both `x` and `y` must be properly aligned.
///
/// * The region of memory beginning at `x` must *not* overlap with the region of memory
/// beginning at `y`.
///
/// [valid]: crate::ptr#safety
#[rustc_nounwind]
#[inline]
#[rustc_intrinsic]
Expand Down
Loading