Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp13929197rwd; Sat, 24 Jun 2023 08:48:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7DuNpR92tfxRPnrxjiMIf2ERVVr4MIYNlcIpQaNjhPJuonyevMA9MZpf8HEcwDhWrFPuUC X-Received: by 2002:a05:6a00:190a:b0:66c:9faa:bb12 with SMTP id y10-20020a056a00190a00b0066c9faabb12mr1684065pfi.9.1687621702545; Sat, 24 Jun 2023 08:48:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687621702; cv=none; d=google.com; s=arc-20160816; b=hMaP5bbRwkbGE+5f+zsi58Wu2MZs2fgtAyejuNtxwjc5JIQxwkmW8Fi8Rcc+U0Hfxy GpWcb89Ibsh3Z/9wjQ/P2FXIwdSuzmq7KfK835Cx+rlAnc8q87ZmzfhPIaUwc1bLdMya OBG3wo3WFQVULHUTOnqodqhPfos8FC5Rxn95WzSvA8fMM47AUXYrYO+cQ3BXkfavWyGF I7vPeq09I/7Dc7CdqC4kvbmIkQQZtAOwIlwWQuh80uwQ6SIVBaIUSsOXa8PqBd86jJh9 zZjhptUrhihoSsTb+EnkcVQmhvxMcT9yb8977hNS/A0O4pCoRbWn3OeAYorPthY4VVjS AXYg== 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:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=UJJ+xg0BEjeX07rbDKXrDZQrXSRpl2S3/JvIkzR6HHY=; fh=G5JJZHPpWxnD6ZFNcd5f5J3pAM1l8A+sr+uFjGq+s0E=; b=XH8kCEmUNYBS+iDiHBWvB1YMCJdl9rnyj3rq+x6R6sMpAjS5+y3kP1DWz6CvMP78Cw riealXv+yw1k6eyN6lYtjmd77cHXoo98Lj4mfzR0bpDJtfwEq3iMzBxPnApbAaS0AuJr MToWQjCLSl6mBdH+3PFeJxWJ1KkiJ26O9g/0ZgjkpIIrGihndVCg3hbAFkyyeGLhk2mx LXyqgUGannLN98zp7j87fieKPDAYjtig79n7iAw+YS6s11+558FQ//6McpMNl4sl70WD LbAKxhSVjw6I2ileqHGbIc7isOr2rjOrYDdT6jwT/Z0oaZvlhnjjECzXdr5ZqCGe9KcO x3Xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b="bi2r/0LH"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cl7-20020a056a0032c700b0066896d1ffddsi1437806pfb.224.2023.06.24.08.48.10; Sat, 24 Jun 2023 08:48:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b="bi2r/0LH"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233129AbjFXPUt (ORCPT + 99 others); Sat, 24 Jun 2023 11:20:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233036AbjFXPUs (ORCPT ); Sat, 24 Jun 2023 11:20:48 -0400 Received: from mail-40141.protonmail.ch (mail-40141.protonmail.ch [185.70.40.141]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1D69BE for ; Sat, 24 Jun 2023 08:20:46 -0700 (PDT) Date: Sat, 24 Jun 2023 15:20:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1687620044; x=1687879244; bh=UJJ+xg0BEjeX07rbDKXrDZQrXSRpl2S3/JvIkzR6HHY=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=bi2r/0LH420keA+7CxhIvE6aswX5462BYjSlArWFC6ao3ArHSWAkDMQGx3P64tqJZ Qg1pA2NCfpKZtb8Fc4neqlw4YPe0B3n7SdMtVWKK7XYJSxOMtRfi0aoy3nvnYhYnws yFZY3bbkUTjVwifNubXh8TWcKhMCXVIM7xMMaq2ZaL6l2ibXr9npmF4RcSLGV0wJoR XUiYjJjGi9aJFneHgDP/ZF872U8b3VFUdzD2/P6/EwRIUKnJPjigqpudM/ldpiONmK hfJ2TVZKWF9Wxb9dNWiTcWlQPa6zzZk0LhQbScRSXcjYUbglj3+8iFnyjenxehmqjO nG67p7xfZdjCA== To: Benno Lossin From: =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= Cc: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Asahi Lina Subject: Re: [PATCH 7/7] rust: init: add support for arbitrary paths in init macros Message-ID: In-Reply-To: <20230624092330.157338-7-benno.lossin@proton.me> References: <20230624092330.157338-1-benno.lossin@proton.me> <20230624092330.157338-7-benno.lossin@proton.me> Feedback-ID: 27884398:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 On Saturday, June 24th, 2023 at 11:25, Benno Lossin wrote: > Previously only `ident` and generic types were supported in the > `{try_}{pin_}init!` macros. This patch allows arbitrary path fragments, > so for example `Foo::Bar` but also very complex paths such as > `::Bar::<0, i32>`. >=20 > Internally this is accomplished by using `path` fragments. Due to some > peculiar declarative macro limitations, we have to "forget" certain > additional parsing information in the token trees. This is achieved by > the new `retokenize` proc macro. It does not modify the input, but just > strips this information. For example, if a declarative macro takes > `$t:path` as its input, it cannot sensibly propagate this to a macro that > takes `$($p:tt)*` as its input, since the `$t` token will only be > considered one `tt` token for the second macro. If we first pipe the > tokens through `retokenize`, then it parses as expected. >=20 > Suggested-by: Asahi Lina > Signed-off-by: Benno Lossin Reviewed-by: Bj=C3=B6rn Roy Baron > --- > rust/kernel/init/__internal.rs | 2 ++ > rust/kernel/init/macros.rs | 42 +++++++++++++++++++--------------- > rust/macros/lib.rs | 17 +++++++++++++- > 3 files changed, 41 insertions(+), 20 deletions(-) >=20 > diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal= .rs > index 7abd1fb65e41..e36a706a4a1b 100644 > --- a/rust/kernel/init/__internal.rs > +++ b/rust/kernel/init/__internal.rs > @@ -9,6 +9,8 @@ >=20 > use super::*; >=20 > +pub use ::macros::retokenize; > + > /// See the [nomicon] for what subtyping is. See also [this table]. > /// > /// [nomicon]: https://doc.rust-lang.org/nomicon/subtyping.html > diff --git a/rust/kernel/init/macros.rs b/rust/kernel/init/macros.rs > index 5dcb2e513f26..6a82be675808 100644 > --- a/rust/kernel/init/macros.rs > +++ b/rust/kernel/init/macros.rs > @@ -998,7 +998,7 @@ impl<$($impl_generics)*> $pin_data<$($ty_generics)*> > macro_rules! __init_internal { > ( > @this($($this:ident)?), > - @typ($t:ident $(::<$($generics:ty),*>)?), > + @typ($t:path), > @fields($($fields:tt)*), > @error($err:ty), > // Either `PinData` or `InitData`, `$use_data` should only be pr= esent in the `PinData` > @@ -1012,7 +1012,7 @@ macro_rules! __init_internal { > ) =3D> { > $crate::__init_internal!(with_update_parsed: > @this($($this)?), > - @typ($t $(::<$($generics),*>)? ), > + @typ($t), > @fields($($fields)*), > @error($err), > @data($data, $($use_data)?), > @@ -1023,7 +1023,7 @@ macro_rules! __init_internal { > }; > ( > @this($($this:ident)?), > - @typ($t:ident $(::<$($generics:ty),*>)?), > + @typ($t:path), > @fields($($fields:tt)*), > @error($err:ty), > // Either `PinData` or `InitData`, `$use_data` should only be pr= esent in the `PinData` > @@ -1037,7 +1037,7 @@ macro_rules! __init_internal { > ) =3D> { > $crate::__init_internal!(with_update_parsed: > @this($($this)?), > - @typ($t $(::<$($generics),*>)? ), > + @typ($t), > @fields($($fields)*), > @error($err), > @data($data, $($use_data)?), > @@ -1048,7 +1048,7 @@ macro_rules! __init_internal { > }; > ( > @this($($this:ident)?), > - @typ($t:ident $(::<$($generics:ty),*>)?), > + @typ($t:path), > @fields($($fields:tt)*), > @error($err:ty), > // Either `PinData` or `InitData`, `$use_data` should only be pr= esent in the `PinData` > @@ -1062,7 +1062,7 @@ macro_rules! __init_internal { > ) =3D> { > $crate::__init_internal!( > @this($($this)?), > - @typ($t $(::<$($generics),*>)? ), > + @typ($t), > @fields($($fields)*), > @error($err), > @data($data, $($use_data)?), > @@ -1073,7 +1073,7 @@ macro_rules! __init_internal { > }; > (with_update_parsed: > @this($($this:ident)?), > - @typ($t:ident $(::<$($generics:ty),*>)?), > + @typ($t:path), > @fields($($fields:tt)*), > @error($err:ty), > // Either `PinData` or `InitData`, `$use_data` should only be pr= esent in the `PinData` > @@ -1092,7 +1092,7 @@ macro_rules! __init_internal { > // Get the data about fields from the supplied type. > let data =3D unsafe { > use $crate::init::__internal::$has_data; > - $t$(::<$($generics),*>)?::$get_data() > + $crate::init::__internal::retokenize!($t::$get_data()) > }; > // Ensure that `data` really is of type `$data` and help with ty= pe inference: > let init =3D $crate::init::__internal::$data::make_closure::<_, = __InitOk, $err>( > @@ -1247,7 +1247,7 @@ fn is_zeroable(ptr: *mut T) {} > }; > (make_initializer: > @slot($slot:ident), > - @type_name($t:ident), > + @type_name($t:path), > @munch_fields(..Zeroable::zeroed() $(,)?), > @acc($($acc:tt)*), > ) =3D> { > @@ -1263,15 +1263,17 @@ fn is_zeroable(ptr: *mut T) {} > // not get executed, so it has no effect. > ::core::ptr::write($slot, zeroed); > zeroed =3D ::core::mem::zeroed(); > - ::core::ptr::write($slot, $t { > - $($acc)* > - ..zeroed > - }); > + $crate::init::__internal::retokenize!( > + ::core::ptr::write($slot, $t { > + $($acc)* > + ..zeroed > + }); > + ); > } > }; > (make_initializer: > @slot($slot:ident), > - @type_name($t:ident), > + @type_name($t:path), > @munch_fields($(,)?), > @acc($($acc:tt)*), > ) =3D> { > @@ -1279,14 +1281,16 @@ fn is_zeroable(ptr: *mut T) {} > // Since we are in the `if false` branch, this will never get ex= ecuted. We abuse `slot` to > // get the correct type inference here: > unsafe { > - ::core::ptr::write($slot, $t { > - $($acc)* > - }); > + $crate::init::__internal::retokenize!( > + ::core::ptr::write($slot, $t { > + $($acc)* > + }); > + ); > } > }; > (make_initializer: > @slot($slot:ident), > - @type_name($t:ident), > + @type_name($t:path), > @munch_fields($field:ident <- $val:expr, $($rest:tt)*), > @acc($($acc:tt)*), > ) =3D> { > @@ -1299,7 +1303,7 @@ fn is_zeroable(ptr: *mut T) {} > }; > (make_initializer: > @slot($slot:ident), > - @type_name($t:ident), > + @type_name($t:path), > @munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*), > @acc($($acc:tt)*), > ) =3D> { > diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs > index 9f056a5c780a..d329ab622fd4 100644 > --- a/rust/macros/lib.rs > +++ b/rust/macros/lib.rs > @@ -12,7 +12,7 @@ > mod vtable; > mod zeroable; >=20 > -use proc_macro::TokenStream; > +use proc_macro::{Group, TokenStream, TokenTree}; >=20 > /// Declares a kernel module. > /// > @@ -266,3 +266,18 @@ pub fn pinned_drop(args: TokenStream, input: TokenSt= ream) -> TokenStream { > pub fn derive_zeroable(input: TokenStream) -> TokenStream { > zeroable::derive(input) > } > + > +/// Does not modify the given TokenStream, but removes any declarative m= acro information. > +#[proc_macro] > +pub fn retokenize(input: TokenStream) -> TokenStream { > + fn id(tt: TokenTree) -> TokenTree { > + match tt { > + TokenTree::Group(g) =3D> TokenTree::Group(Group::new( > + g.delimiter(), > + g.stream().into_iter().map(id).collect(), > + )), > + x =3D> x, > + } > + } > + input.into_iter().map(id).collect() > +} > -- > 2.41.0