Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5614718rdb; Wed, 13 Dec 2023 14:09:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IGrysdc78PtzOZ8uU1HkCjCbjl4SfZd1i9OKSdRgYbSY8h55w54N/IO1UiIVgJgMUO0rqtM X-Received: by 2002:a05:6358:3106:b0:16e:43a1:687f with SMTP id c6-20020a056358310600b0016e43a1687fmr10674315rwe.4.1702505341011; Wed, 13 Dec 2023 14:09:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702505340; cv=none; d=google.com; s=arc-20160816; b=wwxDVl/G2d//Orgh/EcPSPwmHgMiZjRJQLe+vC6uCwX2d2BKZfSQBVCaDaOhr3Oi33 ltG6eW38uY0UWXO6bKTJkgqEnC/4u0aoqt/9f1C4FQddBCK3r3gPDzgq85vcM3wWFpgH oqflwtqwC5aHQ+e2jHpRIgVTWsZVrG53bXLwUNp8jI6QENrsaq7AvxZYhPwvsm9AsWBM Sd9vFFPM4e8nzPq5fS28Gm825Z1VtDtEiGSi96uP8a0vJL0g9RVHPvefnzgD853ue0R7 eBaqwKMnwNDYFKqG4MXPxHXpAtkrGsyknuWRyLIX6Q1gCodV8AOGwBhfEm23ut+2+Hsl M/tA== 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 :feedback-id:message-id:subject:cc:from:to:date:dkim-signature; bh=Wynj0f8vy1dMFkSRpdFHLagtu+BF+QLWLK677HHPjYc=; fh=mqtUycvvt6dhLqs7LstFlpErPS7EEmEp1YKc77Rhw4k=; b=XQq65yG01jYPYGVlSdpWnHVutakDywbgOlHfiJpN6TXfvDSVRiINAP4vQmZmIscdKe 6kiQpicO/2tBIOu0QSpPcUOzwYkmkbWvQrueXEDQSovaviTMaNxVTMSbanSdG5sdCK8s VF0Cnqsqj5gvqiMfETqoVNl4iGDvW9jgCTLJ4hw9fnIi0w8obrf+o2TUb1uXDuRVIj88 qooCALSXoASVLOa6bFrGTTvIQCwV3xhUYa9Ce+PYGhnCUIbTKOFiAbfRgvgl8claROeV xbBaz+0obRv6TvmO1YGZihPAIxHieB8DEBQqTVKSl04g/TvoMeo43k3qgyQ3ShoOlLqs +1sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Qijbzkwg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id by26-20020a056a02059a00b005c65defc400si10589106pgb.624.2023.12.13.14.08.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 14:09:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Qijbzkwg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id ADCA88032235; Wed, 13 Dec 2023 14:08:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442740AbjLMWIi (ORCPT + 99 others); Wed, 13 Dec 2023 17:08:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229929AbjLMWIh (ORCPT ); Wed, 13 Dec 2023 17:08:37 -0500 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92829AC for ; Wed, 13 Dec 2023 14:08:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1702505319; x=1702764519; bh=Wynj0f8vy1dMFkSRpdFHLagtu+BF+QLWLK677HHPjYc=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=Qijbzkwgws8v2BOJU/Y04JKzswsD8go/e5ulLXTKs2nq2THXRHERcDlAoHedQwjuG eOos7HR7iuVpc7t4XoU/0JZxEbIu1KOpv1TeoICu/fBXDr17nhlnElyvkuPVQ4pZHd VeBNtDDBWl4ZNZ1w1DICo2crNDFHrofSmqP+luDoHkjfgW8H8nI+3zUaLDuWaT55pP dOrsQFpi+pHcc3w5oRabMyZwNOBkYNG7eOV6+1x3mWthM9Mp3PqlceAVy1IKLd5CbN jYx/S41HAw91r/266P+EAd2n/sORBzKR51nyFOdvRxf7AwrEPcmySssaqewCzyrZRV kQT7Pi4NCpwDQ== Date: Wed, 13 Dec 2023 22:08:32 +0000 To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Sumera Priyadarsini , Vincenzo Palazzo , Asahi Lina , Martin Rodriguez Reboredo From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/3] rust: macros: add `decl_generics` to `parse_generics()` Message-ID: <20231213220447.3613500-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Wed, 13 Dec 2023 14:08:55 -0800 (PST) The generic parameters on a type definition can specify default values. Currently `parse_generics()` cannot handle this though. For example when parsing the following generics: The `impl_generics` will be set to `T: Clone, const N: usize =3D 0` and `ty_generics` will be set to `T, N`. Now using the `impl_generics` on an impl block: impl<$($impl_generics)*> Foo {} will result in invalid Rust code, because default values are only available on type definitions. Therefore add parsing support for generic parameter default values using a new kind of generics called `decl_generics` and change the old behavior of `impl_generics` to not contain the generic parameter default values. Now `Generics` has three fields: - `impl_generics`: the generics with bounds (e.g. `T: Clone, const N: usize`) - `decl_generics`: the generics with bounds and default values (e.g. `T: Clone, const N: usize =3D 0`) - `ty_generics`: contains the generics without bounds and without default values (e.g. `T, N`) `impl_generics` is designed to be used on `impl<$impl_generics>`, `decl_generics` for the type definition, so `struct Foo<$decl_generics>` and `ty_generics` whenever you use the type, so `Foo<$ty_generics>`. Here is an example that uses all three different types of generics: let (Generics { decl_generics, impl_generics, ty_generics }, rest) =3D = parse_generics(input); quote! { struct Foo<$($decl_generics)*> { // ... } impl<$impl_generics> Foo<$ty_generics> { fn foo() { // ... } } } The next commit contains a fix to the `#[pin_data]` macro making it compatible with generic parameter default values by relying on this new behavior. Signed-off-by: Benno Lossin --- v1 -> v2: - improve documentation and commit message explanation - add motivation to commit message rust/macros/helpers.rs | 122 ++++++++++++++++++++++++++++++---------- rust/macros/pin_data.rs | 1 + rust/macros/zeroable.rs | 1 + 3 files changed, 94 insertions(+), 30 deletions(-) diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs index afb0f2e3a36a..3f50a5c847c8 100644 --- a/rust/macros/helpers.rs +++ b/rust/macros/helpers.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 =20 -use proc_macro::{token_stream, Group, Punct, Spacing, TokenStream, TokenTr= ee}; +use proc_macro::{token_stream, Group, TokenStream, TokenTree}; =20 pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option= { if let Some(TokenTree::Ident(ident)) =3D it.next() { @@ -70,8 +70,40 @@ pub(crate) fn expect_end(it: &mut token_stream::IntoIter= ) { } } =20 +/// Parsed generics. +/// +/// See the field documentation for an explanation what each of the fields= represents. +/// +/// # Examples +/// +/// ```rust,ignore +/// # let input =3D todo!(); +/// let (Generics { decl_generics, impl_generics, ty_generics }, rest) =3D= parse_generics(input); +/// quote! { +/// struct Foo<$($decl_generics)*> { +/// // ... +/// } +/// +/// impl<$impl_generics> Foo<$ty_generics> { +/// fn foo() { +/// // ... +/// } +/// } +/// } +/// ``` pub(crate) struct Generics { + /// The generics with bounds and default values (e.g. `T: Clone, const= N: usize =3D 0`). + /// + /// Use this on type definitions e.g. `struct Foo<$decl_generics> ...`= (or `union`/`enum`). + pub(crate) decl_generics: Vec, + /// The generics with bounds (e.g. `T: Clone, const N: usize`). + /// + /// Use this on `impl` blocks e.g. `impl<$impl_generics> Trait for ...= `. pub(crate) impl_generics: Vec, + /// The generics without bounds and without default values (e.g. `T, N= `). + /// + /// Use this when you use the type that is declared with these generic= s e.g. + /// `Foo<$ty_generics>`. pub(crate) ty_generics: Vec, } =20 @@ -81,6 +113,8 @@ pub(crate) struct Generics { pub(crate) fn parse_generics(input: TokenStream) -> (Generics, Vec) { // `impl_generics`, the declared generics with their bounds. let mut impl_generics =3D vec![]; + // The generics with bounds and default values. + let mut decl_generics =3D vec![]; // Only the names of the generics, without any bounds. let mut ty_generics =3D vec![]; // Tokens not related to the generics e.g. the `where` token and defin= ition. @@ -90,10 +124,17 @@ pub(crate) fn parse_generics(input: TokenStream) -> (G= enerics, Vec) { let mut toks =3D input.into_iter(); // If we are at the beginning of a generic parameter. let mut at_start =3D true; - for tt in &mut toks { + let mut skip_until_comma =3D false; + while let Some(tt) =3D toks.next() { + if nesting =3D=3D 1 && matches!(&tt, TokenTree::Punct(p) if p.as_c= har() =3D=3D '>') { + // Found the end of the generics. + break; + } else if nesting >=3D 1 { + decl_generics.push(tt.clone()); + } match tt.clone() { TokenTree::Punct(p) if p.as_char() =3D=3D '<' =3D> { - if nesting >=3D 1 { + if nesting >=3D 1 && !skip_until_comma { // This is inside of the generics and part of some bou= nd. impl_generics.push(tt); } @@ -105,49 +146,70 @@ pub(crate) fn parse_generics(input: TokenStream) -> (= Generics, Vec) { break; } else { nesting -=3D 1; - if nesting >=3D 1 { + if nesting >=3D 1 && !skip_until_comma { // We are still inside of the generics and part of= some bound. impl_generics.push(tt); } - if nesting =3D=3D 0 { - break; - } } } - tt =3D> { + TokenTree::Punct(p) if skip_until_comma && p.as_char() =3D=3D = ',' =3D> { if nesting =3D=3D 1 { - // Here depending on the token, it might be a generic = variable name. - match &tt { - // Ignore const. - TokenTree::Ident(i) if i.to_string() =3D=3D "const= " =3D> {} - TokenTree::Ident(_) if at_start =3D> { - ty_generics.push(tt.clone()); - // We also already push the `,` token, this ma= kes it easier to append - // generics. - ty_generics.push(TokenTree::Punct(Punct::new('= ,', Spacing::Alone))); - at_start =3D false; - } - TokenTree::Punct(p) if p.as_char() =3D=3D ',' =3D>= at_start =3D true, - // Lifetimes begin with `'`. - TokenTree::Punct(p) if p.as_char() =3D=3D '\'' && = at_start =3D> { - ty_generics.push(tt.clone()); - } - _ =3D> {} - } + impl_generics.push(TokenTree::Punct(p.clone())); + ty_generics.push(TokenTree::Punct(p)); + skip_until_comma =3D false; } - if nesting >=3D 1 { - impl_generics.push(tt); - } else if nesting =3D=3D 0 { + } + tt if !skip_until_comma =3D> { + match nesting { // If we haven't entered the generics yet, we still wa= nt to keep these tokens. - rest.push(tt); + 0 =3D> rest.push(tt), + 1 =3D> { + // Here depending on the token, it might be a gene= ric variable name. + match tt { + TokenTree::Ident(i) if at_start && i.to_string= () =3D=3D "const" =3D> { + let Some(name) =3D toks.next() else { + // Parsing error. + break; + }; + impl_generics.push(TokenTree::Ident(i)); + impl_generics.push(name.clone()); + ty_generics.push(name.clone()); + decl_generics.push(name); + at_start =3D false; + } + tt @ TokenTree::Ident(_) if at_start =3D> { + impl_generics.push(tt.clone()); + ty_generics.push(tt); + at_start =3D false; + } + TokenTree::Punct(p) if p.as_char() =3D=3D ',' = =3D> { + impl_generics.push(TokenTree::Punct(p.clon= e())); + ty_generics.push(TokenTree::Punct(p)); + at_start =3D true; + } + // Lifetimes begin with `'`. + TokenTree::Punct(p) if p.as_char() =3D=3D '\''= && at_start =3D> { + ty_generics.push(TokenTree::Punct(p.clone(= ))); + impl_generics.push(TokenTree::Punct(p)); + } + // Generics can have default values, we skip t= hese. + TokenTree::Punct(p) if p.as_char() =3D=3D '=3D= ' =3D> { + skip_until_comma =3D true; + } + tt =3D> impl_generics.push(tt), + } + } + _ =3D> impl_generics.push(tt), } } + _ =3D> {} } } rest.extend(toks); ( Generics { impl_generics, + decl_generics, ty_generics, }, rest, diff --git a/rust/macros/pin_data.rs b/rust/macros/pin_data.rs index 6d58cfda9872..022e68e9720d 100644 --- a/rust/macros/pin_data.rs +++ b/rust/macros/pin_data.rs @@ -10,6 +10,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStr= eam) -> TokenStream { let ( Generics { impl_generics, + decl_generics: _, ty_generics, }, rest, diff --git a/rust/macros/zeroable.rs b/rust/macros/zeroable.rs index 0d605c46ab3b..cfee2cec18d5 100644 --- a/rust/macros/zeroable.rs +++ b/rust/macros/zeroable.rs @@ -7,6 +7,7 @@ pub(crate) fn derive(input: TokenStream) -> TokenStream { let ( Generics { impl_generics, + decl_generics: _, ty_generics, }, mut rest, base-commit: d9857c16cfc6bce7764e1b79956c6a028f97f4d0 --=20 2.42.0