Add safety section for atomic_load/store - #162854
Conversation
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
These intrinsics exist only for internal use. What is the point of repeating the docs of the public methods here? |
|
The reason I refer to Although these two functions are for internel use, adding safety section may still be useful? The implementation of My basic idea is this function use raw pointer and need the caller to ensure the safety, so it's necessary to add safety section. But as what you say, this is for internal use. |
|
I think we need some policy on whether an intrinsic API needs a safety doc. Currently, some intrinsics have safety docs while others do not. See this table. I agree with @yilin0518 that even if an intrinsic is intended for internal use only, documenting its safety requirements is still valuable for checking or verifying the soundness of the standard library. |
|
I agree that ideally, intrinsics should have safety docs. OTOH, I would like to avoid duplicating complicated safety docs in cases where we have equivalent public stable functions with carefully crafted safety docs. For atomic_load/store
|
47ec4b7 to
8a1e154
Compare
|
@RalfJung thank you for your feedback and support! I have update the commit. Hoping this PR can improve the safety doc of intrinsics. |
|
This works, thanks. :) @bors r+ rollup |
Add safety section for atomic_load/store This PR tries to add `# Safety` section for atomic_load/store in intrinsic module. I notice that some intrinsic unsafe functions already have `# Safety` section. And for these two functions, they have corresponding stable version functions in `core/sync`. But in the stable implementation, I notice that they first call an unsafe `atomic_load/store` defined in the same file(a private function without safety doc), and that unsafe function directly call `atomic_load/store` defined in intrinsic module(for example, [atomic_load](https://doc.rust-lang.org/std/intrinsics/fn.atomic_load.html)). Here is the implementaion of [atomic_load](https://doc.rust-lang.org/src/core/sync/atomic.rs.html#3886) used in AtomicBool::load: ```rust #[inline] #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces unsafe fn atomic_load<T: Copy>(dst: *const T, order: Ordering) -> T { // SAFETY: the caller must uphold the safety contract for `atomic_load`. unsafe { match order { Relaxed => intrinsics::atomic_load::<T, { AO::Relaxed }>(dst), Acquire => intrinsics::atomic_load::<T, { AO::Acquire }>(dst), SeqCst => intrinsics::atomic_load::<T, { AO::SeqCst }>(dst), Release => panic!("there is no such thing as a release load"), AcqRel => panic!("there is no such thing as an acquire-release load"), } } } ``` So I'm trying to add `# Safety` section for the intrinsic atomic_load/store. Although intrinsic API mainly used for Rust standary library, I think that adding `# Safety` section is needed because it pass a raw pointer. When writing the `# Safety` section for these two functions, I refer to [read_volatile](https://doc.rust-lang.org/std/ptr/fn.read_volatile.html) and [write_volatile](https://doc.rust-lang.org/std/ptr/fn.write_volatile.html). If needed, I will review all the atomic operations defined in intrinsic module. Thank you for your review and I'm looking forward to your feedback. Hoping this PR can improve the safety doc of Rust standard library.
…uwer Rollup of 15 pull requests Successful merges: - #162726 (std: fix unix socket address panic on a full sun_path) - #163016 (Don't claim that escaping value is a reference in diagnostics) - #163040 (Tweak "use array's length as const param" suggestion) - #163060 (Point to fields that introduce trait requirements) - #163066 (don't mark `f128` as reliable on AIX) - #162098 (Tweak `Infallible` docs) - #162854 (Add safety section for atomic_load/store) - #163015 (add `minicore::ffi::VaList`) - #163021 (`va_arg`: pass in `TyAndLayout`) - #163036 ([rustdoc] Correctly handle `dyn` trait methods linking for jump to def feature) - #163042 (Remove redundant output from suggestion) - #163046 (Use verbose suggestion for `const _`) - #163050 (Use verbose suggestion for similarly named label suggestion) - #163052 (Use verbose suggestion for wrong primitive type names) - #163055 (Use the full path of `bug_impl` to avoid bogus errors in rust-analyzer)
…uwer Rollup of 15 pull requests Successful merges: - #162726 (std: fix unix socket address panic on a full sun_path) - #163016 (Don't claim that escaping value is a reference in diagnostics) - #163040 (Tweak "use array's length as const param" suggestion) - #163060 (Point to fields that introduce trait requirements) - #163066 (don't mark `f128` as reliable on AIX) - #162098 (Tweak `Infallible` docs) - #162854 (Add safety section for atomic_load/store) - #163015 (add `minicore::ffi::VaList`) - #163021 (`va_arg`: pass in `TyAndLayout`) - #163036 ([rustdoc] Correctly handle `dyn` trait methods linking for jump to def feature) - #163042 (Remove redundant output from suggestion) - #163046 (Use verbose suggestion for `const _`) - #163050 (Use verbose suggestion for similarly named label suggestion) - #163052 (Use verbose suggestion for wrong primitive type names) - #163055 (Use the full path of `bug_impl` to avoid bogus errors in rust-analyzer)
…uwer Rollup of 15 pull requests Successful merges: - #162726 (std: fix unix socket address panic on a full sun_path) - #163016 (Don't claim that escaping value is a reference in diagnostics) - #163040 (Tweak "use array's length as const param" suggestion) - #163060 (Point to fields that introduce trait requirements) - #163066 (don't mark `f128` as reliable on AIX) - #162098 (Tweak `Infallible` docs) - #162854 (Add safety section for atomic_load/store) - #163015 (add `minicore::ffi::VaList`) - #163021 (`va_arg`: pass in `TyAndLayout`) - #163036 ([rustdoc] Correctly handle `dyn` trait methods linking for jump to def feature) - #163042 (Remove redundant output from suggestion) - #163046 (Use verbose suggestion for `const _`) - #163050 (Use verbose suggestion for similarly named label suggestion) - #163052 (Use verbose suggestion for wrong primitive type names) - #163055 (Use the full path of `bug_impl` to avoid bogus errors in rust-analyzer)
…uwer Rollup of 15 pull requests Successful merges: - #162726 (std: fix unix socket address panic on a full sun_path) - #163016 (Don't claim that escaping value is a reference in diagnostics) - #163040 (Tweak "use array's length as const param" suggestion) - #163060 (Point to fields that introduce trait requirements) - #163066 (don't mark `f128` as reliable on AIX) - #162098 (Tweak `Infallible` docs) - #162854 (Add safety section for atomic_load/store) - #163015 (add `minicore::ffi::VaList`) - #163021 (`va_arg`: pass in `TyAndLayout`) - #163036 ([rustdoc] Correctly handle `dyn` trait methods linking for jump to def feature) - #163042 (Remove redundant output from suggestion) - #163046 (Use verbose suggestion for `const _`) - #163050 (Use verbose suggestion for similarly named label suggestion) - #163052 (Use verbose suggestion for wrong primitive type names) - #163055 (Use the full path of `bug_impl` to avoid bogus errors in rust-analyzer)
Rollup merge of #162854 - yilin0518:fix_atomic_op, r=RalfJung Add safety section for atomic_load/store This PR tries to add `# Safety` section for atomic_load/store in intrinsic module. I notice that some intrinsic unsafe functions already have `# Safety` section. And for these two functions, they have corresponding stable version functions in `core/sync`. But in the stable implementation, I notice that they first call an unsafe `atomic_load/store` defined in the same file(a private function without safety doc), and that unsafe function directly call `atomic_load/store` defined in intrinsic module(for example, [atomic_load](https://doc.rust-lang.org/std/intrinsics/fn.atomic_load.html)). Here is the implementaion of [atomic_load](https://doc.rust-lang.org/src/core/sync/atomic.rs.html#3886) used in AtomicBool::load: ```rust #[inline] #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces unsafe fn atomic_load<T: Copy>(dst: *const T, order: Ordering) -> T { // SAFETY: the caller must uphold the safety contract for `atomic_load`. unsafe { match order { Relaxed => intrinsics::atomic_load::<T, { AO::Relaxed }>(dst), Acquire => intrinsics::atomic_load::<T, { AO::Acquire }>(dst), SeqCst => intrinsics::atomic_load::<T, { AO::SeqCst }>(dst), Release => panic!("there is no such thing as a release load"), AcqRel => panic!("there is no such thing as an acquire-release load"), } } } ``` So I'm trying to add `# Safety` section for the intrinsic atomic_load/store. Although intrinsic API mainly used for Rust standary library, I think that adding `# Safety` section is needed because it pass a raw pointer. When writing the `# Safety` section for these two functions, I refer to [read_volatile](https://doc.rust-lang.org/std/ptr/fn.read_volatile.html) and [write_volatile](https://doc.rust-lang.org/std/ptr/fn.write_volatile.html). If needed, I will review all the atomic operations defined in intrinsic module. Thank you for your review and I'm looking forward to your feedback. Hoping this PR can improve the safety doc of Rust standard library.
This PR tries to add
# Safetysection for atomic_load/store in intrinsic module. I notice that some intrinsic unsafe functions already have# Safetysection. And for these two functions, they have corresponding stable version functions incore/sync. But in the stable implementation, I notice that they first call an unsafeatomic_load/storedefined in the same file(a private function without safety doc), and that unsafe function directly callatomic_load/storedefined in intrinsic module(for example, atomic_load). Here is the implementaion of atomic_load used in AtomicBool::load:So I'm trying to add
# Safetysection for the intrinsic atomic_load/store. Although intrinsic API mainly used for Rust standary library, I think that adding# Safetysection is needed because it pass a raw pointer. When writing the# Safetysection for these two functions, I refer to read_volatile and write_volatile.If needed, I will review all the atomic operations defined in intrinsic module. Thank you for your review and I'm looking forward to your feedback. Hoping this PR can improve the safety doc of Rust standard library.