From patchwork Sat Jun 24 09:25:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Benno Lossin X-Patchwork-Id: 112433 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp6292920vqr; Sat, 24 Jun 2023 02:31:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6GFWVpgCdDE7BnpsP/oNRX7CBG2QVVeor/If634WSOlep1CClc8VGHRYe48m6S7mVoBlSC X-Received: by 2002:a17:902:c10c:b0:1b5:4709:fa0e with SMTP id 12-20020a170902c10c00b001b54709fa0emr1550380pli.10.1687599099957; Sat, 24 Jun 2023 02:31:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687599099; cv=none; d=google.com; s=arc-20160816; b=A7ufkskjBlciNzsXGHGftHQd4XYdJngfX+2VrqBUWw8Qi64WJty7CooawSZXZqtZZm Xc/55BBqfK89tcX244K1OZsAL1BwVkG1XLQlPCnsArUmFOng12xZBIn5hwIu4W8H0uYW /KYbo4RLnq6wqCZIH77fP1OJQ8pdHb4oiUh3j62+uiKKST+q7C8gLlSgk0kaWtIxg/74 +tf6gEvoJLN0TdWwmfW3oRs7QIhjPKIEZ6q96VmJg5vY7ILB+xzDKv+FTk3/ou95gksO Wx+pmYCVJLkTW6kC60i8fona+U85564HoDfUeMU7wg113nFFSfkqxYsu0kFaszs1mOGV vlGg== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l10-20020a170903244a00b001ae3cb42bfdsi1004426pls.634.2023.06.24.02.31.26; Sat, 24 Jun 2023 02:31:39 -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 S232760AbjFXJ0e (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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769575914221333756?= X-GMAIL-MSGID: =?utf-8?q?1769575914221333756?= 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 Reviewed-by: Björn 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(-) -- 2.41.0 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 @@ 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 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,7 @@ 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() + $crate::init::__internal::retokenize!($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>( @@ -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)*), ) => { @@ -1263,15 +1263,17 @@ fn is_zeroable(ptr: *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 - }); + $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)*), ) => { @@ -1279,14 +1281,16 @@ fn is_zeroable(ptr: *mut T) {} // Since we are in the `if false` branch, this will never get executed. 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)*), ) => { @@ -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)*), ) => { 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: TokenStream) -> TokenStream { pub fn derive_zeroable(input: TokenStream) -> TokenStream { zeroable::derive(input) } + +/// Does not modify the given TokenStream, but removes any declarative macro information. +#[proc_macro] +pub fn retokenize(input: TokenStream) -> TokenStream { + fn id(tt: TokenTree) -> TokenTree { + match tt { + TokenTree::Group(g) => TokenTree::Group(Group::new( + g.delimiter(), + g.stream().into_iter().map(id).collect(), + )), + x => x, + } + } + input.into_iter().map(id).collect() +}