From nobody Thu Sep 11 12:51:26 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D837C04A6A for ; Thu, 3 Aug 2023 14:11:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235477AbjHCOLz (ORCPT ); Thu, 3 Aug 2023 10:11:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235240AbjHCOLi (ORCPT ); Thu, 3 Aug 2023 10:11:38 -0400 Received: from out0-221.mail.aliyun.com (out0-221.mail.aliyun.com [140.205.0.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A2EC49D6; Thu, 3 Aug 2023 07:10:41 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R151e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047204;MF=changxian.cqs@antgroup.com;NM=1;PH=DS;RN=12;SR=0;TI=SMTPD_---.U7iJE1X_1691071805; Received: from localhost(mailfrom:changxian.cqs@antgroup.com fp:SMTPD_---.U7iJE1X_1691071805) by smtp.aliyun-inc.com; Thu, 03 Aug 2023 22:10:06 +0800 From: "Qingsong Chen" To: linux-kernel@vger.kernel.org Cc: "=?UTF-8?B?55Sw5rSq5Lqu?=" , "Qingsong Chen" , "Miguel Ojeda" , "Alex Gaynor" , "Wedson Almeida Filho" , "Boqun Feng" , "Gary Guo" , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , "Benno Lossin" , "=?UTF-8?q?Sergio=20Gonz=C3=A1lez=20Collado?=" , Subject: [PATCH v2] rust: macros: vtable: fix `HAS_*` redefinition (`gen_const_name`) Date: Thu, 03 Aug 2023 22:09:23 +0800 Message-Id: <20230803140926.205974-1-changxian.cqs@antgroup.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If we define the same function name twice in a trait (using `#[cfg]`), the `vtable` macro will redefine its `gen_const_name`, e.g. this will define `HAS_BAR` twice: ```rust #[vtable] pub trait Foo { #[cfg(CONFIG_X)] fn bar(); #[cfg(not(CONFIG_X))] fn bar(x: usize); } ``` Changelog: Reviewed-by: Alice Ryhl Reviewed-by: Martin Rodriguez Reboredo ---------- v1 -> v2: - Use `BTreeSet` and existing `consts` as suggested by Alice and Gary. - Reword commit messages as suggested by Miguel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Fixes: b44becc5ee80 ("rust: macros: add `#[vtable]` proc macro") Signed-off-by: Qingsong Chen --- rust/macros/vtable.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rust/macros/vtable.rs b/rust/macros/vtable.rs index 34d5e7fb5768..8a1baedcc280 100644 --- a/rust/macros/vtable.rs +++ b/rust/macros/vtable.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 =20 use proc_macro::{Delimiter, Group, TokenStream, TokenTree}; -use std::collections::HashSet; +use std::collections::BTreeSet; use std::fmt::Write; =20 pub(crate) fn vtable(_attr: TokenStream, ts: TokenStream) -> TokenStream { @@ -28,7 +28,7 @@ pub(crate) fn vtable(_attr: TokenStream, ts: TokenStream)= -> TokenStream { =20 let mut body_it =3D body.stream().into_iter(); let mut functions =3D Vec::new(); - let mut consts =3D HashSet::new(); + let mut consts =3D BTreeSet::new(); while let Some(token) =3D body_it.next() { match token { TokenTree::Ident(ident) if ident.to_string() =3D=3D "fn" =3D> { @@ -74,6 +74,7 @@ pub(crate) fn vtable(_attr: TokenStream, ts: TokenStream)= -> TokenStream { const {gen_const_name}: bool =3D false;", ) .unwrap(); + consts.insert(gen_const_name); } } else { const_items =3D "const USE_VTABLE_ATTR: () =3D ();".to_owned(); --=20 2.40.1