Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp5282315rwr; Mon, 24 Apr 2023 01:29:35 -0700 (PDT) X-Google-Smtp-Source: AKy350a2mP1n5+UMqatLCn1KQHJF7Fj5+zt/ENcXEFARGgfMuIGEuT5qipTHsbnqK8H3D1wgVEQK X-Received: by 2002:a05:6a00:1304:b0:63b:8423:9e31 with SMTP id j4-20020a056a00130400b0063b84239e31mr15530816pfu.11.1682324974923; Mon, 24 Apr 2023 01:29:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682324974; cv=none; d=google.com; s=arc-20160816; b=G3Wi5T+2rYV1ZxelNX5KKHqUEAJACre+puQC8LPAPSiXEegLIt3Q99nj+8c6IcA9y3 XPfeFXXi/sUSWcz+tJnmsTOs7bp3A8Ycpnjn+z4hlNiVHgXljB/r+Oa8d7IAXvI/akRj r4GoBJxBVHFvMJgFQ1N5abfDFFw+G5odU/I9FmGvaPVvcO7V+lWzB1xmJMI8vGali2SF 9CPX9CImz++jX/zKX6HIXFwRCyP9TLhPyov2syRLx9e384PVGLtC0Sa4moKoNvP/BzsN 5880iHEC0XnZORZu7gUDPhnJ3wgAzXuJSNP329iQafxjLiLZMR4FHY8t14JcA+bUligp iaPQ== 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=xsbkQLlU/8z2bpso9Wuc5fgrVJCsTlQwnkd8h5OEwvU=; b=jgeeKuG/U5iz6Pzn52VawDNJc1g5kPdWvX2Ba3L834onsftOl62WdwNHEM09lOImBm O0+oFSkh675pVUQM7SJDhDbojh3cAF4MVfQPkstxkEqqgAWf8LDUfv6S7XNiT4sntPpr QzVc7/bw9YNAaJQe2P9p4kgpg7WGJCGLonYiDQrV/aZzc4j0eFjUFZ7NjyIVjXTTZmIP a2ZR33+3dpYOAFpY0a9TyJ44KvUzX98P89dA8b0OgOwL+QGwUTjEL+62mXQIvGRqQ89q TtL2hg+URRBvtXQwzOnWiwxEzl2EmlthtliM4A/WIn27WpSbRFK1y1iDHWl2mx9TJbxg 7T4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=kqFIpKXH; 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 u11-20020a6540cb000000b0051ace49ded1si6148128pgp.886.2023.04.24.01.29.23; Mon, 24 Apr 2023 01:29:34 -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=kqFIpKXH; 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 S231588AbjDXIMK (ORCPT + 99 others); Mon, 24 Apr 2023 04:12:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231572AbjDXIMF (ORCPT ); Mon, 24 Apr 2023 04:12:05 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 167A71733 for ; Mon, 24 Apr 2023 01:11:57 -0700 (PDT) Date: Mon, 24 Apr 2023 08:11:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1682323916; x=1682583116; bh=xsbkQLlU/8z2bpso9Wuc5fgrVJCsTlQwnkd8h5OEwvU=; 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=kqFIpKXHsXe4ce/a6ZebDqdoMZtkzYCsVdVV/oVVFk9qV/N8DvQP7fcO6BJDb/5xd rE7h4WP7hnTkSOzFte4OMLFvaqIJkaJxiKdG4N5nw4aDMMBB0DrmYGaXFqG1N50UJR XnYrRYSG/cdD0YtH/21BZj3Qcou4nRjSWQocJ0NWMCMd4QC+RAKB8dbm+9+Iu9gUdL nl4JTBvGUuCys0ordxlD0m/dxla8zNLDMSuQgl8zcE1h9o85Tna0W/I4Tv8wpF8a2t lfypQCpWvRd72Bf7pfQVrpoSDlsvYSsbpKY9+HFTlh3j9rEr2W6649rQUxwaJAzlZn O8esjaT61K8eQ== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Benno Lossin , Alice Ryhl Subject: [PATCH 3/4] rust: macros: replace Self with the concrete type in #[pin_data] Message-ID: <20230424081112.99890-3-benno.lossin@proton.me> In-Reply-To: <20230424081112.99890-1-benno.lossin@proton.me> References: <20230424081112.99890-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_H2,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 When using `#[pin_data]` on a struct that used `Self` in the field types, a type error would be emitted when trying to use `pin_init!`. Since an internal type would be referenced by `Self` instead of the defined struct. This patch fixes this issue by replacing all occurrences of `Self` in the `#[pin_data]` macro with the concrete type circumventing the issue. Since rust allows type definitions inside of blocks, which are expressions, the macro also checks for these and emits a compile error when it finds `trait`, `enum`, `union`, `struct` or `impl`. These keywords allow creating new `Self` contexts, which conflicts with the current implementation of replacing every `Self` ident. If these were allowed, some `Self` idents would be replaced incorrectly. Signed-off-by: Benno Lossin Reported-by: Alice Ryhl --- rust/macros/pin_data.rs | 108 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 4 deletions(-) diff --git a/rust/macros/pin_data.rs b/rust/macros/pin_data.rs index c593b05d9e8c..6d58cfda9872 100644 --- a/rust/macros/pin_data.rs +++ b/rust/macros/pin_data.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT use crate::helpers::{parse_generics, Generics}; -use proc_macro::TokenStream; +use proc_macro::{Group, Punct, Spacing, TokenStream, TokenTree}; pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStre= am { // This proc-macro only does some pre-parsing and then delegates the a= ctual parsing to @@ -12,16 +12,116 @@ pub(crate) fn pin_data(args: TokenStream, input: Token= Stream) -> TokenStream { impl_generics, ty_generics, }, - mut rest, + rest, ) =3D parse_generics(input); + // The struct definition might contain the `Self` type. Since `__pin_d= ata!` will define a new + // type with the same generics and bounds, this poses a problem, since= `Self` will refer to the + // new type as opposed to this struct definition. Therefore we have to= replace `Self` with the + // concrete name. + + // Errors that occur when replacing `Self` with `struct_name`. + let mut errs =3D TokenStream::new(); + // The name of the struct with ty_generics. + let struct_name =3D rest + .iter() + .skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if i.to_string(= ) =3D=3D "struct")) + .nth(1) + .and_then(|tt| match tt { + TokenTree::Ident(_) =3D> { + let tt =3D tt.clone(); + let mut res =3D vec![tt]; + if !ty_generics.is_empty() { + // We add this, so it is maximally compatible with e.g= . `Self::CONST` which + // will be replaced by `StructName::<$generics>::CONST= `. + res.push(TokenTree::Punct(Punct::new(':', Spacing::Joi= nt))); + res.push(TokenTree::Punct(Punct::new(':', Spacing::Alo= ne))); + res.push(TokenTree::Punct(Punct::new('<', Spacing::Alo= ne))); + res.extend(ty_generics.iter().cloned()); + res.push(TokenTree::Punct(Punct::new('>', Spacing::Alo= ne))); + } + Some(res) + } + _ =3D> None, + }) + .unwrap_or_else(|| { + // If we did not find the name of the struct then we will use = `Self` as the replacement + // and add a compile error to ensure it does not compile. + errs.extend( + "::core::compile_error!(\"Could not locate type name.\");" + .parse::() + .unwrap(), + ); + "Self".parse::().unwrap().into_iter().collect() + }); + let impl_generics =3D impl_generics + .into_iter() + .flat_map(|tt| replace_self_and_deny_type_defs(&struct_name, tt, &= mut errs)) + .collect::>(); + let mut rest =3D rest + .into_iter() + .flat_map(|tt| { + // We ignore top level `struct` tokens, since they would emit = a compile error. + if matches!(&tt, TokenTree::Ident(i) if i.to_string() =3D=3D "= struct") { + vec![tt] + } else { + replace_self_and_deny_type_defs(&struct_name, tt, &mut err= s) + } + }) + .collect::>(); // This should be the body of the struct `{...}`. let last =3D rest.pop(); - quote!(::kernel::__pin_data! { + let mut quoted =3D quote!(::kernel::__pin_data! { parse_input: @args(#args), @sig(#(#rest)*), @impl_generics(#(#impl_generics)*), @ty_generics(#(#ty_generics)*), @body(#last), - }) + }); + quoted.extend(errs); + quoted +} + +/// Replaces `Self` with `struct_name` and errors on `enum`, `trait`, `str= uct` `union` and `impl` +/// keywords. +/// +/// The error is appended to `errs` to allow normal parsing to continue. +fn replace_self_and_deny_type_defs( + struct_name: &Vec, + tt: TokenTree, + errs: &mut TokenStream, +) -> Vec { + match tt { + TokenTree::Ident(ref i) + if i.to_string() =3D=3D "enum" + || i.to_string() =3D=3D "trait" + || i.to_string() =3D=3D "struct" + || i.to_string() =3D=3D "union" + || i.to_string() =3D=3D "impl" =3D> + { + errs.extend( + format!( + "::core::compile_error!(\"Cannot use `{i}` inside of s= truct definition with \ + `#[pin_data]`.\");" + ) + .parse::() + .unwrap() + .into_iter() + .map(|mut tok| { + tok.set_span(tt.span()); + tok + }), + ); + vec![tt] + } + TokenTree::Ident(i) if i.to_string() =3D=3D "Self" =3D> struct_nam= e.clone(), + TokenTree::Literal(_) | TokenTree::Punct(_) | TokenTree::Ident(_) = =3D> vec![tt], + TokenTree::Group(g) =3D> vec![TokenTree::Group(Group::new( + g.delimiter(), + g.stream() + .into_iter() + .flat_map(|tt| replace_self_and_deny_type_defs(struct_name= , tt, errs)) + .collect(), + ))], + } } -- 2.40.0