public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: ptr: replace unneeded use of `build_assert`
@ 2025-12-16  8:24 Alexandre Courbot
  2025-12-17 13:22 ` Alice Ryhl
  2026-01-19  8:20 ` Miguel Ojeda
  0 siblings, 2 replies; 7+ messages in thread
From: Alexandre Courbot @ 2025-12-16  8:24 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich
  Cc: rust-for-linux, linux-kernel, Alexandre Courbot

Since `ALIGN` is a const parameter, this assertion can be done in const
context using the `assert!` macro.

Suggested-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
Result of a quick discussion about build_assert with Alice at LPC. :)
Thanks for pointing this out.
---
 rust/kernel/ptr.rs | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/rust/kernel/ptr.rs b/rust/kernel/ptr.rs
index e3893ed04049..5b6a382637fe 100644
--- a/rust/kernel/ptr.rs
+++ b/rust/kernel/ptr.rs
@@ -5,8 +5,6 @@
 use core::mem::align_of;
 use core::num::NonZero;
 
-use crate::build_assert;
-
 /// Type representing an alignment, which is always a power of two.
 ///
 /// It is used to validate that a given value is a valid alignment, and to perform masking and
@@ -40,10 +38,12 @@ impl Alignment {
     /// ```
     #[inline(always)]
     pub const fn new<const ALIGN: usize>() -> Self {
-        build_assert!(
-            ALIGN.is_power_of_two(),
-            "Provided alignment is not a power of two."
-        );
+        const {
+            assert!(
+                ALIGN.is_power_of_two(),
+                "Provided alignment is not a power of two."
+            );
+        }
 
         // INVARIANT: `align` is a power of two.
         // SAFETY: `align` is a power of two, and thus non-zero.

---
base-commit: 60c7398bded2e11f0db40a409a241b8be5910ee2
change-id: 20251216-ptr_assert-3d9990798dd7

Best regards,
-- 
Alexandre Courbot <acourbot@nvidia.com>


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] rust: ptr: replace unneeded use of `build_assert`
  2025-12-16  8:24 [PATCH] rust: ptr: replace unneeded use of `build_assert` Alexandre Courbot
@ 2025-12-17 13:22 ` Alice Ryhl
  2026-01-17  3:16   ` Alexandre Courbot
  2026-01-19  8:20 ` Miguel Ojeda
  1 sibling, 1 reply; 7+ messages in thread
From: Alice Ryhl @ 2025-12-17 13:22 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
	rust-for-linux, linux-kernel

On Tue, Dec 16, 2025 at 9:25 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> Since `ALIGN` is a const parameter, this assertion can be done in const
> context using the `assert!` macro.
>
> Suggested-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] rust: ptr: replace unneeded use of `build_assert`
  2025-12-17 13:22 ` Alice Ryhl
@ 2026-01-17  3:16   ` Alexandre Courbot
  2026-01-17 14:30     ` Miguel Ojeda
  0 siblings, 1 reply; 7+ messages in thread
From: Alexandre Courbot @ 2026-01-17  3:16 UTC (permalink / raw)
  To: Alice Ryhl, Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Trevor Gross, Danilo Krummrich, rust-for-linux,
	linux-kernel

On Wed Dec 17, 2025 at 10:22 PM JST, Alice Ryhl wrote:
> On Tue, Dec 16, 2025 at 9:25 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>>
>> Since `ALIGN` is a const parameter, this assertion can be done in const
>> context using the `assert!` macro.
>>
>> Suggested-by: Alice Ryhl <aliceryhl@google.com>
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>

Miguel, are you comfortable taking this through the Rust tree for this
cycle?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] rust: ptr: replace unneeded use of `build_assert`
  2026-01-17  3:16   ` Alexandre Courbot
@ 2026-01-17 14:30     ` Miguel Ojeda
  2026-01-17 16:48       ` Gary Guo
  0 siblings, 1 reply; 7+ messages in thread
From: Miguel Ojeda @ 2026-01-17 14:30 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich, rust-for-linux, linux-kernel

On Sat, Jan 17, 2026 at 4:17 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>rtable taking this through the Rust tree for this
> cycle?

That file is maintained in the Rust tree, so it should be taken
through the Rust tree barring other reasons, yeah. I didn't start
applying yet for -next.

(By the way, we have `static_assert!` for this -- we should probably
discuss if we want to start using `const { ... }` more instead.)

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] rust: ptr: replace unneeded use of `build_assert`
  2026-01-17 14:30     ` Miguel Ojeda
@ 2026-01-17 16:48       ` Gary Guo
  2026-01-17 19:16         ` Miguel Ojeda
  0 siblings, 1 reply; 7+ messages in thread
From: Gary Guo @ 2026-01-17 16:48 UTC (permalink / raw)
  To: Miguel Ojeda, Alexandre Courbot
  Cc: Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich, rust-for-linux, linux-kernel

On Sat Jan 17, 2026 at 2:30 PM GMT, Miguel Ojeda wrote:
> On Sat, Jan 17, 2026 at 4:17 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>>rtable taking this through the Rust tree for this
>> cycle?
>
> That file is maintained in the Rust tree, so it should be taken
> through the Rust tree barring other reasons, yeah. I didn't start
> applying yet for -next.
>
> (By the way, we have `static_assert!` for this -- we should probably
> discuss if we want to start using `const { ... }` more instead.)

static_assert! creates an item and thus cannot refer to generic parameters.

Best,
Gary

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] rust: ptr: replace unneeded use of `build_assert`
  2026-01-17 16:48       ` Gary Guo
@ 2026-01-17 19:16         ` Miguel Ojeda
  0 siblings, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2026-01-17 19:16 UTC (permalink / raw)
  To: Gary Guo
  Cc: Alexandre Courbot, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich, rust-for-linux, linux-kernel

On Sat, Jan 17, 2026 at 5:49 PM Gary Guo <gary@garyguo.net> wrote:
>
> static_assert! creates an item and thus cannot refer to generic parameters.

Ah, right, thanks Gary -- you were the one removing that limitation
from const blocks years ago!

Since we are here, I wondered about whether there was progress on
allowing top-level inline const so that we could perhaps use that for
a macro that can do both (or just use the inline const syntax
everywhere, but `rustfmt` may employ three lines...), i.e. this one I
had linked in our usual list:

    https://github.com/rust-lang/lang-team/issues/251

But I see someone recently created a tracking issue and even an
implementation PR (without commenting in the above issue, which makes
it hard to notice). It actually seems close to landing:

    https://github.com/rust-lang/rust/issues/149226
    https://github.com/rust-lang/rust/pull/149174

There is/was an early experiment on omitting `: ()` on const items as well:

    https://github.com/rust-lang/rust/pull/149738

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] rust: ptr: replace unneeded use of `build_assert`
  2025-12-16  8:24 [PATCH] rust: ptr: replace unneeded use of `build_assert` Alexandre Courbot
  2025-12-17 13:22 ` Alice Ryhl
@ 2026-01-19  8:20 ` Miguel Ojeda
  1 sibling, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2026-01-19  8:20 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, rust-for-linux, linux-kernel

On Tue, Dec 16, 2025 at 9:25 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> Since `ALIGN` is a const parameter, this assertion can be done in const
> context using the `assert!` macro.
>
> Suggested-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>

Applied to `rust-next` -- thanks everyone!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-01-19  8:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-16  8:24 [PATCH] rust: ptr: replace unneeded use of `build_assert` Alexandre Courbot
2025-12-17 13:22 ` Alice Ryhl
2026-01-17  3:16   ` Alexandre Courbot
2026-01-17 14:30     ` Miguel Ojeda
2026-01-17 16:48       ` Gary Guo
2026-01-17 19:16         ` Miguel Ojeda
2026-01-19  8:20 ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox