Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp13567400rwd; Sat, 24 Jun 2023 02:39:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6VBqWD51x0XQxIpIpgdB5q6UBn1LdeTtEn6i04VAgWr9gH4CfxAMn0DjXmk53BTENPhLXT X-Received: by 2002:a05:6a00:17a6:b0:666:e954:8ab5 with SMTP id s38-20020a056a0017a600b00666e9548ab5mr29427812pfg.5.1687599549036; Sat, 24 Jun 2023 02:39:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687599549; cv=none; d=google.com; s=arc-20160816; b=mp3yKoZ3RcJ/tqZ5RaL+Y3PCDAdsKK418vwIsCPMLijRfD/NGN2Sl7Zs+7B0EOy2B/ aemZtF8whe3HY4OjtxUbEPq6LXHh/CTwA7S/XBPJF6YRAXTIIkHGJ+d9pul/vAkuxdWt sO+Xa0C99eLWelWpSNyOG7ndB7X01hU5Jm3KdM8R3We8dxzD76lCkuJIyYBKgWOJgWMV 2alzZzqb8e2wkTdlnH+ULoWYrz22hBUV4fM3BxKq9x0Mqn87LpD/+MYd8XDunBSBRhi3 G1lyxNBlNkgHCBg1NtAYhJ0LTi+KIbBnyTGqXQSiX5wSIyyU0Xa874WPj6OHuvaKPE0a hT4A== 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=4u3eXFuoov+G4R/MtGOXrWncucnn87dIfxR6A2h1gls=; fh=Y3hGqDvLhFUfCxW08frpp1lth+cS6Xq25ZUxZll0Zv0=; b=YwwPq/t2lZc8ztxsHMvBMSKIHtcT7ES5SZFMM3BrwovRYYUa6aosCVFL5Ap5ZGNXtW wMsnp5DC5uxZC0j+JnfB0K9r+KtvmhOfaKVsK1aztZKbzKb2M83BFmvonLVPRClxv4Kh ISGkUuPow3CIkRpZ4amAqFEXbkGqo1p2wYGyVo2rjYiSnd6enklygvQ3j9ADMKm5+86r /COASSClMWiP+9a0l8neFWjoEhHNbPFc3YA2iv/ajhKgouX9Z5xDi1M5m9T0m632Z9yL tV5vExw5QlZNTU8Ep8B9eKq8ENvwqNyzcA1RVooMHebSNo/Oq1TXWBDWBQSWFN73Tt4B lJAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=cKuF3PVs; 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 g184-20020a636bc1000000b0054fdf98de2dsi1135791pgc.793.2023.06.24.02.38.54; Sat, 24 Jun 2023 02:39:08 -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=cKuF3PVs; 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 S233116AbjFXJ0e (ORCPT + 99 others); Sat, 24 Jun 2023 05:26:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233056AbjFXJ0S (ORCPT ); Sat, 24 Jun 2023 05:26:18 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07939211E; Sat, 24 Jun 2023 02:25:47 -0700 (PDT) Date: Sat, 24 Jun 2023 09:25:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1687598744; x=1687857944; bh=4u3eXFuoov+G4R/MtGOXrWncucnn87dIfxR6A2h1gls=; 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=cKuF3PVseiBIPHVbekskRtA5tfj9oT8WATPSFesjshLVN3uHAqe8yeQWu5+REZVbq 2VmxbaYsYxdSXMnT1divFecszBbKOM8cL7STDOfT6J6xrZEu4rmlnNqRRx9sayKMdM GaK1SWGq8KHyufHHrVisp9NsfQmBouc3Svanwd7pfCeGNgmcVrRHBP3uS5bBP7R/US WX3JK9BY910N5z290B1ItfCwfmlFA1Eud+tCvOJdsiRucDSado5DQNXJ0DK4qXBeTt t7wv0M5Bt8ofR77pwJpLAUPNVJyxSNtze6fWfSixZLKWRemVfXKDRKifHWQYBwAzDR EHkoN7Sry55tg== 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 Subject: [PATCH 7/7] rust: init: add support for arbitrary paths in init macros Message-ID: <20230624092330.157338-7-benno.lossin@proton.me> In-Reply-To: <20230624092330.157338-1-benno.lossin@proton.me> References: <20230624092330.157338-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=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no 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 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. Suggested-by: Asahi Lina Signed-off-by: Benno Lossin --- rust/kernel/init/__internal.rs | 2 ++ rust/kernel/init/macros.rs | 42 +++++++++++++++++++--------------- rust/macros/lib.rs | 17 +++++++++++++- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.r= s index 7abd1fb65e41..e36a706a4a1b 100644 --- a/rust/kernel/init/__internal.rs +++ b/rust/kernel/init/__internal.rs @@ -9,6 +9,8 @@ use super::*; +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 pres= ent 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 pres= ent 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 pres= ent 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 pres= ent 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 type= 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 exec= uted. 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; -use proc_macro::TokenStream; +use proc_macro::{Group, TokenStream, TokenTree}; /// Declares a kernel module. /// @@ -266,3 +266,18 @@ pub fn pinned_drop(args: TokenStream, input: TokenStre= am) -> TokenStream { pub fn derive_zeroable(input: TokenStream) -> TokenStream { zeroable::derive(input) } + +/// Does not modify the given TokenStream, but removes any declarative mac= ro 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