mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "David Gow" <david@davidgow.net>, "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 16:37:35 +0100	[thread overview]
Message-ID: <DLLYGLVEA0R3.3D1733XFFTFPV@garyguo.net> (raw)
In-Reply-To: <d00d6512-ec95-4337-ad62-a515b507862b@davidgow.net>

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()] = [...];

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?

Best,
Gary


> Then, as long as there's only one or zero active configurations, the 
> array size should match, and any duplicates will be caught by having 
> multiple definitions of case1.
>
> I assume the compiler would be able to reduce that down to a 
> compile-time integer, even if it is extremely ugly...
>
> -- David



  reply	other threads:[~2026-09-22 15:37 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 [this message]
2026-09-22 15:53         ` David Gow
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=DLLYGLVEA0R3.3D1733XFFTFPV@garyguo.net \
    --to=gary@garyguo.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=david@davidgow.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®