Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5614886rdb; Wed, 13 Dec 2023 14:09:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IFQrcgDwrdvRn2XfXS3gOlNs5Pkd58AAm70zylxvXIwavol/M7DYaRQy1IVgSPNlNp5kOJE X-Received: by 2002:a92:cd87:0:b0:35d:7ea6:fee9 with SMTP id r7-20020a92cd87000000b0035d7ea6fee9mr8919297ilb.10.1702505362774; Wed, 13 Dec 2023 14:09:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702505362; cv=none; d=google.com; s=arc-20160816; b=bq+xkqVSvmeLszZGqeovbcUBzW9izI5lWmZRT2A5FTLYHsC3iXtJsFtU0jqWWAnTls TqfzfA7MquERpw77+3KHHYWx43fQoabucN1icKAomcFKJ2ExtzFXTcAhgDTXyus0qFyj o+dTLugLj01K1y8cmFUuSzQvqHyV0bcpZmNGbRdqrMB2eNAZuMVDM22+t7wyWS0W/GHj Awon7mmbfiih6HWAr6vTKlnb60vkEC5ALVJxDWWAXzevPweuZteETG1X7na21JlDRfsv 36y6pL/y3PhE4NAZlpXXDVJoUi2z4tAEoo9awzJWLDfPz4EAJhdXqCTeol5Dr8lpEON2 gkqw== 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 :date:dkim-signature; bh=/sxUPUCZGGUcmNBROpNTSbutQhtb2lUVRGVV1RJCeAs=; fh=Ov/rTNdileIkvoFBvDwlWN6kRuLU6cjKBx/1/vxBh1M=; b=YXPCpKZx+4BOJ/rxkkakZQiB+7Xv4s+12t0nv6a0/WmnINW9/JFMuvSFmTb9JSqqRv EEt6fsNHsMveoe3RwJ18y+N4t9XqL0ECFHzlunIq2ONwhmYXeZtgKk7Zv6Ql/G6iCheB eMszqEEQLQTRV/gJC6zRiwFS25WxUsMFNCSPL/TwOA+jpqe7mdmJcybECKchnPO+7fYY L3qkzxIPduZJp48IULvEgh1JfOqNEZmNogDlNzqGrb8HlB7smJscL6GpjaLJvc/f3Adx HkUbrA1cxCjvhd8f3VdcCqf/RqjJixpEtS3b82lmUTJRffNswsm1QDLRjm8RsM3vLO7/ 3WnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=5ij2hnl3gzgkvokoca6crcyvfa.protonmail header.b=eVe3aIjz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id o10-20020a63e34a000000b00565e0624182si10121151pgj.404.2023.12.13.14.09.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 14:09:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=5ij2hnl3gzgkvokoca6crcyvfa.protonmail header.b=eVe3aIjz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id CDD33826FA9E; Wed, 13 Dec 2023 14:09:19 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233930AbjLMWJE (ORCPT + 99 others); Wed, 13 Dec 2023 17:09:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230397AbjLMWJC (ORCPT ); Wed, 13 Dec 2023 17:09:02 -0500 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34A9DAF for ; Wed, 13 Dec 2023 14:09:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=5ij2hnl3gzgkvokoca6crcyvfa.protonmail; t=1702505346; x=1702764546; bh=/sxUPUCZGGUcmNBROpNTSbutQhtb2lUVRGVV1RJCeAs=; 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=eVe3aIjzzfpnSaAPoKNkuOCaVOTNRVRQ7OBVrNOfRNGMvVVnJhid9GQkHQ9e3f5Ec vFr2k082SBsYljRG7Au0EFKa+s9H32ZwigRPmskYl5N9ppBNr8IX1RoXz3aXTcD5cy AXNgeySoFLHutIOPxsX7c500uC9Uhi3qeT7TWIDmIY7q4hfliMsinSPLdVbqfZ7jLh XDLPvQPKn4414FtmQm0AfMXHQjXZDG13pwk1iahO6zupKC+nFBOjmLkMMLzwCAtztE DmiIJXOD2I4jIgVo5jnjI38o8q6js+CNWP7yx49ch4qZMtsztWmMHH9qEauNSh8owG rfEsUmfjdn7/g== Date: Wed, 13 Dec 2023 22:08:53 +0000 To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Martin Rodriguez Reboredo From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/3] rust: macros: allow generic parameter default values in `#[pin_data]` Message-ID: <20231213220447.3613500-2-benno.lossin@proton.me> In-Reply-To: <20231213220447.3613500-1-benno.lossin@proton.me> References: <20231213220447.3613500-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=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 13 Dec 2023 14:09:19 -0800 (PST) Add support for generic parameters defaults in `#[pin_data]` by using the newly introduced `decl_generics` instead of the `impl_generics`. Before this would not compile: #[pin_data] struct Foo { // ... } because it would be expanded to this: struct Foo { // ... } const _: () =3D { struct __ThePinData { __phantom: ::core::marker::PhantomData) -> Foo>, } impl ::core::clone::Clone for __ThePinData= { fn clone(&self) -> Self { *self } } // [...] rest of expansion omitted }; The problem is with the `impl`, since that is invalid Rust syntax. It should not mention the default value at all, since default values only make sense on type definitions. The new `impl_generics` do not contain the default values, thus generating correct Rust code. This is used by the next commit that puts `#[pin_data]` on `kernel::workqueue::Work`. Signed-off-by: Benno Lossin --- v1 -> v2: - clarify the change in the commit message - add motivation to the commit message rust/kernel/init/macros.rs | 19 ++++++++++++++++++- rust/macros/pin_data.rs | 3 ++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/rust/kernel/init/macros.rs b/rust/kernel/init/macros.rs index cb6e61b6c50b..624e9108e3b4 100644 --- a/rust/kernel/init/macros.rs +++ b/rust/kernel/init/macros.rs @@ -538,6 +538,7 @@ macro_rules! __pin_data { ), @impl_generics($($impl_generics:tt)*), @ty_generics($($ty_generics:tt)*), + @decl_generics($($decl_generics:tt)*), @body({ $($fields:tt)* }), ) =3D> { // We now use token munching to iterate through all of the fields.= While doing this we @@ -560,6 +561,9 @@ macro_rules! __pin_data { @impl_generics($($impl_generics)*), // The 'ty generics', the generics that will need to be specif= ied on the impl blocks. @ty_generics($($ty_generics)*), + // The 'decl generics', the generics that need to be specified= on the struct + // definition. + @decl_generics($($decl_generics)*), // The where clause of any impl block and the declaration. @where($($($whr)*)?), // The remaining fields tokens that need to be processed. @@ -585,6 +589,7 @@ macro_rules! __pin_data { @name($name:ident), @impl_generics($($impl_generics:tt)*), @ty_generics($($ty_generics:tt)*), + @decl_generics($($decl_generics:tt)*), @where($($whr:tt)*), // We found a PhantomPinned field, this should generally be pinned= ! @fields_munch($field:ident : $($($(::)?core::)?marker::)?PhantomPi= nned, $($rest:tt)*), @@ -607,6 +612,7 @@ macro_rules! __pin_data { @name($name), @impl_generics($($impl_generics)*), @ty_generics($($ty_generics)*), + @decl_generics($($decl_generics)*), @where($($whr)*), @fields_munch($($rest)*), @pinned($($pinned)* $($accum)* $field: ::core::marker::Phantom= Pinned,), @@ -623,6 +629,7 @@ macro_rules! __pin_data { @name($name:ident), @impl_generics($($impl_generics:tt)*), @ty_generics($($ty_generics:tt)*), + @decl_generics($($decl_generics:tt)*), @where($($whr:tt)*), // We reached the field declaration. @fields_munch($field:ident : $type:ty, $($rest:tt)*), @@ -640,6 +647,7 @@ macro_rules! __pin_data { @name($name), @impl_generics($($impl_generics)*), @ty_generics($($ty_generics)*), + @decl_generics($($decl_generics)*), @where($($whr)*), @fields_munch($($rest)*), @pinned($($pinned)* $($accum)* $field: $type,), @@ -656,6 +664,7 @@ macro_rules! __pin_data { @name($name:ident), @impl_generics($($impl_generics:tt)*), @ty_generics($($ty_generics:tt)*), + @decl_generics($($decl_generics:tt)*), @where($($whr:tt)*), // We reached the field declaration. @fields_munch($field:ident : $type:ty, $($rest:tt)*), @@ -673,6 +682,7 @@ macro_rules! __pin_data { @name($name), @impl_generics($($impl_generics)*), @ty_generics($($ty_generics)*), + @decl_generics($($decl_generics)*), @where($($whr)*), @fields_munch($($rest)*), @pinned($($pinned)*), @@ -689,6 +699,7 @@ macro_rules! __pin_data { @name($name:ident), @impl_generics($($impl_generics:tt)*), @ty_generics($($ty_generics:tt)*), + @decl_generics($($decl_generics:tt)*), @where($($whr:tt)*), // We found the `#[pin]` attr. @fields_munch(#[pin] $($rest:tt)*), @@ -705,6 +716,7 @@ macro_rules! __pin_data { @name($name), @impl_generics($($impl_generics)*), @ty_generics($($ty_generics)*), + @decl_generics($($decl_generics)*), @where($($whr)*), @fields_munch($($rest)*), // We do not include `#[pin]` in the list of attributes, since= it is not actually an @@ -724,6 +736,7 @@ macro_rules! __pin_data { @name($name:ident), @impl_generics($($impl_generics:tt)*), @ty_generics($($ty_generics:tt)*), + @decl_generics($($decl_generics:tt)*), @where($($whr:tt)*), // We reached the field declaration with visibility, for simplicit= y we only munch the // visibility and put it into `$accum`. @@ -741,6 +754,7 @@ macro_rules! __pin_data { @name($name), @impl_generics($($impl_generics)*), @ty_generics($($ty_generics)*), + @decl_generics($($decl_generics)*), @where($($whr)*), @fields_munch($field $($rest)*), @pinned($($pinned)*), @@ -757,6 +771,7 @@ macro_rules! __pin_data { @name($name:ident), @impl_generics($($impl_generics:tt)*), @ty_generics($($ty_generics:tt)*), + @decl_generics($($decl_generics:tt)*), @where($($whr:tt)*), // Some other attribute, just put it into `$accum`. @fields_munch(#[$($attr:tt)*] $($rest:tt)*), @@ -773,6 +788,7 @@ macro_rules! __pin_data { @name($name), @impl_generics($($impl_generics)*), @ty_generics($($ty_generics)*), + @decl_generics($($decl_generics)*), @where($($whr)*), @fields_munch($($rest)*), @pinned($($pinned)*), @@ -789,6 +805,7 @@ macro_rules! __pin_data { @name($name:ident), @impl_generics($($impl_generics:tt)*), @ty_generics($($ty_generics:tt)*), + @decl_generics($($decl_generics:tt)*), @where($($whr:tt)*), // We reached the end of the fields, plus an optional additional c= omma, since we added one // before and the user is also allowed to put a trailing comma. @@ -802,7 +819,7 @@ macro_rules! __pin_data { ) =3D> { // Declare the struct with all fields in the correct order. $($struct_attrs)* - $vis struct $name <$($impl_generics)*> + $vis struct $name <$($decl_generics)*> where $($whr)* { $($fields)* diff --git a/rust/macros/pin_data.rs b/rust/macros/pin_data.rs index 022e68e9720d..1d4a3547c684 100644 --- a/rust/macros/pin_data.rs +++ b/rust/macros/pin_data.rs @@ -10,7 +10,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStr= eam) -> TokenStream { let ( Generics { impl_generics, - decl_generics: _, + decl_generics, ty_generics, }, rest, @@ -77,6 +77,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStr= eam) -> TokenStream { @sig(#(#rest)*), @impl_generics(#(#impl_generics)*), @ty_generics(#(#ty_generics)*), + @decl_generics(#(#decl_generics)*), @body(#last), }); quoted.extend(errs); --=20 2.42.0