Message ID | 20231008094816.320424-1-tmgross@umich.edu |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp1294103vqo; Sun, 8 Oct 2023 02:56:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEft70/WT9s35/InLfaSXe6h3iw6/2q/JHHCFjCXUVWvt3Bd92abvO6RDNQviQoz7dgx7rr X-Received: by 2002:a17:90a:b015:b0:26b:c5b:bb44 with SMTP id x21-20020a17090ab01500b0026b0c5bbb44mr11794249pjq.13.1696758992317; Sun, 08 Oct 2023 02:56:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696758992; cv=none; d=google.com; s=arc-20160816; b=EV7PJnCkYYa9gLy0Oyub7ZzEZwiTJ63t4WL3yRxQVTvdmphEBF7I3MFOGq+ngQKT9q mnaaLvtux88ZLapwqUK7XYybl9O6KWLN4nYf5I4uKj/K+eSPZU0ZVE9NKKs+kNr2Y7Xs y0ik3kLkT1NrSRuXvOKEdcndR3bKEqAHK7xiZuRr2mo2V8/edTebEAxsfawjHiGwuo40 t4Dnb5LoZtj7A672r4jJvfq6qFt+fMse1NMv5tQlrr9HmzZoTCuMMAerVOLuZbdLySFF +nRVnkePSZaVOo8uXmd8gkt5pEg/t3zFmrO1Ba4EmYkHQzfKJBnCM8T4qxMrplvh/62o CvTg== 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=I+o9SIX4PotCqWF/xeatcNnnqUwJ9aNzchabhWdrqjA=; fh=paHpB5QxaFjz5LzUU5O6Ouzrt1NoZWFpFO44CbZeugo=; b=CG5kTvDOn59/nsXrG3seeZ0YIi7C4dps/tAk9cofPok0FcvuLOU8zPbVAxtUzvHt0o 6UarASY47TYxq0tRh/Lxhqf2iKuZAyKU6fw12m3C4KgLZ9Zwoamnprnq9kZXR9QY8RsG UmuY0YQCQiyFppHw2gaRc9gEy8D0Qg0rfKfvyq+lM2ymWnkYz67JdHnMT8XVvjgYNmn/ nmGLtOY9bbMRmhR8lNnr4FenndFgR77/tRjh9/XDEw6TiTsGsrYsNgogoADJhuiz+y/L ZpAPUBbOO5KW/H8/cp6ISdz8Ziaal5Hk7tJvPBMsd10jIeDafKHIEhe+yF6/mx7dD1HZ zbeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@umich.edu header.s=relay-2018-08-29 header.b=q5dVvz0U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id m1-20020a17090aab0100b00277507e8085si9004333pjq.55.2023.10.08.02.56.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 02:56:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@umich.edu header.s=relay-2018-08-29 header.b=q5dVvz0U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 2C0C5803DAD5; Sun, 8 Oct 2023 02:56:30 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344605AbjJHJ4H (ORCPT <rfc822;pusanteemu@gmail.com> + 17 others); Sun, 8 Oct 2023 05:56:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344601AbjJHJ4G (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 8 Oct 2023 05:56:06 -0400 X-Greylist: delayed 283 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Sun, 08 Oct 2023 02:56:04 PDT Received: from short-nisien.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 4D220EB; Sun, 8 Oct 2023 02:56:04 -0700 (PDT) Received: from exalted-banshee.authn-relay.a.mail.umich.edu (ip-10-0-74-179.us-east-2.compute.internal [10.0.74.179]) by short-nisien.relay-egress.a.mail.umich.edu with ESMTPS id 65227B97.15EC55FA.7AE301A9.81919; Sun, 08 Oct 2023 05:51:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=umich.edu; s=relay-2018-08-29; t=1696758678; bh=I+o9SIX4PotCqWF/xeatcNnnqUwJ9aNzchabhWdrqjA=; h=From:To:Cc:Subject:Date; b=q5dVvz0UBP3d3l2RanJF9SMVehhIpvDqS+ES4mXq2bIa7WowwiVEGUP4oHkSj9r7h gOvtKBiDiv2LsAsHuT+UVcucyxzL6uiTAdICEwCVuGMDxQLl25uN/Yzr1USAJ1FxPG iSoIoW9N+LYweqBTUGJev5/5wn+sfABzdExCXhUtIa7LAtow7W4B0/V+wPaYicAGmI 1md/jPLut7YAdXfT/iUGcHL7/YZJ/cVD+ZqpC4JF09PLwrqI3nOYo8gVK+UlGEdzXb q3qh0AI/Zb/Mws7VxOAm6XXVYj9YHA9SekhoEDIvXxxhWElVju16pzMhUQRB87v0tY ykDi2Dy3GS8rg== Authentication-Results: exalted-banshee.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 exalted-banshee.authn-relay.a.mail.umich.edu with ESMTPSA id 65227B96.46860EC.3A6166B1.881404; Sun, 08 Oct 2023 05:51:18 -0400 From: Trevor Gross <tmgross@umich.edu> To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Wedson Almeida Filho <wedsonaf@gmail.com> Cc: Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, Benno Lossin <benno.lossin@proton.me>, Andreas Hindborg <a.hindborg@samsung.com>, Alice Ryhl <aliceryhl@google.com>, Martin Rodriguez Reboredo <yakoyoku@gmail.com>, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, FUJITA Tomonori <fujita.tomonori@gmail.com>, Trevor Gross <tmgross@umich.edu> Subject: [PATCH] rust: macros: update 'paste!' macro to accept string literals Date: Sun, 8 Oct 2023 05:48:18 -0400 Message-Id: <20231008094816.320424-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.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Sun, 08 Oct 2023 02:56:30 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779180757116795529 X-GMAIL-MSGID: 1779180757116795529 |
Series |
rust: macros: update 'paste!' macro to accept string literals
|
|
Commit Message
Trevor Gross
Oct. 8, 2023, 9:48 a.m. UTC
Enable combining identifiers with string 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_;
Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Signed-off-by: Trevor Gross <tmgross@umich.edu>
---
Original mention of this problem in [1]
[1]: https://lore.kernel.org/rust-for-linux/20231008.164906.1151622782836568538.fujita.tomonori@gmail.com/
rust/macros/paste.rs | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
Comments
On 10/8/23 06:48, Trevor Gross wrote: > Enable combining identifiers with string 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_; > > Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com> > Signed-off-by: Trevor Gross <tmgross@umich.edu> > --- > > Original mention of this problem in [1] > > [1]: https://lore.kernel.org/rust-for-linux/20231008.164906.1151622782836568538.fujita.tomonori@gmail.com/ Next time I think you should put this in `Fixes:`. > [...] Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
On Sun, Oct 8, 2023 at 8:27 AM Martin Rodriguez Reboredo <yakoyoku@gmail.com> wrote: > > Next time I think you should put this in `Fixes:`. > > > [...] > Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com> > Good point, thanks! I'll add that if there is a v2 (or Miguel can probably add it if not)
> 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_; > > Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com> > Signed-off-by: Trevor Gross <tmgross@umich.edu> > --- Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
On Sun, Oct 8, 2023 at 11:51 AM Trevor Gross <tmgross@umich.edu> wrote: > Enable combining identifiers with string 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_; > > Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com> > Signed-off-by: Trevor Gross <tmgross@umich.edu> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
On Mon, Oct 9, 2023 at 5:04 AM Trevor Gross <tmgross@umich.edu> wrote: > > Good point, thanks! I'll add that if there is a v2 (or Miguel can > probably add it if not) Yes, I add them myself when I notice they are missing (e.g. most recently 2 of the ones in `rust-fixes`), but patches should definitely come with the `Fixes: ` tag themselves, i.e. it should be the exceptional case. However, is this actually a fix? The title and commit message make it sound like it is a new feature rather than a fix. And the docs of the macro says literals are not supported, right? So this probably needs to update those docs too (and ideally add an example with the newly supported construct too). Or am I misunderstanding? Cheers, Miguel
On 08.10.23 11:48, Trevor Gross wrote: > Enable combining identifiers with string 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_; > > Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com> > Signed-off-by: Trevor Gross <tmgross@umich.edu> Reviewed-by: Benno Lossin <benno.lossin@proton.me>
On Mon, Oct 9, 2023 at 6:49 AM Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote: > > On Mon, Oct 9, 2023 at 5:04 AM Trevor Gross <tmgross@umich.edu> wrote: > > > > Good point, thanks! I'll add that if there is a v2 (or Miguel can > > probably add it if not) > > Yes, I add them myself when I notice they are missing (e.g. most > recently 2 of the ones in `rust-fixes`), but patches should definitely > come with the `Fixes: ` tag themselves, i.e. it should be the > exceptional case. > > However, is this actually a fix? The title and commit message make it > sound like it is a new feature rather than a fix. And the docs of the > macro says literals are not supported, right? I suppose it is something that augments current behavior and "fixes" the linked use case by making it possible. I am not sure what qualifies as a fix. > So this probably needs to update those docs too (and ideally add an > example with the newly supported construct too). Or am I > misunderstanding? > > Cheers, > Miguel I will update the documentation, thanks for the catch.
On Mon, Oct 9, 2023 at 9:14 PM Trevor Gross <tmgross@umich.edu> wrote: > > I suppose it is something that augments current behavior and "fixes" > the linked use case by making it possible. I am not sure what > qualifies as a fix. `Fixes` is meant for issues/bugs. So if the macro was broken, i.e. it does not do what it says it would, it would be a fix. But if I understand correctly, the docs say this was not supported, so it is not a fix, it is just expanding the feature set. Similarly, `Reported-by` is not meant for feature requests. > I will update the documentation, thanks for the catch. Thanks! Cheers, Miguel
On Sun, 8 Oct 2023 05:48:18 -0400 Trevor Gross <tmgross@umich.edu> wrote: > Enable combining identifiers with string 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_; > > Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com> > Signed-off-by: Trevor Gross <tmgross@umich.edu> Reviewed-by: Gary Guo <gary@garyguo.net> > --- > > Original mention of this problem in [1] > > [1]: https://lore.kernel.org/rust-for-linux/20231008.164906.1151622782836568538.fujita.tomonori@gmail.com/ > > rust/macros/paste.rs | 10 +++++++++- > 1 file changed, 9 insertions(+), 1 deletion(-) > > 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#") {
On Sun, Oct 08, 2023 at 05:48:18AM -0400, Trevor Gross wrote: > Enable combining identifiers with string 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_; > > Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com> > Signed-off-by: Trevor Gross <tmgross@umich.edu> This looks good to me, but could you (in a follow-up patch mabye) add an example demonstrating the usage, which could also serve as a test if we can run doctest for macro doc. Thanks! Regards, Boqun > --- > > Original mention of this problem in [1] > > [1]: https://lore.kernel.org/rust-for-linux/20231008.164906.1151622782836568538.fujita.tomonori@gmail.com/ > > rust/macros/paste.rs | 10 +++++++++- > 1 file changed, 9 insertions(+), 1 deletion(-) > > 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 > >
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#") {