From patchwork Sun Oct 8 09:48:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Trevor Gross X-Patchwork-Id: 149722 Return-Path: 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 + 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 ); 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 To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Martin Rodriguez Reboredo , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, FUJITA Tomonori , Trevor Gross 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 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: 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 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 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 --- 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#") {