[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <8lCE3SyChVVb2HphigkwKsxv7etgmS0N3AzcDyYtCEoqcFEKvY-5ILkOrWUr_vnWrvsWrAHXVfwcWARfsiMHC8Yc03sND-PuuK-2z9j4z6I=@protonmail.com>
Date: Sun, 25 Jun 2023 14:17:44 +0000
From: Björn Roy Baron <bjorn3_gh@...tonmail.com>
To: Benno Lossin <benno.lossin@...ton.me>
Cc: Miguel Ojeda <ojeda@...nel.org>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Alice Ryhl <aliceryhl@...gle.com>,
Andreas Hindborg <nmi@...aspace.dk>,
rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
patches@...ts.linux.dev, Asahi Lina <lina@...hilina.net>
Subject: Re: [PATCH 5/7] rust: init: add `..Zeroable::zeroed()` syntax for zeroing all missing fields
On Sunday, June 25th, 2023 at 15:07, Benno Lossin <benno.lossin@...ton.me> wrote:
> On 25.06.23 14:56, Björn Roy Baron wrote:
> > On Saturday, June 24th, 2023 at 23:14, Benno Lossin <benno.lossin@...ton.me> wrote:
>
> >>>> + // Ensure that the struct is indeed `Zeroable`.
> >>>> + is_zeroable(slot);
> >>>> + // SAFETY: The type implements `Zeroable` by the check above.
> >>>> + unsafe { ::core::ptr::write_bytes(slot, 0, 1) };
> >>>> + $init_zeroed // this will be `()` if set.
> >>>
> >>> How does this work? Shouldn't there be a ; after $init_zeroed to consume the () value?
> >>
> >> It is the last expression of a block and since it is `()` it is ok
> >> (adding a ; would also be ok, but it is not necessary).
> >
> > I'm surprised it is considered the last expression of a block. Unlike with {} using $()? will still
> > allow variables defined inside this as if they were outside of it. Also I can't reproduce this
> > behavior with:
> >
> > macro_rules! foo {
> > ($($a:expr)?) => {
> > $($a)?
> > bar();
> > }
> > }
> >
> > fn main() {
> > foo!(());
> > }
> >
> > Is there something I'm missing?
> >
> > Cheers,
> > Björn
>
> Not sure what you mean with "allow variables defined inside this
> as if they were outside of it". But note that in the macro `$init_zeroed`
> is the last expression of a block. Here is a small example:
$(let $this = unsafe { ::core::ptr::NonNull::new_unchecked(slot) };)? comes after
this code in the same block that contains struct __InitOk;. And after that another
$crate::__init_internal!() invocation. That is why I don't get that this is allowed
at all.
>
> ```
> macro_rules! foo {
> ($($a:expr)?) => {{
> $(
> bar();
> $a
> )?
> }};
> }
>
> fn bar() {}
>
> fn main() {
> foo!(());
> foo!();
> }
> ```
>
> it expands to this:
> ```
> fn main() {
> {
> bar();
> ()
> };
> {};
> }
> ```
>
> --
> Cheers,
> Benno
>
Powered by blists - more mailing lists