Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ trybuild = { version = "1.0", features = ["diff"] }
macrotest = "1.0"
# needed for macrotest, have to enable verbatim feature to be able to format `&raw` expressions.
prettyplease = { version = "0.2.37", features = ["verbatim"] }
dummy = { path = "tests/dummy" }

[lints.rust]
stable_features = "allow"
Expand Down
17 changes: 11 additions & 6 deletions internal/src/pin_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use syn::{
parse_quote, parse_quote_spanned,
spanned::Spanned,
visit_mut::VisitMut,
Field, Generics, Ident, Item, PathSegment, Type, TypePath, Visibility, WhereClause,
Attribute, Field, Generics, Ident, Item, PathSegment, Type, TypePath, Visibility, WhereClause,
};

use crate::diagnostics::{DiagCtxt, ErrorGuaranteed};
Expand Down Expand Up @@ -35,6 +35,11 @@ impl Parse for Args {
}
}

fn keep_attr(a: &Attribute) -> bool {
let p = a.path();
["cfg", "doc"].iter().any(|a| p.is_ident(a))
}

pub(crate) fn pin_data(
args: Args,
input: Item,
Expand Down Expand Up @@ -77,9 +82,9 @@ pub(crate) fn pin_data(
.fields
.iter_mut()
.map(|field| {
let len = field.attrs.len();
field.attrs.retain(|a| !a.path().is_ident("pin"));
(len != field.attrs.len(), &*field)
let pinned = field.attrs.iter().any(|a| a.path().is_ident("pin"));
field.attrs.retain(keep_attr);
(pinned, &*field)
})
.collect();

Expand Down Expand Up @@ -259,7 +264,7 @@ fn generate_projections(
},
)| {
let mut attrs = attrs.clone();
attrs.retain(|a| !a.path().is_ident("pin"));
attrs.retain(keep_attr);
let mut no_doc_attrs = attrs.clone();
no_doc_attrs.retain(|a| !a.path().is_ident("doc"));
let ident = ident
Expand Down Expand Up @@ -359,7 +364,7 @@ fn generate_the_pin_data(
pinned: bool,
) -> TokenStream {
let mut attrs = attrs.clone();
attrs.retain(|a| !a.path().is_ident("pin"));
attrs.retain(keep_attr);
let ident = ident
.as_ref()
.expect("only structs with named fields are supported");
Expand Down
12 changes: 12 additions & 0 deletions tests/dummy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "dummy"
version = "0.0.0"
edition = "2024"

license = "MIT OR Apache-2.0"
description = "Proc macro only for test reproduction."

publish = false

[lib]
proc-macro = true
4 changes: 4 additions & 0 deletions tests/dummy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[proc_macro_derive(Dummy, attributes(dummy_attr))]
pub fn derive_device(_: proc_macro::TokenStream) -> proc_macro::TokenStream {
proc_macro::TokenStream::new()
}
21 changes: 21 additions & 0 deletions tests/extra_attrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use dummy::Dummy;
use pin_init::*;

#[pin_data]
#[derive(Dummy)]
struct Pointless {
#[pin]
#[dummy_attr]
#[cfg(test)]
member: i8,
#[pin]
#[dummy_attr]
#[cfg(not(test))]
member: u8,
}

#[test]
fn multiple_attributes() {
stack_pin_init!(let p = init!(Pointless { member: 0 }));
println!("{}", p.member);
}
Loading