Skip to content

Commit 8a23051

Browse files
committed
Merge tag 'pin-init-v7.1' of https://github.com/Rust-for-Linux/linux into rust-next
Pull pin-init updates from Benno Lossin: - Replace the 'Zeroable' impls for 'Option<NonZero*>' with impls of 'ZeroableOption' for 'NonZero*'. - Improve feature gate handling for unstable features. - Declutter the documentation of implementations of 'Zeroable' for tuples. - Replace uses of 'addr_of[_mut]!' with '&raw [mut]'. * tag 'pin-init-v7.1' of https://github.com/Rust-for-Linux/linux: rust: pin-init: replace `addr_of_mut!` with `&raw mut` rust: pin-init: implement ZeroableOption for NonZero* integer types rust: pin-init: doc: de-clutter documentation with fake-variadics rust: pin-init: properly document let binding workaround rust: pin-init: build: simplify use of nightly features
2 parents b06b348 + 0980883 commit 8a23051

10 files changed

Lines changed: 75 additions & 36 deletions

File tree

rust/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ syn-flags := \
119119
$(call cfgs-to-flags,$(syn-cfgs))
120120

121121
pin_init_internal-cfgs := \
122-
kernel
122+
kernel USE_RUSTC_FEATURES
123123

124124
pin_init_internal-flags := \
125125
--extern proc_macro2 \
@@ -128,7 +128,7 @@ pin_init_internal-flags := \
128128
$(call cfgs-to-flags,$(pin_init_internal-cfgs))
129129

130130
pin_init-cfgs := \
131-
kernel
131+
kernel USE_RUSTC_FEATURES
132132

133133
pin_init-flags := \
134134
--extern pin_init_internal \

rust/pin-init/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ actually does the initialization in the correct way. Here are the things to look
160160
```rust
161161
use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
162162
use core::{
163-
ptr::addr_of_mut,
164163
marker::PhantomPinned,
165164
cell::UnsafeCell,
166165
pin::Pin,
@@ -199,7 +198,7 @@ impl RawFoo {
199198
unsafe {
200199
pin_init_from_closure(move |slot: *mut Self| {
201200
// `slot` contains uninit memory, avoid creating a reference.
202-
let foo = addr_of_mut!((*slot).foo);
201+
let foo = &raw mut (*slot).foo;
203202
let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();
204203

205204
// Initialize the `foo`

rust/pin-init/examples/big_struct_in_place.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// SPDX-License-Identifier: Apache-2.0 OR MIT
22

3+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
4+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
5+
36
use pin_init::*;
47

58
// Struct with size over 1GiB

rust/pin-init/examples/linked_list.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
#![allow(clippy::undocumented_unsafe_blocks)]
44
#![cfg_attr(feature = "alloc", feature(allocator_api))]
5-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
5+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
6+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
67

78
use core::{
89
cell::Cell,

rust/pin-init/examples/mutex.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
#![allow(clippy::undocumented_unsafe_blocks)]
44
#![cfg_attr(feature = "alloc", feature(allocator_api))]
5-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
5+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
6+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
67
#![allow(clippy::missing_safety_doc)]
78

89
use core::{

rust/pin-init/examples/pthread_mutex.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// inspired by <https://github.com/nbdd0121/pin-init/blob/trunk/examples/pthread_mutex.rs>
44
#![allow(clippy::undocumented_unsafe_blocks)]
55
#![cfg_attr(feature = "alloc", feature(allocator_api))]
6-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
6+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
7+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
78

89
#[cfg(not(windows))]
910
mod pthread_mtx {

rust/pin-init/examples/static_init.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
#![allow(clippy::undocumented_unsafe_blocks)]
44
#![cfg_attr(feature = "alloc", feature(allocator_api))]
5-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
5+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
6+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
67
#![allow(unused_imports)]
78

89
use core::{

rust/pin-init/internal/src/init.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ pub(crate) fn expand(
173173
};
174174
// SAFETY: TODO
175175
let init = unsafe { ::pin_init::#init_from_closure::<_, #error>(init) };
176+
// FIXME: this let binding is required to avoid a compiler error (cycle when computing the
177+
// opaque type returned by this function) before Rust 1.81. Remove after MSRV bump.
178+
#[allow(
179+
clippy::let_and_return,
180+
reason = "some clippy versions warn about the let binding"
181+
)]
176182
init
177183
}})
178184
}
@@ -264,7 +270,7 @@ fn init_fields(
264270
{
265271
#value_prep
266272
// SAFETY: TODO
267-
unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
273+
unsafe { #write(&raw mut (*#slot).#ident, #value_ident) };
268274
}
269275
#(#cfgs)*
270276
#[allow(unused_variables)]
@@ -287,7 +293,7 @@ fn init_fields(
287293
// return when an error/panic occurs.
288294
// - We also use `#data` to require the correct trait (`Init` or `PinInit`)
289295
// for `#ident`.
290-
unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
296+
unsafe { #data.#ident(&raw mut (*#slot).#ident, #init)? };
291297
},
292298
quote! {
293299
// SAFETY: TODO
@@ -302,7 +308,7 @@ fn init_fields(
302308
unsafe {
303309
::pin_init::Init::__init(
304310
#init,
305-
::core::ptr::addr_of_mut!((*#slot).#ident),
311+
&raw mut (*#slot).#ident,
306312
)?
307313
};
308314
},
@@ -342,7 +348,7 @@ fn init_fields(
342348
// SAFETY: We forget the guard later when initialization has succeeded.
343349
let #guard = unsafe {
344350
::pin_init::__internal::DropGuard::new(
345-
::core::ptr::addr_of_mut!((*slot).#ident)
351+
&raw mut (*slot).#ident
346352
)
347353
};
348354
});

rust/pin-init/internal/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
//! `pin-init` proc macros.
88
9-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
9+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
1010
// Documentation is done in the pin-init crate instead.
1111
#![allow(missing_docs)]
1212

rust/pin-init/src/lib.rs

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@
172172
//! # #![feature(extern_types)]
173173
//! use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
174174
//! use core::{
175-
//! ptr::addr_of_mut,
176175
//! marker::PhantomPinned,
177176
//! cell::UnsafeCell,
178177
//! pin::Pin,
@@ -211,7 +210,7 @@
211210
//! unsafe {
212211
//! pin_init_from_closure(move |slot: *mut Self| {
213212
//! // `slot` contains uninit memory, avoid creating a reference.
214-
//! let foo = addr_of_mut!((*slot).foo);
213+
//! let foo = &raw mut (*slot).foo;
215214
//! let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();
216215
//!
217216
//! // Initialize the `foo`
@@ -264,12 +263,10 @@
264263
//! [`impl Init<T, E>`]: crate::Init
265264
//! [Rust-for-Linux]: https://rust-for-linux.com/
266265
267-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
266+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
267+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
268268
#![cfg_attr(
269-
all(
270-
any(feature = "alloc", feature = "std"),
271-
not(RUSTC_NEW_UNINIT_IS_STABLE)
272-
),
269+
all(any(feature = "alloc", feature = "std"), USE_RUSTC_FEATURES),
273270
feature(new_uninit)
274271
)]
275272
#![forbid(missing_docs, unsafe_op_in_unsafe_fn)]
@@ -279,6 +276,8 @@
279276
all(feature = "unsafe-pinned", CONFIG_RUSTC_HAS_UNSAFE_PINNED),
280277
feature(unsafe_pinned)
281278
)]
279+
#![cfg_attr(all(USE_RUSTC_FEATURES, doc), allow(internal_features))]
280+
#![cfg_attr(all(USE_RUSTC_FEATURES, doc), feature(rustdoc_internals))]
282281

283282
use core::{
284283
cell::UnsafeCell,
@@ -755,7 +754,7 @@ macro_rules! stack_try_pin_init {
755754
///
756755
/// ```rust
757756
/// # use pin_init::*;
758-
/// # use core::{ptr::addr_of_mut, marker::PhantomPinned};
757+
/// # use core::marker::PhantomPinned;
759758
/// #[pin_data]
760759
/// #[derive(Zeroable)]
761760
/// struct Buf {
@@ -769,7 +768,7 @@ macro_rules! stack_try_pin_init {
769768
/// let init = pin_init!(&this in Buf {
770769
/// buf: [0; 64],
771770
/// // SAFETY: TODO.
772-
/// ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() },
771+
/// ptr: unsafe { (&raw mut (*this.as_ptr()).buf).cast() },
773772
/// pin: PhantomPinned,
774773
/// });
775774
/// let init = pin_init!(Buf {
@@ -1147,9 +1146,12 @@ pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl Pin
11471146
// SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
11481147
// requirements.
11491148
let res = unsafe { pin_init_from_closure(|ptr: *mut U| init.__pinned_init(ptr.cast::<T>())) };
1150-
// FIXME: remove the let statement once the nightly-MSRV allows it (1.78 otherwise encounters a
1151-
// cycle when computing the type returned by this function)
1152-
#[allow(clippy::let_and_return)]
1149+
// FIXME: this let binding is required to avoid a compiler error (cycle when computing the opaque
1150+
// type returned by this function) before Rust 1.81. Remove after MSRV bump.
1151+
#[allow(
1152+
clippy::let_and_return,
1153+
reason = "some clippy versions warn about the let binding"
1154+
)]
11531155
res
11541156
}
11551157

@@ -1163,9 +1165,12 @@ pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E>
11631165
// SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
11641166
// requirements.
11651167
let res = unsafe { init_from_closure(|ptr: *mut U| init.__init(ptr.cast::<T>())) };
1166-
// FIXME: remove the let statement once the nightly-MSRV allows it (1.78 otherwise encounters a
1167-
// cycle when computing the type returned by this function)
1168-
#[allow(clippy::let_and_return)]
1168+
// FIXME: this let binding is required to avoid a compiler error (cycle when computing the opaque
1169+
// type returned by this function) before Rust 1.81. Remove after MSRV bump.
1170+
#[allow(
1171+
clippy::let_and_return,
1172+
reason = "some clippy versions warn about the let binding"
1173+
)]
11691174
res
11701175
}
11711176

@@ -1610,13 +1615,6 @@ impl_zeroable! {
16101615
// SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
16111616
{<T: ?Sized + Zeroable>} UnsafeCell<T>,
16121617

1613-
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
1614-
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>).
1615-
Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,
1616-
Option<NonZeroU128>, Option<NonZeroUsize>,
1617-
Option<NonZeroI8>, Option<NonZeroI16>, Option<NonZeroI32>, Option<NonZeroI64>,
1618-
Option<NonZeroI128>, Option<NonZeroIsize>,
1619-
16201618
// SAFETY: `null` pointer is valid.
16211619
//
16221620
// We cannot use `T: ?Sized`, since the VTABLE pointer part of fat pointers is not allowed to be
@@ -1635,8 +1633,14 @@ impl_zeroable! {
16351633
}
16361634

16371635
macro_rules! impl_tuple_zeroable {
1638-
($(,)?) => {};
1636+
($first:ident, $(,)?) => {
1637+
#[cfg_attr(all(USE_RUSTC_FEATURES, doc), doc(fake_variadic))]
1638+
/// Implemented for tuples up to 10 items long.
1639+
// SAFETY: All elements are zeroable and padding can be zero.
1640+
unsafe impl<$first: Zeroable> Zeroable for ($first,) {}
1641+
};
16391642
($first:ident, $($t:ident),* $(,)?) => {
1643+
#[cfg_attr(doc, doc(hidden))]
16401644
// SAFETY: All elements are zeroable and padding can be zero.
16411645
unsafe impl<$first: Zeroable, $($t: Zeroable),*> Zeroable for ($first, $($t),*) {}
16421646
impl_tuple_zeroable!($($t),* ,);
@@ -1651,7 +1655,16 @@ macro_rules! impl_fn_zeroable_option {
16511655
$(impl_fn_zeroable_option!({unsafe extern $abi} $args);)*
16521656
};
16531657
({$($prefix:tt)*} {$(,)?}) => {};
1658+
({$($prefix:tt)*} {$ret:ident, $arg:ident $(,)?}) => {
1659+
#[cfg_attr(all(USE_RUSTC_FEATURES, doc), doc(fake_variadic))]
1660+
/// Implemented for function pointers with up to 20 arity.
1661+
// SAFETY: function pointers are part of the option layout optimization:
1662+
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>.
1663+
unsafe impl<$ret, $arg> ZeroableOption for $($prefix)* fn($arg) -> $ret {}
1664+
impl_fn_zeroable_option!({$($prefix)*} {$arg,});
1665+
};
16541666
({$($prefix:tt)*} {$ret:ident, $($rest:ident),* $(,)?}) => {
1667+
#[cfg_attr(doc, doc(hidden))]
16551668
// SAFETY: function pointers are part of the option layout optimization:
16561669
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>.
16571670
unsafe impl<$ret, $($rest),*> ZeroableOption for $($prefix)* fn($($rest),*) -> $ret {}
@@ -1661,6 +1674,20 @@ macro_rules! impl_fn_zeroable_option {
16611674

16621675
impl_fn_zeroable_option!(["Rust", "C"] { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U });
16631676

1677+
macro_rules! impl_non_zero_int_zeroable_option {
1678+
($($int:ty),* $(,)?) => {
1679+
// SAFETY: Safety comment written in the macro invocation.
1680+
$(unsafe impl ZeroableOption for $int {})*
1681+
};
1682+
}
1683+
1684+
impl_non_zero_int_zeroable_option! {
1685+
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
1686+
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>).
1687+
NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
1688+
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize,
1689+
}
1690+
16641691
/// This trait allows creating an instance of `Self` which contains exactly one
16651692
/// [structurally pinned value](https://doc.rust-lang.org/std/pin/index.html#projections-and-structural-pinning).
16661693
///

0 commit comments

Comments
 (0)