From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Joel Becker" <jlbec@evilplan.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
"Waiman Long" <longman@redhat.com>,
"Fiona Behrens" <me@kloenk.dev>,
"Charalampos Mitrodimas" <charmitro@posteo.net>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Breno Leitao" <leitao@debian.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v6 1/3] rust: configfs: introduce rust support for configfs
Date: Thu, 01 May 2025 21:51:29 +0200 [thread overview]
Message-ID: <87bjsc154u.fsf@kernel.org> (raw)
In-Reply-To: <CANiq72miL3eZ40ujmA-pXCqMS8y2AzJQ1UKpL1_hX03AJ0fteQ@mail.gmail.com> (Miguel Ojeda's message of "Thu, 01 May 2025 21:26:24 +0200")
"Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com> writes:
> On Thu, May 1, 2025 at 8:11 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>>
>> But why does that matter? Anything in the commit message after the cut
>> is dropped when applying the patch, right?
>
> Yes, but it is not common to add a newline there. I mentioned it
> because it looked odd, no worries.
>
>> I might not have the full picture, but it is my understanding that
>> while `const fn` are evaluated in const context when called from const
>> context, they _may_ be called from non-const context, and then they are
>> evaluated in non-const context if their arguments are not const [1].
>> They are not guaranteed to be evaluated in const context.
>>
>> So my thinking is that down the road, refactoring of this code may cause
>> the `AttributeList::add` to be called in a way so that it is not
>> evaluated in const context, and then the `assert` would be evaluated at
>> run time. With `build_error` we would get an error during build.
>
> No, it will always be evaluated at compile-time (if it is evaluated at all).
>
> Please see again the links I provided. There is no `const fn` in the
> example I provided, and yet it is a build error.
>
> From your link, it is clear `const` blocks are a const context, which
> are always evaluated at compile time:
>
> Certain forms of expressions, called constant expressions, can be
> evaluated at compile time.
>
> In const contexts, these are the only allowed expressions, and are
> always evaluated at compile time.
>
> A const context is one of the following: (...) A const block
>
> And from mine, it mentions that it is guaranteed to be evaluated if
> execution reaches that point and that such evaluation would happen at
> compile time:
>
> A const block is a variant of a block expression whose body
> evaluates at compile-time instead of at runtime.
>
> If the const block expression is executed at runtime, then the
> constant is guaranteed to be evaluated, even if its return value is
> ignored:
>
> fn foo<T>() -> usize {
> // If this code ever gets executed, then the assertion has
> // definitely been evaluated at compile-time.
> const { assert!(std::mem::size_of::<T>() > 0); }
> // Here we can have unsafe code relying on the type being
> // non-zero-sized.
> /* ... */
> 42
> }
>
> That example they give is precisely about using a `const` block for
> guaranteeing something that unsafe code relies on.
>
> I hope that helps.
Ah - I understand. I missed that you wrapped the assert in a const
block, a quite important bit 🤦 Thanks for explaining!
Best regards,
Andreas Hindborg
next prev parent reply other threads:[~2025-05-01 19:51 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-01 10:14 [PATCH v6 0/3] rust: configfs abstractions Andreas Hindborg
2025-05-01 10:14 ` [PATCH v6 1/3] rust: configfs: introduce rust support for configfs Andreas Hindborg
2025-05-01 10:52 ` Miguel Ojeda
2025-05-01 11:31 ` Andreas Hindborg
2025-05-01 14:08 ` Miguel Ojeda
2025-05-01 18:11 ` Andreas Hindborg
2025-05-01 19:26 ` Miguel Ojeda
2025-05-01 19:51 ` Andreas Hindborg [this message]
2025-05-01 21:28 ` Miguel Ojeda
2025-05-01 21:46 ` Miguel Ojeda
2025-05-02 6:57 ` Andreas Hindborg
2025-05-03 11:18 ` Miguel Ojeda
2025-05-05 7:50 ` Andreas Hindborg
2025-05-06 11:18 ` Miguel Ojeda
2025-05-06 11:31 ` Andreas Hindborg
2025-05-06 11:51 ` Andreas Hindborg
2025-05-01 10:14 ` [PATCH v6 2/3] rust: configfs: add a sample demonstrating configfs usage Andreas Hindborg
2025-05-02 7:27 ` Charalampos Mitrodimas
2025-05-02 12:05 ` Andreas Hindborg
2025-05-01 10:14 ` [PATCH v6 3/3] MAINTAINERS: add configfs Rust abstractions Andreas Hindborg
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=87bjsc154u.fsf@kernel.org \
--to=a.hindborg@kernel.org \
--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=charmitro@posteo.net \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=jlbec@evilplan.org \
--cc=leitao@debian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=me@kloenk.dev \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=mingo@redhat.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=will@kernel.org \
/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).