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 21:20:45 +0200 [thread overview]
Message-ID: <407f04ff-b313-4629-bbdd-f25df14f44da@gmail.com> (raw)
In-Reply-To: <DAGI57J7WBD0.2BOT553TRIXH8@kernel.org>
On 07.06.25 8:11 PM, Benno Lossin wrote:
> On Sat Jun 7, 2025 at 5:54 PM CEST, Christian Schrefl wrote:
>> On 07.06.25 5:42 PM, Benno Lossin wrote:
>>> On Sat Jun 7, 2025 at 3:02 PM CEST, Christian Schrefl wrote:
>>>> - Add `assert_send` as well.
>>>
>>> Sounds like a good idea.
>>
>> Should I already add this in V2 for this series?
>
> If you want to then sure, but we can also wait until we have a use-case.
> Also, let's finish the discussion about the macro idea below.
>
>>>> +/// 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.
>
> Hmm, I thought that this worked... @Miguel any idea?
>
>>> We also could provide a macro similar to [1].
>>>
>>> [1]: https://docs.rs/static_assertions/latest/static_assertions/
>>
>> You mean the `assert_impl_*!` macros?
>
> Yes, but the others might also be useful from time to time.
> >> 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 100% will be optimized out.
>
>> 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.
>
> It depends, the macro impl needs to define its own function, which might
> be inefficient if one uses it a lot. But there is no way to be generic
> over traits, so there is no other way.
>
> Let's see what the others think.
The error messages in the macro are slightly worse:
error[E0277]: `*mut ()` cannot be shared between threads safely
--> rust/kernel/compile_assert.rs:40:18
|
40 | assert_impl_all!(NotThreadSafe: Sync); // Fails because `NotThreadSafe` is not `Sync`
| ^^^^^^^^^^^^^ `*mut ()` cannot be shared between threads safely
|
= help: within `PhantomData<*mut ()>`, the trait `Sync` is not implemented for `*mut ()`, which is required by `PhantomData<*mut ()>: Sync`
note: required because it appears within the type `PhantomData<*mut ()>`
--> /home/chrisi/.rustup/toolchains/1.78-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/marker.rs:740:12
|
740 | pub struct PhantomData<T: ?Sized>;
| ^^^^^^^^^^^
note: required by a bound in `assert_impl`
--> rust/kernel/compile_assert.rs:34:48
|
34 | const fn assert_impl<T: ?Sized $(+ $trait)+>() {}
| ^^^^^^ required by this bound in `assert_impl`
...
40 | assert_impl_all!(NotThreadSafe: Sync); // Fails because `NotThreadSafe` is not `Sync`
| ------------------------------------- in this macro invocation
= note: this error originates in the macro `assert_impl_all` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 1 previous error
compared to the function:
error[E0277]: `*mut ()` cannot be shared between threads safely
--> rust/kernel/compile_assert.rs:28:31
|
28 | const _: () = { assert_sync::<NotThreadSafe>() };
| ^^^^^^^^^^^^^ `*mut ()` cannot be shared between threads safely
|
= help: within `PhantomData<*mut ()>`, the trait `Sync` is not implemented for `*mut ()`, which is required by `PhantomData<*mut ()>: Sync`
note: required because it appears within the type `PhantomData<*mut ()>`
--> /home/chrisi/.rustup/toolchains/1.78-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/marker.rs:740:12
|
740 | pub struct PhantomData<T: ?Sized>;
| ^^^^^^^^^^^
note: required by a bound in `assert_sync`
--> rust/kernel/compile_assert.rs:26:38
|
26 | pub const fn assert_sync<T: ?Sized + Sync>() {}
| ^^^^ required by this bound in `assert_sync`
I guess I'll keep it as a function for now.
Cheers
Christian
next prev parent reply other threads:[~2025-06-07 19:20 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
2025-06-07 17:30 ` Miguel Ojeda
2025-06-07 18:11 ` Benno Lossin
2025-06-07 19:20 ` Christian Schrefl [this message]
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=407f04ff-b313-4629-bbdd-f25df14f44da@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).