Skip to content

Commit e3df912

Browse files
authored
Merge pull request #89 from Rust-for-Linux/dev/syn2
- tests: test extraneous argument in `#[pin_data]` - internal: init: simplify Zeroable safety check - tests: track no_field_access expansion - tests: update prettyplease to support modern expressions - tests: track the diagnostics of packed structs and field accessors - tests: ensure that `#[disable_initialized_field_access]` does not emit accessors - internal: init: add escape hatch for referencing initialized fields - tests: add test to check cfgs working correctly - internal: init: add support for attributes on initializer fields - add `#[default_error(<type>)]` attribute to initializer macros - rewrite the initializer macros using `syn` - add `?Sized` bounds to traits in `#[pin_data]` macro - rewrite `#[pin_data]` using `syn` - rewrite the `#[pinned_drop]` attribute macro using `syn` - rewrite `derive(Zeroable)` and `derive(MaybeZeroable)` using `syn` - internal: add utility API for syn error handling - add `syn` dependency and remove `proc-macro[2]` and `quote` workarounds - allow the crate to refer to itself as `pin-init` in doc tests - remove `try_` versions of the initializer macros - tests: add `MaybeZeroable` expansion test with generics - tests: add new test case to underscore - tests: ensure that the data cannot be accessed - tests: ensure the guards cannot be accessed - tests: check that enums cannot derive `Zeroable`
2 parents 470c122 + 8ceb14e commit e3df912

73 files changed

Lines changed: 1868 additions & 2650 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- `[pin_]init_scope` functions to run arbitrary code inside of an initializer.
1313
- `&'static mut MaybeUninit<T>` now implements `InPlaceWrite`. This enables users to use external
1414
allocation mechanisms such as `static_cell`.
15+
- Rewrote all proc-macros using `syn`.
1516

1617
### Changed
1718

@@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2122
code at that point.
2223
- Make the `[try_][pin_]init!` macros expose initialized fields via a `let`
2324
binding as `&mut T` or `Pin<&mut T>` for later fields.
25+
- `derive([Maybe]Zeroable)` now support tuple structs
2426

2527
## [0.0.10] - 2025-08-19
2628

Cargo.lock

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ libc = "0.2"
3131
trybuild = { version = "1.0", features = ["diff"] }
3232
macrotest = "1.0"
3333
# needed for macrotest, have to enable verbatim feature to be able to format `&raw` expressions.
34-
prettyplease = { version = "0.2", features = ["verbatim"] }
34+
prettyplease = { version = "0.2.37", features = ["verbatim"] }
3535

3636
[lints.rust]
3737
non_ascii_idents = "deny"

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)

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]

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)]

internal/Cargo.lock

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ proc-macro = true
1515
[dependencies]
1616
quote = "1.0"
1717
proc-macro2 = "1.0"
18+
syn = { version = "2.0.86", features = ["full", "parsing", "visit-mut"] }
1819

1920
[build-dependencies]
2021
rustc_version = "0.4"

internal/src/diagnostics.rs

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+
}

internal/src/helpers.rs

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

0 commit comments

Comments
 (0)