linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Schrefl <chrisi.schrefl@gmail.com>
To: "Benno Lossin" <lossin@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>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] rust: add `assert_sync` function
Date: Sat, 7 Jun 2025 17:54:52 +0200	[thread overview]
Message-ID: <2531463d-fa6e-430c-a3a6-b179654cfbbe@gmail.com> (raw)
In-Reply-To: <DAGEZCRR61A0.30H1MJQXW4CV5@kernel.org>

On 07.06.25 5:42 PM, Benno Lossin wrote:
> On Sat Jun 7, 2025 at 3:02 PM CEST, Christian Schrefl wrote:
>> Adds a new file `compile_assert.rs` for asserts during compile time and
>> add the `assert_sync` function to this file.
>>
>> This will be used in `miscdevice` to avoid regression in case a `: Send`
>> bound falsely gets dropped in the future.
>>
>> Suggested-by: Benno Lossin <lossin@kernel.org>
>> Link: https://lore.kernel.org/rust-for-linux/20250530-b4-rust_miscdevice_registrationdata-v4-0-d313aafd7e59@gmail.com/T/#mdf3328834ce1d136daf836c9e089b5a8627a6d53
>> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com>
> 
> Thanks!
> 
>> ---
>> For now I've only added the function.
>>
>> Some things that might make sense to do as well:
>> - Move `static_assert` into `compile_assert.rs`.
> 
> Sounds reasonable.
> 
>> - Add `assert_sync` to prelude.
> 
> I don't think we need to do that. At least not yet.

Alright

>> - Add `assert_send` as well.
> 
> Sounds like a good idea.

Should I already add this in V2 for this series?

> 
>> - Use these asserts in various places around the kernel. (I'm not sure
>> where it would make sense)
>> ---
>>  rust/kernel/compile_assert.rs | 24 ++++++++++++++++++++++++
>>  rust/kernel/lib.rs            |  1 +
>>  2 files changed, 25 insertions(+)
>>
>> diff --git a/rust/kernel/compile_assert.rs b/rust/kernel/compile_assert.rs
>> new file mode 100644
>> index 0000000000000000000000000000000000000000..2a99de1ba919dc3952d7a1585869567a44106b44
>> --- /dev/null
>> +++ b/rust/kernel/compile_assert.rs
>> @@ -0,0 +1,24 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +//! Compile-time asserts.
>> +
>> +/// Asserts that the given type is [`Sync`]. This check is done at compile time and does nothing
>> +/// at runtime.
>> +///
>> +/// Note that this is only intended to avoid regressions and for sanity checks.
>> +///
>> +/// # Examples
>> +/// ```
>> +/// # use kernel::compile_assert::assert_sync;
>> +/// # use kernel::types::NotThreadSafe;
>> +///
>> +///
>> +/// // Do the assertion in a const block to make sure it won't be executed at runtime.
>> +/// const _:() = {
> 
> s/_:()/_: ()/

Fixed.

> 
>> +///     assert_sync::<i32>(); // Succeeds because `i32` is Sync
>> +///     // assert_sync::<NotThreadSafe>(); // Fails because `NotThreadSafe` is not `Sync`.
> 
> Can you split this into two examples and mark the failing one with
> `compile_fail`?

I've tried it with `compile_fail` and it didn't work, I think
that's not supported in (kernel) doc tests yet. 

> 
> We also could provide a macro similar to [1].
>
> [1]: https://docs.rs/static_assertions/latest/static_assertions/

You mean the `assert_impl_*!` macros?

That might make sense, with macros we would not need to write
a const block to ensure its not executed at runtime (although
it's probably optimized out anyways). It would also mean that
we won't need a assert for every Trait, which seems nice.
So a macro sounds pretty good to me.

Cheers
Christian


  reply	other threads:[~2025-06-07 15:54 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-07 13:02 [PATCH] rust: add `assert_sync` function Christian Schrefl
2025-06-07 15:42 ` Benno Lossin
2025-06-07 15:54   ` Christian Schrefl [this message]
2025-06-07 17:30     ` Miguel Ojeda
2025-06-07 18:11     ` Benno Lossin
2025-06-07 19:20       ` Christian Schrefl
2025-06-07 22:31         ` Benno Lossin
2025-06-07 23:38           ` Christian Schrefl
2025-06-08  7:41             ` Benno Lossin
2025-06-07 17:29 ` Miguel Ojeda
2025-06-07 20:19   ` Christian Schrefl

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=2531463d-fa6e-430c-a3a6-b179654cfbbe@gmail.com \
    --to=chrisi.schrefl@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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).