From: Gary Guo <gary@kernel.org>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Tamir Duberstein" <tamird@gmail.com>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] rust: macros: support `#[cfg]` properly in `#[vtable]` macro.
Date: Tue, 13 Jan 2026 13:11:25 +0000 [thread overview]
Message-ID: <20260113131138.2155899-1-gary@kernel.org> (raw)
In-Reply-To: <CAJ-ks9=UwOz=-NR0MJ3pjKZNGjb7+3R-shFqYKQF206s5Tp6ew@mail.gmail.com>
From: Gary Guo <gary@garyguo.net>
Currently, we generate `HAS_` constants as long as the definition exists in
the source, regardless if it is cfg-ed out or not.
Currently, uses of `#[cfg]` present in both trait and impl, so it is not a
problem; however if only the impl side uses `#[cfg]` then `HAS_` constants
will incorrectly be true while it shouldnt't.
With `syn` support, we can now implement `#[cfg]` handling properly by
propagating the `#[cfg]` attributes to the constants.
Signed-off-by: Gary Guo <gary@garyguo.net>
---
rust/macros/helpers.rs | 6 ++++++
rust/macros/vtable.rs | 13 ++++---------
2 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs
index adfa60d8f42d8..11117b4c95d25 100644
--- a/rust/macros/helpers.rs
+++ b/rust/macros/helpers.rs
@@ -7,6 +7,7 @@
Parse,
ParseStream, //
},
+ Attribute,
Error,
LitStr,
Result, //
@@ -53,3 +54,8 @@ pub(crate) fn file() -> String {
proc_macro::Span::call_site().file()
}
}
+
+/// Obtain all `#[cfg]` attributes.
+pub(crate) fn gather_cfg_attrs(attr: &[Attribute]) -> impl Iterator<Item = Attribute> + '_ {
+ attr.iter().filter(|a| a.path().is_ident("cfg")).cloned()
+}
diff --git a/rust/macros/vtable.rs b/rust/macros/vtable.rs
index 72ae0a1816a04..c6510b0c4ea1d 100644
--- a/rust/macros/vtable.rs
+++ b/rust/macros/vtable.rs
@@ -23,7 +23,6 @@
fn handle_trait(mut item: ItemTrait) -> Result<ItemTrait> {
let mut gen_items = Vec::new();
- let mut gen_consts = HashSet::new();
gen_items.push(parse_quote! {
/// A marker to prevent implementors from forgetting to use [`#[vtable]`](vtable)
@@ -38,22 +37,17 @@ fn handle_trait(mut item: ItemTrait) -> Result<ItemTrait> {
&format!("HAS_{}", name.to_string().to_uppercase()),
name.span(),
);
- // Skip if it's declared already -- this can happen if `#[cfg]` is used to selectively
- // define functions.
- // FIXME: `#[cfg]` should be copied and propagated to the generated consts.
- if gen_consts.contains(&gen_const_name) {
- continue;
- }
// We don't know on the implementation-site whether a method is required or provided
// so we have to generate a const for all methods.
+ let cfg_attrs = crate::helpers::gather_cfg_attrs(&fn_item.attrs);
let comment =
format!("Indicates if the `{name}` method is overridden by the implementor.");
gen_items.push(parse_quote! {
+ #(#cfg_attrs)*
#[doc = #comment]
const #gen_const_name: bool = false;
});
- gen_consts.insert(gen_const_name);
}
}
@@ -87,10 +81,11 @@ fn handle_impl(mut item: ItemImpl) -> Result<ItemImpl> {
if defined_consts.contains(&gen_const_name) {
continue;
}
+ let cfg_attrs = crate::helpers::gather_cfg_attrs(&fn_item.attrs);
gen_items.push(parse_quote! {
+ #(#cfg_attrs)*
const #gen_const_name: bool = true;
});
- defined_consts.insert(gen_const_name);
}
}
base-commit: 761e1fbc67b122bfe3d58518a9ea9ee668962c92
--
2.51.2
next prev parent reply other threads:[~2026-01-13 13:11 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-12 17:07 [PATCH v3 00/12] refactor Rust proc macros with `syn` Gary Guo
2026-01-12 17:07 ` [PATCH v3 01/12] rust: pin-init: internal: remove proc-macro[2] and quote workarounds Gary Guo
2026-01-12 17:07 ` [PATCH v3 02/12] rust: macros: use `quote!` from vendored crate Gary Guo
2026-01-19 7:09 ` David Gow
2026-01-12 17:07 ` [PATCH v3 03/12] rust: macros: convert `#[vtable]` macro to use `syn` Gary Guo
2026-01-17 19:25 ` Benno Lossin
2026-01-12 17:07 ` [PATCH v3 04/12] rust: macros: use `syn` to parse `module!` macro Gary Guo
2026-01-17 20:12 ` Benno Lossin
2026-01-12 17:07 ` [PATCH v3 05/12] rust: macros: use `quote!` for " Gary Guo
2026-01-12 17:07 ` [PATCH v3 06/12] rust: macros: convert `#[export]` to use `syn` Gary Guo
2026-01-12 17:07 ` [PATCH v3 07/12] rust: macros: convert `concat_idents!` " Gary Guo
2026-01-12 17:07 ` [PATCH v3 08/12] rust: macros: convert `#[kunit_tests]` macro " Gary Guo
2026-01-17 20:09 ` Benno Lossin
2026-01-19 7:10 ` David Gow
2026-01-12 17:07 ` [PATCH v3 09/12] rust: macros: allow arbitrary types to be used in `module!` macro Gary Guo
2026-01-12 17:07 ` [PATCH v3 10/12] rust: macros: rearrange `#[doc(hidden)]` " Gary Guo
2026-01-12 17:07 ` [PATCH v3 11/12] rust: kunit: use `pin_init::zeroed` instead of custom null value Gary Guo
2026-01-19 7:10 ` David Gow
2026-01-12 17:07 ` [PATCH v3 12/12] rust: macros: support `#[cfg]` properly in `#[vtable]` macro Gary Guo
2026-01-12 17:48 ` Tamir Duberstein
2026-01-13 13:11 ` Gary Guo [this message]
2026-01-13 13:39 ` [PATCH] " Tamir Duberstein
2026-01-13 14:09 ` Gary Guo
2026-01-13 14:23 ` Tamir Duberstein
2026-01-13 17:05 ` [PATCH v2] " Gary Guo
2026-01-17 20:09 ` Benno Lossin
2026-01-28 15:09 ` [PATCH v3 00/12] refactor Rust proc macros with `syn` Miguel Ojeda
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260113131138.2155899-1-gary@kernel.org \
--to=gary@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@gmail.com \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.