From patchwork Wed Jul 19 14:21:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benno Lossin X-Patchwork-Id: 122653 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2482834vqt; Wed, 19 Jul 2023 07:35:36 -0700 (PDT) X-Google-Smtp-Source: APBJJlEzIwQXEVBRKxDkQz6ai15KrkI0DBFkfepIQtQs7pD/e+5ZvDvi2VEcXQq9p68KtE+cuy6w X-Received: by 2002:a17:906:3150:b0:992:b928:adb3 with SMTP id e16-20020a170906315000b00992b928adb3mr2264430eje.54.1689777336460; Wed, 19 Jul 2023 07:35:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689777336; cv=none; d=google.com; s=arc-20160816; b=fZZySq2yBfMmYmCUqD+cEvGszMaYqs6i+WIutE1XwpkCVaYQhaN12XKnMFKFk1TKTC o4xPX3VGCvbWq6j5hjN0mdEgHrUv3jvkrYB34A1DDHpo5ThrFWdKxD13ziPJzTVp3R9A y5Rc8zziKMQWm2JPbwa1DKZ04a8o0BawBGsOfONjeHueZi033g/OWUJJe5EYLePo4Rsq Mq0OvKWlP8QsSBCSb5ZIMVLSsnSzWlh+BebXjvN71T3JLN+c3g/vIjSnTNMlWKC32Gnp CMhIW3iEToAy5yY/cewBJ5Qv0l09horJXZx9hU7XyoKVZbKUheB99mhDXl7baqBxBYWh FasQ== 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=YIuxWzFErHS/G2fc91GDnhwDMG2gDHUgDWefQ2yWmME=; fh=ahHPrvxY+ty2HIuIPh5QXslVmwB1wHeJjzGZ+GeRryw=; b=zGL0DCCGWFkII5W9S3OqkWYXDQvrg9r8/+emRNsVvlHengpGZ1KESMz7us1Ok8YvY5 Xum/IXc1SsOupm0OLvCmmjX72XeDkGqlCN/DzTNkLrtyQEzbZP8S5seb6C9f3rr7BBmh fCRSYmgpfu9IRZL79bWoLV6tXbAPMCgI+ECs5fuxRxroWyGympOwShZ94J9r41sdzkyd YxtZwnlhYZjrfbzSbvM3ZHZ+PmNoeypDnHNUrnyGq2ZDU7+NPdLLlcSDJX8+ga+r8b2i gUOUdhEpK1sHVi4gtoLAzk5Jtr/XGJy6FxFndk1/vNBVt8t020HnSi/Th/3WEuy+Uj97 UiIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=bfuufCxG; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gq15-20020a170906e24f00b0098886d50308si2914811ejb.490.2023.07.19.07.35.08; Wed, 19 Jul 2023 07:35:36 -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=bfuufCxG; 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 S231508AbjGSOWC (ORCPT + 99 others); Wed, 19 Jul 2023 10:22:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230375AbjGSOWA (ORCPT ); Wed, 19 Jul 2023 10:22:00 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3BC926A2 for ; Wed, 19 Jul 2023 07:21:34 -0700 (PDT) Date: Wed, 19 Jul 2023 14:21:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1689776477; x=1690035677; bh=YIuxWzFErHS/G2fc91GDnhwDMG2gDHUgDWefQ2yWmME=; 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=bfuufCxGbBZv9CUYoDr0RrvLhYTmm6dmIHOS0Z2bDUwUcxyV6I0hjWszLTaF5Vx5W HrUqFylFnj4WwjhZTlCVCKQvs1YRGQCwXprm5Nr0l/mz1Nb+324CqE2J/mGkCUR2Gv gpE139ybVE5a7d0kjaeMQrhdtjfHDT5JBHlcMsC2vN2/OMhcj3iW8VepYvKzet2/7j cOjROJaG5Rqy8P4ntFLseemzGm9YQwtsnmDBDSfW7lOTWfFosVxG1GsbCnlqPZF4Gn c6YXXZhp5uNYg8W/2TLIjYTI1qdc4wtStblB/YSAdFAeBUjhUpU4PaYgfPR2J7v5Bg /KeRmfHZMiw/A== 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 Subject: [PATCH v2 08/12] rust: init: add support for arbitrary paths in init macros Message-ID: <20230719141918.543938-9-benno.lossin@proton.me> In-Reply-To: <20230719141918.543938-1-benno.lossin@proton.me> References: <20230719141918.543938-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 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, 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771859960500235263 X-GMAIL-MSGID: 1771859960500235263 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 Signed-off-by: Benno Lossin Reviewed-by: Alice Ryhl Reviewed-by: Martin Rodriguez Reboredo --- 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 f5d7f0943f60..345cfc0e6d37 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 present in the `PinData` @@ -1012,7 +1012,7 @@ macro_rules! __init_internal { ) => { $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 present in the `PinData` @@ -1037,7 +1037,7 @@ macro_rules! __init_internal { ) => { $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 present in the `PinData` @@ -1062,7 +1062,7 @@ macro_rules! __init_internal { ) => { $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 present in the `PinData` @@ -1092,7 +1092,11 @@ macro_rules! __init_internal { // Get the data about fields from the supplied type. let data = unsafe { use $crate::init::__internal::$has_data; - $t$(::<$($generics),*>)?::$get_data() + // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal + // information that is associated to already parsed fragments, so a path fragment + // cannot be used in this position. Doing the retokenization results 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 = $crate::init::__internal::$data::make_closure::<_, __InitOk, $err>( @@ -1251,7 +1255,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)*), ) => { @@ -1268,15 +1272,21 @@ fn assert_zeroable(_: *mut T) {} // not get executed, so it has no effect. ::core::ptr::write($slot, zeroed); zeroed = ::core::mem::zeroed(); - ::core::ptr::write($slot, $t { - $($acc)* - ..zeroed - }); + // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal + // information that is associated to already parsed fragments, so a path fragment + // cannot be used in this position. Doing the retokenization results 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)*), ) => { @@ -1284,14 +1294,20 @@ fn assert_zeroable(_: *mut T) {} // Since we are in the closure that is never called, this will never 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 macros have some internal + // information that is associated to already parsed fragments, so a path fragment + // cannot be used in this position. Doing the retokenization results 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)*), ) => { @@ -1304,7 +1320,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)*), ) => {