From: David Gow <david@davidgow.net>
To: "Gary Guo" <gary@garyguo.net>,
"Nicolás Antinori" <nico.antinori.7@gmail.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Burak Emir" <burak.emir@gmail.com>,
"Brendan Higgins" <brendan.higgins@linux.dev>,
"Miguel Ojeda" <ojeda@kernel.org>
Cc: "Alexandre Courbot" <acourbot@nvidia.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Benno Lossin" <lossin@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Boqun Feng" <boqun@kernel.org>,
"Brigham Campbell" <me@brighamcampbell.com>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Danilo Krummrich" <dakr@kernel.org>,
"Jori Koolstra" <jkoolstra@xs4all.nl>,
"Onur Özkan" <work@onurozkan.dev>,
"Rae Moar" <raemoar63@gmail.com>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Tamir Duberstein" <tamird@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Yury Norov" <yury.norov@gmail.com>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-kernel-mentees@lists.linux.dev
Subject: Re: [PATCH RFC 0/3] rust: kunit: #[should_panic] and same test name with different #[cfg(...)] support
Date: Tue, 22 Sep 2026 23:53:36 +0800 [thread overview]
Message-ID: <cfed2509-a365-4040-8079-484750ad3cdf@davidgow.net> (raw)
In-Reply-To: <DLLYGLVEA0R3.3D1733XFFTFPV@garyguo.net>
Le 22/09/2026 à 23:37, Gary Guo a écrit :
> On Tue Sep 22, 2026 at 4:27 PM BST, David Gow wrote:
>> Le 22/09/2026 à 21:35, Gary Guo a écrit :
>>> On Tue Sep 22, 2026 at 8:56 AM BST, David Gow wrote:
>>>> Le 16/09/2026 à 03:33, Nicolás Antinori a écrit :
>>>>> - Is it ok to 'stringify' the configuration so it can be distinguished
>>>>> in the report? Would you prefer something like `_case_1` `_case_2` ..
>>>>> instead?
>>>
>>> I also don't like the stringifcation of cfgs.
>>>
>>>>
>>>> I don't _like_ this: my preference would be for us to keep the same
>>>> name, and just not emit a test_case for anything which should be
>>>> compiled out with cfg. Unfortunately, implementing that is a bit harder
>>>> than would be ideal: we need a way of evaluating the cfg() arguments in
>>>> a proc macro, I think. (Ultimately, because otherwise there's no way of
>>>> statically determining the length of the TEST_CASES array?)
>>>
>>> This is possible with a trick. In pin-init we have a similar need, so what I do
>>> is for
>>>
>>> #[macro]
>>> struct Foo {
>>> #[cfg(a)]
>>> bar: u32,
>>> }
>>>
>>> to be expanded to
>>>
>>> #[cfg(a)]
>>> #[macro]
>>> struct Foo {
>>> bar: u32
>>> }
>>>
>>> #[cfg(not(a))]
>>> #[macro]
>>> struct Foo {
>>> }
>>>
>>> However, for kunit I don't think that's needed. Deduplicating the names
>>> should be sufficient?
>>>
>> The problem with (at least my naive implementation of) duplication is
>> that -- while it works great for switching between implementations -- it
>> doesn't handle the case where _no_ implementation is active.
>>
>> (The current implementation just compiles to a skipped test if the
>> #[cfg(...)] isn't active, which sidesteps the problem until we have
>> multiple implementations...)
>>
>> Even the expansion above could be problematic, as we really are trying
>> to add entries to a static array, so I don't know what we could put in
>> the not(a) case (particularly since there'd be potentially lots of them).
>>
>> Maybe the trick is to generate the array size by using a big series of
>> something like:
>> static mut TEST_CASES: [...,
>> #[cfg(a)] 1
>> #[cfg(not(a))]0
>> +
>> #[cfg(b)] 1
>> #[cfg(not(b))]0
>> +
>> …] = {
>> #[cfg(a)] case1,
>> #[cfg(b)] case1,
>> …
>> }
>> }
>>
>
> For array sizes, you have the option of building a slice first.
>
> Some thing like:
>
> const TEST_CASES_UNIT: &[()] = [
> #[cfg(a)] (),
> #[cfg(b)] (),
> ];
>
> static mut TEST_CASES: [...; TEST_CASES_SLICE.len()] = [...];
>
Neat: I hadn't thought of that, and it seems to work great.
> You could also just build everything as a const slice of `&'static
> [kunit_cases]`, if there is no need to make it `mut`. But I suppose it needs to
> be `static mut` for some reason?
>
Yeah, the kunit_cases are modified at runtime to store the result, so
this really does need to be `static mut`. And while these writes are all
done behind the scenes from C, so they should _appear_ constant from
Rust (modulo a couple of writes to status we can get rid of once we fix
the cfg() stuff here), we still need to ensure they can't end up in
read-only memory.
Cheers,
-- David
next prev parent reply other threads:[~2026-09-22 15:53 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-15 19:33 Nicolás Antinori
2026-09-15 19:33 ` [PATCH RFC 1/3] rust: kunit: add #[should_panic] support Nicolás Antinori
2026-09-15 19:33 ` [PATCH RFC 2/3] rust: kunit: allow same test name with different #[cfg(...)] Nicolás Antinori
2026-09-15 19:33 ` [PATCH RFC 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case Nicolás Antinori
2026-09-22 7:56 ` [PATCH RFC 0/3] rust: kunit: #[should_panic] and same test name with different #[cfg(...)] support David Gow
2026-09-22 13:35 ` Gary Guo
2026-09-22 15:27 ` David Gow
2026-09-22 15:37 ` Gary Guo
2026-09-22 15:53 ` David Gow [this message]
2026-09-23 15:04 ` Nicolás Antinori
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=cfed2509-a365-4040-8079-484750ad3cdf@davidgow.net \
--to=david@davidgow.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brendan.higgins@linux.dev \
--cc=burak.emir@gmail.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=jkoolstra@xs4all.nl \
--cc=linux-kernel-mentees@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=me@brighamcampbell.com \
--cc=nico.antinori.7@gmail.com \
--cc=ojeda@kernel.org \
--cc=raemoar63@gmail.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
--cc=yury.norov@gmail.com \
/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
all inboxes | Powered by JetHome®