From: Gary Guo <gary@garyguo.net>
To: Benno Lossin <benno.lossin@proton.me>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Andreas Hindborg" <nmi@metaspace.dk>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
patches@lists.linux.dev, "Asahi Lina" <lina@asahilina.net>
Subject: Re: [PATCH 3/7] rust: init: make guards in the init macros hygienic
Date: Wed, 28 Jun 2023 17:48:07 +0100 [thread overview]
Message-ID: <20230628174807.5eabf3bd.gary@garyguo.net> (raw)
In-Reply-To: <w4kyaXwWZEyoBoCCFmVtRmkUS6d0m_DJYTs7OVWLkbBX7PFwjj_wemi1mtvSemjUyhZwSDCA2l-5fXn4sxO4C3DT_8wGyZebpea-wQseOmA=@proton.me>
On Wed, 28 Jun 2023 11:41:59 +0000
Benno Lossin <benno.lossin@proton.me> wrote:
> On 25.06.23 22:54, Gary Guo wrote:
> > On Sat, 24 Jun 2023 09:25:10 +0000
> > Benno Lossin <benno.lossin@proton.me> wrote:
> >
> >> Use hygienic identifiers for the guards instead of the field names. This
> >> makes the init macros feel more like normal struct initializers, since
> >> assigning identifiers with the name of a field does not create
> >> conflicts.
> >> Also change the internals of the guards, no need to make the `forget`
> >> function `unsafe`, since users cannot access the guards anyways. Now the
> >> guards are carried directly on the stack and have no extra `Cell<bool>`
> >> field that marks if they have been forgotten or not, instead they are
> >> just forgotten via `mem::forget`.
> >
> > The code LGTM, so:
> >
> > Reviewed-by: Gary Guo <gary@garyguo.net>
> >
> > Although this will cause the new expansion we have to be no longer
> > compatible with a totally-proc-macro impl, if we want to do everything
> > in proc macro in the future.
> >
> > If we have the paste macro upstream (
> > https://github.com/nbdd0121/linux/commit/fff00461b0be7fd3ec218dcc428f25886b5ec04a
> > ) then we can replace the `guard` with `paste!([<$field>])` and keep
> > the expansion identical.
> >
>
> I tried it and it seems to work, but I am not sure why the hygiene is
> set correctly. Could you maybe explain why this works?
> ```
> $crate::__internal::paste!{
> let [<$field>] = unsafe {
> $crate::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
> };
> $crate::__init_internal!(init_slot($use_data):
> @data($data),
> @slot($slot),
> @guards([<$field>], $($guards,)*),
> @munch_fields($($rest)*),
> );
> }
> ```
>
> i.e. why can't a user access the guard? I think it is because the hygiene of the `[<>]`
> is used, but not sure why that works.
Yes, by default the hygiene of pasted macro is that of the group,
unless explicitly overriden.
Best,
Gary
next prev parent reply other threads:[~2023-06-28 16:49 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-24 9:24 [PATCH 1/7] rust: init: consolidate init macros Benno Lossin
2023-06-24 9:25 ` [PATCH 2/7] rust: add derive macro for `Zeroable` Benno Lossin
2023-06-24 14:55 ` Björn Roy Baron
2023-06-25 20:46 ` Gary Guo
2023-07-03 11:50 ` Alice Ryhl
2023-06-24 9:25 ` [PATCH 3/7] rust: init: make guards in the init macros hygienic Benno Lossin
2023-06-24 14:58 ` Björn Roy Baron
2023-06-25 20:54 ` Gary Guo
2023-06-28 11:41 ` Benno Lossin
2023-06-28 16:48 ` Gary Guo [this message]
2023-06-24 9:25 ` [PATCH 4/7] rust: init: wrap type checking struct initializers in a closure Benno Lossin
2023-06-24 15:03 ` Björn Roy Baron
2023-06-24 21:05 ` Benno Lossin
2023-06-24 9:25 ` [PATCH 5/7] rust: init: add `..Zeroable::zeroed()` syntax for zeroing all missing fields Benno Lossin
2023-06-24 15:11 ` Björn Roy Baron
2023-06-24 21:14 ` Benno Lossin
2023-06-25 12:56 ` Björn Roy Baron
2023-06-25 13:07 ` Benno Lossin
2023-06-25 14:17 ` Björn Roy Baron
2023-06-25 16:46 ` Benno Lossin
2023-07-03 11:58 ` Alice Ryhl
2023-07-03 18:15 ` Boqun Feng
2023-07-05 17:48 ` Gary Guo
2023-07-05 21:44 ` Benno Lossin
2023-06-24 9:25 ` [PATCH 6/7] rust: init: Add functions to create array initializers Benno Lossin
2023-06-24 15:17 ` Björn Roy Baron
2023-07-03 12:03 ` Alice Ryhl
2023-06-24 9:25 ` [PATCH 7/7] rust: init: add support for arbitrary paths in init macros Benno Lossin
2023-06-24 15:20 ` Björn Roy Baron
2023-06-25 21:01 ` Gary Guo
2023-06-28 11:26 ` Benno Lossin
2023-06-28 17:13 ` Gary Guo
2023-06-24 14:49 ` [PATCH 1/7] rust: init: consolidate " Björn Roy Baron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230628174807.5eabf3bd.gary@garyguo.net \
--to=gary@garyguo.net \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=lina@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=nmi@metaspace.dk \
--cc=ojeda@kernel.org \
--cc=patches@lists.linux.dev \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).