Skip to content

Commit 99ba0fa

Browse files
committed
Merge tag 'pin-init-v7.0' of https://github.com/Rust-for-Linux/linux into rust-next
Pull pin-init updates from Benno Lossin: "Added: - Implement 'InPlaceWrite' for '&'static mut MaybeUninit<T>'. This enables users to use external allocation mechanisms such as 'static_cell'. - Add Gary Guo as a Maintainer. Changed: - Rewrote all proc-macros ('[pin_]init!', '#[pin_data]', '#[pinned_drop]', 'derive([Maybe]Zeroable)'), using 'syn' with better diagnostics. - Support tuple structs in 'derive([Maybe]Zeroable)'. - Support attributes on fields in '[pin_]init!' (such as '#[cfg(...)]'). - Add a '#[default_error(<type>)]' attribute to '[pin_]init!' to override the default error (when no '? Error' is specified). - Support packed structs in '[pin_]init!' with '#[disable_initialized_field_access]'. Removed: - Remove 'try_[pin_]init!' in favor of merging their feature with '[pin_]init!'. Update the kernel's own 'try_[pin_]init!' macros to use the 'default_error' attribute. Fixed: - Correct 'T: Sized' bounds to 'T: ?Sized' in the generated 'PinnedDrop' check by '#[pin_data]'." * tag 'pin-init-v7.0' of https://github.com/Rust-for-Linux/linux: rust: pin-init: Implement `InPlaceWrite<T>` for `&'static mut MaybeUninit<T>` MAINTAINERS: add Gary Guo to pin-init rust: pin-init: internal: init: simplify Zeroable safety check rust: pin-init: internal: init: add escape hatch for referencing initialized fields rust: pin-init: internal: init: add support for attributes on initializer fields rust: init: use `#[default_error(err)]` for the initializer macros rust: pin-init: add `#[default_error(<type>)]` attribute to initializer macros rust: pin-init: rewrite the initializer macros using `syn` rust: pin-init: add `?Sized` bounds to traits in `#[pin_data]` macro rust: pin-init: rewrite `#[pin_data]` using `syn` rust: pin-init: rewrite the `#[pinned_drop]` attribute macro using `syn` rust: pin-init: rewrite `derive(Zeroable)` and `derive(MaybeZeroable)` using `syn` rust: pin-init: internal: add utility API for syn error handling rust: pin-init: add `syn` dependency and remove `proc-macro[2]` and `quote` workarounds rust: pin-init: allow the crate to refer to itself as `pin-init` in doc tests rust: pin-init: remove `try_` versions of the initializer macros
2 parents 74a862d + aeb5eca commit 99ba0fa

16 files changed

Lines changed: 1305 additions & 2300 deletions

File tree

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22931,6 +22931,7 @@ F: rust/kernel/num/
2293122931

2293222932
RUST [PIN-INIT]
2293322933
M: Benno Lossin <lossin@kernel.org>
22934+
M: Gary Guo <gary@garyguo.net>
2293422935
L: rust-for-linux@vger.kernel.org
2293522936
S: Maintained
2293622937
W: https://rust-for-linux.com/pin-init

rust/Makefile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,10 @@ rustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
212212

213213
rustdoc-pin_init_internal: private rustdoc_host = yes
214214
rustdoc-pin_init_internal: private rustc_target_flags = --cfg kernel \
215-
--extern proc_macro --crate-type proc-macro
215+
--extern proc_macro --extern proc_macro2 --extern quote --extern syn \
216+
--crate-type proc-macro
216217
rustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
217-
rustdoc-clean FORCE
218+
rustdoc-clean rustdoc-proc_macro2 rustdoc-quote rustdoc-syn FORCE
218219
+$(call if_changed,rustdoc)
219220

220221
rustdoc-pin_init: private rustdoc_host = yes
@@ -273,9 +274,10 @@ rusttestlib-macros: $(src)/macros/lib.rs \
273274
+$(call if_changed,rustc_test_library)
274275

275276
rusttestlib-pin_init_internal: private rustc_target_flags = --cfg kernel \
276-
--extern proc_macro
277+
--extern proc_macro --extern proc_macro2 --extern quote --extern syn
277278
rusttestlib-pin_init_internal: private rustc_test_library_proc = yes
278-
rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs FORCE
279+
rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
280+
rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
279281
+$(call if_changed,rustc_test_library)
280282

281283
rusttestlib-pin_init: private rustc_target_flags = --extern pin_init_internal \
@@ -547,8 +549,10 @@ $(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib \
547549
$(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
548550
+$(call if_changed_dep,rustc_procmacro)
549551

550-
$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel
551-
$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs FORCE
552+
$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel \
553+
--extern proc_macro2 --extern quote --extern syn
554+
$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
555+
$(obj)/libproc_macro2.rlib $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
552556
+$(call if_changed_dep,rustc_procmacro)
553557

554558
quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@

rust/kernel/init.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -219,20 +219,12 @@ pub trait InPlaceInit<T>: Sized {
219219
/// [`Error`]: crate::error::Error
220220
#[macro_export]
221221
macro_rules! try_init {
222-
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
223-
$($fields:tt)*
224-
}) => {
225-
::pin_init::try_init!($(&$this in)? $t $(::<$($generics),*>)? {
226-
$($fields)*
227-
}? $crate::error::Error)
228-
};
229-
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
230-
$($fields:tt)*
231-
}? $err:ty) => {
232-
::pin_init::try_init!($(&$this in)? $t $(::<$($generics),*>)? {
233-
$($fields)*
234-
}? $err)
235-
};
222+
($($args:tt)*) => {
223+
::pin_init::init!(
224+
#[default_error($crate::error::Error)]
225+
$($args)*
226+
)
227+
}
236228
}
237229

238230
/// Construct an in-place, fallible pinned initializer for `struct`s.
@@ -279,18 +271,10 @@ macro_rules! try_init {
279271
/// [`Error`]: crate::error::Error
280272
#[macro_export]
281273
macro_rules! try_pin_init {
282-
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
283-
$($fields:tt)*
284-
}) => {
285-
::pin_init::try_pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
286-
$($fields)*
287-
}? $crate::error::Error)
288-
};
289-
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
290-
$($fields:tt)*
291-
}? $err:ty) => {
292-
::pin_init::try_pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
293-
$($fields)*
294-
}? $err)
295-
};
274+
($($args:tt)*) => {
275+
::pin_init::pin_init!(
276+
#[default_error($crate::error::Error)]
277+
$($args)*
278+
)
279+
}
296280
}

rust/pin-init/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ struct DriverData {
135135

136136
impl DriverData {
137137
fn new() -> impl PinInit<Self, Error> {
138-
try_pin_init!(Self {
138+
pin_init!(Self {
139139
status <- CMutex::new(0),
140140
buffer: Box::init(pin_init::init_zeroed())?,
141141
}? Error)

rust/pin-init/examples/linked_list.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use core::{
88
cell::Cell,
9-
convert::Infallible,
109
marker::PhantomPinned,
1110
pin::Pin,
1211
ptr::{self, NonNull},
@@ -31,31 +30,31 @@ pub struct ListHead {
3130

3231
impl ListHead {
3332
#[inline]
34-
pub fn new() -> impl PinInit<Self, Infallible> {
35-
try_pin_init!(&this in Self {
33+
pub fn new() -> impl PinInit<Self> {
34+
pin_init!(&this in Self {
3635
next: unsafe { Link::new_unchecked(this) },
3736
prev: unsafe { Link::new_unchecked(this) },
3837
pin: PhantomPinned,
39-
}? Infallible)
38+
})
4039
}
4140

4241
#[inline]
4342
#[allow(dead_code)]
44-
pub fn insert_next(list: &ListHead) -> impl PinInit<Self, Infallible> + '_ {
45-
try_pin_init!(&this in Self {
43+
pub fn insert_next(list: &ListHead) -> impl PinInit<Self> + '_ {
44+
pin_init!(&this in Self {
4645
prev: list.next.prev().replace(unsafe { Link::new_unchecked(this)}),
4746
next: list.next.replace(unsafe { Link::new_unchecked(this)}),
4847
pin: PhantomPinned,
49-
}? Infallible)
48+
})
5049
}
5150

5251
#[inline]
53-
pub fn insert_prev(list: &ListHead) -> impl PinInit<Self, Infallible> + '_ {
54-
try_pin_init!(&this in Self {
52+
pub fn insert_prev(list: &ListHead) -> impl PinInit<Self> + '_ {
53+
pin_init!(&this in Self {
5554
next: list.prev.next().replace(unsafe { Link::new_unchecked(this)}),
5655
prev: list.prev.replace(unsafe { Link::new_unchecked(this)}),
5756
pin: PhantomPinned,
58-
}? Infallible)
57+
})
5958
}
6059

6160
#[inline]

rust/pin-init/examples/pthread_mutex.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ mod pthread_mtx {
9898
// SAFETY: mutex has been initialized
9999
unsafe { pin_init_from_closure(init) }
100100
}
101-
try_pin_init!(Self {
102-
data: UnsafeCell::new(data),
103-
raw <- init_raw(),
104-
pin: PhantomPinned,
105-
}? Error)
101+
pin_init!(Self {
102+
data: UnsafeCell::new(data),
103+
raw <- init_raw(),
104+
pin: PhantomPinned,
105+
}? Error)
106106
}
107107

108108
#[allow(dead_code)]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
3+
use std::fmt::Display;
4+
5+
use proc_macro2::TokenStream;
6+
use syn::{spanned::Spanned, Error};
7+
8+
pub(crate) struct DiagCtxt(TokenStream);
9+
pub(crate) struct ErrorGuaranteed(());
10+
11+
impl DiagCtxt {
12+
pub(crate) fn error(&mut self, span: impl Spanned, msg: impl Display) -> ErrorGuaranteed {
13+
let error = Error::new(span.span(), msg);
14+
self.0.extend(error.into_compile_error());
15+
ErrorGuaranteed(())
16+
}
17+
18+
pub(crate) fn with(
19+
fun: impl FnOnce(&mut DiagCtxt) -> Result<TokenStream, ErrorGuaranteed>,
20+
) -> TokenStream {
21+
let mut dcx = Self(TokenStream::new());
22+
match fun(&mut dcx) {
23+
Ok(mut stream) => {
24+
stream.extend(dcx.0);
25+
stream
26+
}
27+
Err(ErrorGuaranteed(())) => dcx.0,
28+
}
29+
}
30+
}

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

Lines changed: 0 additions & 152 deletions
This file was deleted.

0 commit comments

Comments
 (0)