Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp2258489rwb; Sat, 29 Jul 2023 03:21:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlG4W1Z9ep0LDj57m69AnFmXQ9vx9plWLZBOLWl6KJ3BnQUNlBbKYVHo/kIH3fHaMWqmJufZ X-Received: by 2002:ac2:5e2b:0:b0:4fb:5dd5:715c with SMTP id o11-20020ac25e2b000000b004fb5dd5715cmr2712942lfg.4.1690626069167; Sat, 29 Jul 2023 03:21:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690626069; cv=none; d=google.com; s=arc-20160816; b=YDWYt/KsEBK/nKntKqJngc4kDLONqk9L+y3j31M8D57dvbCA4wH2lCbvhQUwWZO/0w 0Pi9qtkyL3YBtkqb+sUEi85y+zlEd4Rashg4+KAz9ddjL/YV5Vgap2zPAnOmhOo/Q8Uc rOUDK+D2y8TDYO+CSFhEL1GEW4SZSKeGGiTs59uo4EeK1SWCqddh7IGU4+09CkJwpviy dm5SEfVhZlxsREBCWYaF0Ki1LsRf7IQrsAyraBW9Ft4lc2zHxgW6XI1ugKlKNQL8pUt+ pw+qdzdLO6GPky8uyh4MNV0B2a7K18H+L36AesKzfjXDuOXk0hRqHefDZL56jtjJ6wP5 hXWA== 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=UwJBusNwPUw169oexSJli8hQ25g/5KFXHZuYaKqD2S8=; fh=MTk7MD/292FwJGz5jKS0PBOnpzkNxprDjhqM8t+E7nE=; b=hc68xqT7hwVfVRKQoyb4JzbUqvyZpld4KgH/bcnGYFKOx9K57WJIyS1NmUSkSfjHwe ij+RhAI3le3HCSPUtPFc6KiGuPniW7KWFgb/2F8Uc19IggpizW+iAShFSBYHFe6S1+mt CA7159345EzWHetgOjTBO/9kige0zrTygjMBts87EMyU2+AALf/NX2QtmjD/ahXEBy9B gJSU6wWgmQ+YvoCk0uUuU0a4wL2vU3hoAqJAcuzGaL9FoJ+Ck5i+VUmPZXw2BOJFL6XQ ivAeUrLwFxnlOYFQ1a3ir6ArgafgeS2UUabnOK7wQMsZzAifFNrOF8cfRC07s6/5ynLr n8+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=fuKNzDRX; 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 l11-20020aa7d94b000000b005224e403e96si4026447eds.196.2023.07.29.03.20.44; Sat, 29 Jul 2023 03:21:09 -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=fuKNzDRX; 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 S231680AbjG2JLi (ORCPT + 99 others); Sat, 29 Jul 2023 05:11:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231527AbjG2JLS (ORCPT ); Sat, 29 Jul 2023 05:11:18 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B9E44C16; Sat, 29 Jul 2023 02:10:31 -0700 (PDT) Date: Sat, 29 Jul 2023 09:10:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1690621828; x=1690881028; bh=UwJBusNwPUw169oexSJli8hQ25g/5KFXHZuYaKqD2S8=; 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=fuKNzDRX36zsowd+EUph9dhtDnDJysdkESS1IJXuSmjveBiXsV9GIS8W43oLiWyU7 xFlXhDfDPs0wVGYIOY3g7ZJa4K/HOGEUs26DZQ1XHRSKEGzX15M7/hCprOzdTHUCOT 49O0DXRVorVyhmnSfj6GQ0x8/fNAUYYv6RjKiLNbYYCbeRxNzQaYZk7+wWK1hwH4oh X64VH9AL8qNQGVS744JUCgtU8lYnrjUidm1YLv5ZdKLTzuqyu3tHSj+95GvtghUozO XoyRLmBXrrYUeAEJkiCEbOv0z4QKptCptkAFxyuDWS1/+IG+74hb6TAfSifMPSG+Yz WMwSTaWMiuYcA== 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, Asahi Lina , Martin Rodriguez Reboredo Subject: [PATCH v3 09/13] rust: init: add support for arbitrary paths in init macros Message-ID: <20230729090838.225225-10-benno.lossin@proton.me> In-Reply-To: <20230729090838.225225-1-benno.lossin@proton.me> References: <20230729090838.225225-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_MSPIKE_H5, RCVD_IN_MSPIKE_WL,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 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 Signed-off-by: Benno Lossin --- 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