Messages in this thread | | | Date | Wed, 28 Jun 2023 17:48:07 +0100 | From | Gary Guo <> | Subject | Re: [PATCH 3/7] rust: init: make guards in the init macros hygienic |
| |
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
| |