From 5ec807292c55eff56387f32d5b2a0a81f2926b81 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Tue, 11 Aug 2026 14:55:55 +0100 Subject: [PATCH] rust: macros: vtable: hide generated `HAS_` constants for required methods These constants have to be generated as the implementation-site `#[vtable]` invocation has no way of knowing whether the methods are required or provided-but-overridden. However, these constants will have a fixed value so shouldn't be used. Thus, hide them. Signed-off-by: Gary Guo Link: https://patch.msgid.link/20260811-vtable_macro-v1-1-50c5aab3c211@garyguo.net [ Reworded for typo. - Miguel ] Signed-off-by: Miguel Ojeda --- rust/macros/vtable.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/rust/macros/vtable.rs b/rust/macros/vtable.rs index be9a5ed8abe5..1dfc40eb53f8 100644 --- a/rust/macros/vtable.rs +++ b/rust/macros/vtable.rs @@ -9,7 +9,10 @@ Ident, TokenStream, // }; -use quote::ToTokens; +use quote::{ + quote, + ToTokens, // +}; use syn::{ parse_quote, Error, @@ -56,12 +59,18 @@ fn handle_trait(mut item: ItemTrait) -> Result { // 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. + // However, hide it for required methods as it will always be true. + let doc = if fn_item.default.is_some() { + let comment = + format!("Indicates if the `{name}` method is overridden by the implementor."); + quote!(#[doc = #comment]) + } else { + quote!(#[doc(hidden)]) + }; 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] + #doc const #gen_const_name: bool = false; }); }