Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp2462412rdg; Mon, 14 Aug 2023 03:29:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGGTKoQsXja8z/GbKqIwpJ4qW9n7Y822bx8G9CbQdmUGxaopM8igwT6K2f253h4885q3YYt X-Received: by 2002:a17:902:e743:b0:1b6:783d:9ba7 with SMTP id p3-20020a170902e74300b001b6783d9ba7mr12337741plf.27.1692008944034; Mon, 14 Aug 2023 03:29:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692008944; cv=none; d=google.com; s=arc-20160816; b=ilkUlS2ALHMacwTgJm5QOsnNFASXx0wOOaYGvvHc5K3YWXlvPRrJ3fXBCH4B+q3MPH r1qhkkBO2R/ukWkS+ri8pj+txGxkq3Ik5YU93fwU0ZM9HGCOu86RX3zPjrUrRRDM+LDe LbGPMb9JsLZP/KlSUOpbBuaXv84sqUZX2pTuJG+rtgPzlMrhKdyMJ4RmN41UI1Hrdzuy gnDLiZzfSRjSChJqMNgcMNsVklYRuxu9V6sfVwwtRkzn2waqLSeE5JotOzK5Q7IB7qK+ uSdiXPC9EXoIgpdxGS4TzT4hXx3y1qWcnFbd6PFh/tTX4A2BsZe1YhA/yL/hEYZrkwpw q4Ug== 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=yxj7d+etZ8Z/JcONAl0pO5uOZe+qoxHzlb+NGmYO3PA=; fh=h3KS08NeBaYH5bCiXUF1yMk4NKDs2FHNIfHsq8atlgQ=; b=aVhPBva7SPDGDFJcVzBq0XQiM4mQrCR9nSISHU7vJYtgyn2jKhtAJ2KXj5aKeK4cEo aoMplW0Erq56MAT8qP6GWG2GJZeyAq0Mh/9KuIfCYsT37+aQ0eDUIP/oLE3m28kpJk7B xPpnzLleKksWR7CjplQMGEbTttyN3sdjSmtb7cDaG8Q0UkZ/3G+3zpSrv1PF0vpPezg1 YkBD1o8cxhy4n7IMivmiXBSikNRKOll1OBdT8hkmGZzUJTWmTQ6wyahZrEcQnuOcz/To ys4sL+BImuhlHgI/KrAiP4DgMsAmszqp80c6EkZ+aTgaNgJvBvihF5ZaeVzFeU8CWnK+ wdVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=InAkLBj8; 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=proton.me Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k3-20020a170902c40300b001b9e2ce5723si8155749plk.495.2023.08.14.03.28.51; Mon, 14 Aug 2023 03:29:04 -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=@proton.me header.s=protonmail header.b=InAkLBj8; 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=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234490AbjHNIsT (ORCPT + 99 others); Mon, 14 Aug 2023 04:48:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233319AbjHNIro (ORCPT ); Mon, 14 Aug 2023 04:47:44 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84DA510D for ; Mon, 14 Aug 2023 01:47:43 -0700 (PDT) Date: Mon, 14 Aug 2023 08:47:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1692002861; x=1692262061; bh=yxj7d+etZ8Z/JcONAl0pO5uOZe+qoxHzlb+NGmYO3PA=; 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=InAkLBj8A6dnh7fwLDAjeDxjHKwTOKRay1IRrR8wUqc3zQK5WPrv1rqpD6mmil2Mj 1pn5X7ziU3o530tv+lcuSWDu3zw73oU+yOJMatKRHkT18951HHkAWqRPG3CX71gpRm kwtUMTzuSqW5VW+hxEZnWwRdpuycHzxq3IDh+RcANdtjIsJ8j3ZutHWep1ed3h/hU8 47bMQ+Co7RkynsifoGQy/0FLHxYtST7JZdb0w0z0OKINsPa8rlx6lZdDS5/O/2NfL7 d/LOikf98dgQYkxHH9VxV+uvCfGo3uZe70K1nt+s+2ROA3XOQRzn4GusBohSOIRdip KSwX0+0Jt6k3w== To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor From: Benno Lossin Cc: Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Asahi Lina , Martin Rodriguez Reboredo Subject: [PATCH v4 09/13] rust: init: add support for arbitrary paths in init macros Message-ID: <20230814084602.25699-10-benno.lossin@proton.me> In-Reply-To: <20230814084602.25699-1-benno.lossin@proton.me> References: <20230814084602.25699-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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS 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 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>`. 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 using the `paste!` proc macro. It does not actually modify the input, since no `[< >]` will be present in the input, so it just strips the information held by declarative macros. 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 `paste!`, then it parses as expected. Suggested-by: Asahi Lina Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Signed-off-by: Benno Lossin --- v3 -> v4: - added Reviewed-by from Gary. v2 -> v3: - added Reviewed-by's from Martin and Alice. v1 -> v2: - use Gary's `paste!` macro instead of `retokenize`, - remove the retokenize macro. rust/kernel/init/macros.rs | 54 ++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/rust/kernel/init/macros.rs b/rust/kernel/init/macros.rs index 4c86281301d8..d54243cd3c82 100644 --- a/rust/kernel/init/macros.rs +++ b/rust/kernel/init/macros.rs @@ -1000,7 +1000,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 pres= ent in the `PinData` @@ -1014,7 +1014,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)?), @@ -1025,7 +1025,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 pres= ent in the `PinData` @@ -1039,7 +1039,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)?), @@ -1050,7 +1050,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 pres= ent in the `PinData` @@ -1064,7 +1064,7 @@ macro_rules! __init_internal { ) =3D> { $crate::__init_internal!( @this($($this)?), - @typ($t $(::<$($generics),*>)? ), + @typ($t), @fields($($fields)*), @error($err), @data($data, $($use_data)?), @@ -1075,7 +1075,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 pres= ent in the `PinData` @@ -1094,7 +1094,11 @@ 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() + // Here we abuse `paste!` to retokenize `$t`. Declarative macr= os have some internal + // information that is associated to already parsed fragments,= so a path fragment + // cannot be used in this position. Doing the retokenization r= esults in valid rust + // code. + ::kernel::macros::paste!($t::$get_data()) }; // Ensure that `data` really is of type `$data` and help with type= inference: let init =3D $crate::init::__internal::$data::make_closure::<_, __= InitOk, $err>( @@ -1253,7 +1257,7 @@ fn assert_zeroable(_: *mut= T) {} }; (make_initializer: @slot($slot:ident), - @type_name($t:ident), + @type_name($t:path), @munch_fields(..Zeroable::zeroed() $(,)?), @acc($($acc:tt)*), ) =3D> { @@ -1270,15 +1274,21 @@ fn assert_zeroable(_: *m= ut 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 - }); + // Here we abuse `paste!` to retokenize `$t`. Declarative macr= os have some internal + // information that is associated to already parsed fragments,= so a path fragment + // cannot be used in this position. Doing the retokenization r= esults in valid rust + // code. + ::kernel::macros::paste!( + ::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> { @@ -1286,14 +1296,20 @@ fn assert_zeroable(_: *m= ut T) {} // Since we are in the closure that is never called, this will nev= er get executed. // We abuse `slot` to get the correct type inference here: unsafe { - ::core::ptr::write($slot, $t { - $($acc)* - }); + // Here we abuse `paste!` to retokenize `$t`. Declarative macr= os have some internal + // information that is associated to already parsed fragments,= so a path fragment + // cannot be used in this position. Doing the retokenization r= esults in valid rust + // code. + ::kernel::macros::paste!( + ::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> { @@ -1306,7 +1322,7 @@ fn assert_zeroable(_: *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> { --=20 2.41.0