Received: by 2002:a05:7412:b130:b0:e2:908c:2ebd with SMTP id az48csp922469rdb; Fri, 17 Nov 2023 17:46:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IGgoJUpobL8mvuuK4By7iGVPNSGNlUwwCJFMJh+pwOMH7hJ9cfCNzNk5T49zrlqZCh/yhs7 X-Received: by 2002:a05:6a00:24c9:b0:6c3:871a:7f0f with SMTP id d9-20020a056a0024c900b006c3871a7f0fmr1525888pfv.5.1700271992203; Fri, 17 Nov 2023 17:46:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700271992; cv=none; d=google.com; s=arc-20160816; b=OfRXyQSK9c2XpvBwJ+CF+/+o1ogl9jsDDtmz/TTnKXroJ91fUx16Z82+kLqK0yiipq ncApfWHZBjOGgLdySOOah7iKkgi3A10i67lz9Uq0jdgGwFGIkk8GmiaTAD9W3Xy4Wyyp nBeaRKMPXRNHlOxuadHXNW7ytXHvYBwmzKBllYMsj4/9yexlaDedEjIcUt/OYtB2Ifr5 FAD8Sl1eQnVlaKNeWDhkhIx3Ia3ngFHN2kgPnJuAycGkh+oTOPCY0NVzAXr16YrAyc6W dx4qNd/FjDJTCNPablDx5yiEBcf+s+ghTCRFIy+jL5hOLvFrb/yXrkpEx2T7tiCAcL/F 7XZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=Vh/HaZa7HJvFyuyNB0eCPpU6WqUM0OIvSvSE9/ugphQ=; fh=Sox8iXses78cto72M0zHcewh/pAtcMu7AWErPkkrnD0=; b=LHXIlTAgVwZP28/nfEoYG0xhhIDEvRT5GV2Q2sL6AmDWz1dCe2qnlkRQNbOAtVBOtG CwF5WBBE1bySekIpECo5hO1JAjKm12YLiU2bIhXxRjZFJ3R18ZLr4HRaNAAgEthjLcxo M1iOnxizfpjB2M7EaFlsqreaeOWNU/qrBw93U9D0EpuLU9DwcLfQT1AHBlL/KzlZGth+ LPUZljpZlxybQ3h72APwRZ22UP3/zOcgeF2/hUJMJavNugXQ6oWM8bE+uw0gjOZVbV93 Xvgi7Djwf/DAX8uyRJeenWwlKcfWpdAhtp0uX0fzIgbGb2uE+Ta+fwS8W2pDiMm5oEE5 /5aA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@umich.edu header.s=relay-2018-08-29 header.b=ljbribNl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=umich.edu Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id y12-20020aa7942c000000b006c507a60e2dsi3105257pfo.177.2023.11.17.17.46.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Nov 2023 17:46:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@umich.edu header.s=relay-2018-08-29 header.b=ljbribNl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=umich.edu Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 7A695804E7A3; Fri, 17 Nov 2023 17:45:14 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232673AbjKRBpH (ORCPT + 99 others); Fri, 17 Nov 2023 20:45:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229737AbjKRBpG (ORCPT ); Fri, 17 Nov 2023 20:45:06 -0500 X-Greylist: delayed 239 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Fri, 17 Nov 2023 17:45:02 PST Received: from lively-olwen.relay-egress.a.mail.umich.edu (relay-egress-host.us-east-2.a.mail.umich.edu [18.216.144.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09BFDD79; Fri, 17 Nov 2023 17:45:01 -0800 (PST) Received: from mammoth-yacaruna.authn-relay.a.mail.umich.edu (ip-10-0-73-60.us-east-2.compute.internal [10.0.73.60]) by lively-olwen.relay-egress.a.mail.umich.edu with ESMTPS id 6558162C.23A596CC.5D730AC5.3305047; Fri, 17 Nov 2023 20:41:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=umich.edu; s=relay-2018-08-29; t=1700271660; bh=Vh/HaZa7HJvFyuyNB0eCPpU6WqUM0OIvSvSE9/ugphQ=; h=From:To:Cc:Subject:Date; b=ljbribNlOeIV8shwVqGZXICcLSBQPNxKk4dzNUHAmgQVoZcblQZ1OJNT9Mq9FgoF2 odq54YYb1D/lc2Q2Ww+6GYIx9UkCmxT5PRSj7oC+7BBkz2+pjiE6RHh+1C12SUg3FT dR+p67yKXBpUve03zv847pldxjYbcZTOn5yGm6uhsR9dtUKLi72C2AXD3tdn3Dh3o3 8U/NtRlijVneAAXcRhfiITXyGVddmeaGc8t61OVFXNLD4mQpKeBdQk2jiSoMRIoOwZ UL8+mS99Sl1jvImA1v4Yqywo9+m3MoQ2mzcJvGJGyI3YMoTv0wIKxexEM7+eAJVRfx bJXZbkG7YgoPg== Authentication-Results: mammoth-yacaruna.authn-relay.a.mail.umich.edu; iprev=pass policy.iprev=76.141.129.158 (c-76-141-129-158.hsd1.il.comcast.net); auth=pass smtp.auth=tmgross Received: from localhost.localdomain (c-76-141-129-158.hsd1.il.comcast.net [76.141.129.158]) by mammoth-yacaruna.authn-relay.a.mail.umich.edu with ESMTPSA id 6558162B.22539A90.1E7385F2.2183879; Fri, 17 Nov 2023 20:40:59 -0500 From: Trevor Gross To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Martin Rodriguez Reboredo , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, FUJITA Tomonori , Vincenzo Palazzo , Trevor Gross Subject: [PATCH v2] rust: macros: update 'paste!' macro to accept string literals Date: Fri, 17 Nov 2023 20:39:59 -0500 Message-Id: <20231118013959.37384-1-tmgross@umich.edu> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 17 Nov 2023 17:45:14 -0800 (PST) Enable combining identifiers with literals in the 'paste!' macro. This allows combining user-specified strings with affixes to create namespaced identifiers. This sample code: macro_rules! m { ($name:lit) => { paste!(struct [<_some_ $name _struct_>] {}) } } m!("foo_bar"); Would previously cause a compilation error. It will now generate: struct _some_foo_bar_struct_ {} Signed-off-by: Trevor Gross Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Vincenzo Palazzo Reviewed-by: Alice Ryhl Reviewed-by: Benno Lossin Reviewed-by: Gary Guo --- Changes since v1: correct documentation and add an example. I added the reviewed-bys since the only change is in documentation, not functionality. v1: https://lore.kernel.org/rust-for-linux/20231008094816.320424-1-tmgross@umich.edu/ rust/macros/lib.rs | 22 ++++++++++++++++++++-- rust/macros/paste.rs | 10 +++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index c42105c2ff96..51d1f8836667 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -254,8 +254,8 @@ pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream { /// Within the `paste!` macro, identifiers inside `[<` and `>]` are concatenated together to form a /// single identifier. /// -/// This is similar to the [`paste`] crate, but with pasting feature limited to identifiers -/// (literals, lifetimes and documentation strings are not supported). There is a difference in +/// This is similar to the [`paste`] crate, but with pasting feature limited to identifiers and +/// literals (lifetimes and documentation strings are not supported). There is a difference in /// supported modifiers as well. /// /// # Example @@ -337,6 +337,24 @@ pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream { /// assert_eq!(br_ok(), binder_driver_return_protocol_BR_OK); /// ``` /// +/// # Literals +/// +/// Literals can also be concatenated with other identifiers +/// +/// ```ignore +/// macro_rules! create_numbered_fn { +/// ($name:literal, $val:literal) => { +/// kernel::macros::paste! { +/// fn []() -> u32 { $val } +/// } +/// }; +/// } +/// +/// create_numbered_fn!("foo", 100); +/// +/// assert_eq!(some_foo_fn100(), 100) +/// ``` +/// /// [`paste`]: https://docs.rs/paste/ #[proc_macro] pub fn paste(input: TokenStream) -> TokenStream { diff --git a/rust/macros/paste.rs b/rust/macros/paste.rs index 385a78434224..f40d42b35b58 100644 --- a/rust/macros/paste.rs +++ b/rust/macros/paste.rs @@ -9,7 +9,15 @@ fn concat(tokens: &[TokenTree], group_span: Span) -> TokenTree { loop { match tokens.next() { None => break, - Some(TokenTree::Literal(lit)) => segments.push((lit.to_string(), lit.span())), + Some(TokenTree::Literal(lit)) => { + // Allow us to concat string literals by stripping quotes + let mut value = lit.to_string(); + if value.starts_with('"') && value.ends_with('"') { + value.remove(0); + value.pop(); + } + segments.push((value, lit.span())); + } Some(TokenTree::Ident(ident)) => { let mut value = ident.to_string(); if value.starts_with("r#") { -- 2.34.1