mirror of
https://github.com/torvalds/linux.git
synced 2026-11-07 06:17:02 +01:00
rust: pin-init: internal: add utility for extract a single attribute
More attributes are to be added which has the requirement that they should only be specified once. Add a utility code to extract attributes for a specific path and report error when appeared multiple times. Convert `#[pin]` parsing to use the added utility. Acked-by: Benno Lossin <lossin@kernel.org> Link: https://patch.msgid.link/20261006-dev-selfref-dep-v1-4-da716f740d6d@garyguo.net Signed-off-by: Gary Guo <gary@garyguo.net>
This commit is contained in:
parent
25e82b177c
commit
003a8953d2
|
|
@ -137,12 +137,7 @@ pub(crate) fn pin_data(
|
|||
.iter_mut()
|
||||
.enumerate()
|
||||
.map(|(index, field)| {
|
||||
let len = field.attrs.len();
|
||||
field.attrs.retain(|a| !a.path().is_ident("pin"));
|
||||
let pinned_count = len - field.attrs.len();
|
||||
if pinned_count > 1 {
|
||||
dcx.error(&field, "#[pin] attribute specified more than once");
|
||||
}
|
||||
let pinned = field.attrs.extract_single_attr(dcx, "pin").is_some();
|
||||
|
||||
assert!(
|
||||
!field.attrs.iter().any(|a| a.path().is_ident("cfg")),
|
||||
|
|
@ -159,7 +154,7 @@ pub(crate) fn pin_data(
|
|||
FieldInfo {
|
||||
field: &*field,
|
||||
member,
|
||||
pinned: pinned_count != 0,
|
||||
pinned,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
|
|
|||
|
|
@ -4,8 +4,15 @@
|
|||
use quote::{format_ident, ToTokens};
|
||||
use syn::{Attribute, GenericParam, Generics, Index, Member, Token};
|
||||
|
||||
use crate::DiagCtxt;
|
||||
|
||||
pub(crate) trait AttrListExt {
|
||||
fn extract_cfg_attrs(&mut self) -> Vec<TokenStream>;
|
||||
|
||||
/// Extract attribute with identifier `path`.
|
||||
///
|
||||
/// Report error if the attribute appears multiple times.
|
||||
fn extract_single_attr(&mut self, dcx: &DiagCtxt, path: &str) -> Option<Attribute>;
|
||||
}
|
||||
|
||||
impl AttrListExt for Vec<Attribute> {
|
||||
|
|
@ -25,6 +32,24 @@ fn extract_cfg_attrs(&mut self) -> Vec<TokenStream> {
|
|||
|
||||
cfg
|
||||
}
|
||||
|
||||
fn extract_single_attr(&mut self, dcx: &DiagCtxt, path: &str) -> Option<Attribute> {
|
||||
// FIXME: Replace with `extract_if` when MSRV >= 1.85.
|
||||
let attr_pos = self.iter().position(|attr| attr.path().is_ident(path))?;
|
||||
let attr = self.remove(attr_pos);
|
||||
self.retain(|attr| {
|
||||
if !attr.path().is_ident(path) {
|
||||
return true;
|
||||
}
|
||||
|
||||
dcx.error(
|
||||
attr,
|
||||
format!("`#[{path}]` attribute specified more than once"),
|
||||
);
|
||||
false
|
||||
});
|
||||
Some(attr)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait MemberExt {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user