mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Gow <david@davidgow.net>
To: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Cc: nico.antinori.7@gmail.com, "Alice Ryhl" <aliceryhl@google.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Brendan Higgins" <brendan.higgins@linux.dev>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Gary Guo" <gary@garyguo.net>, "Miguel Ojeda" <ojeda@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Burak Emir" <burak.emir@gmail.com>,
	"Rae Moar" <raemoar63@gmail.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Yury Norov" <yury.norov@gmail.com>,
	kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v2 1/3] rust: kunit: add #[should_panic] support
Date: Fri, 6 Nov 2026 13:31:31 +0800	[thread overview]
Message-ID: <06e83174-8fe3-4cca-a357-3c351d0873a2@davidgow.net> (raw)
In-Reply-To: <CANiq72mf6Hx1YZDKv0GKwEd2gDfQtUkuA4RGhgUEfOCpR0ixDw@mail.gmail.com>

Le 04/11/2026 à 12:32 AM, Miguel Ojeda a écrit :
> On Tue, Nov 3, 2026 at 3:46 PM David Gow <david@davidgow.net> wrote:
>>
>> I'd rather this not pass, personally. I think that an assert!() within a
>> test should work normally (i.e., it's possible to assert that the test
>> state is normal, and a failing assert will fail the test).
>>
>> I could be talked out of this if it's really unpleasantly different from
>> non-kernel Rust, but in that case I think we'd want to make sure that
>> kunit_assert!() within a test behaves like a normal assert even in a
>> should_panic test.
> 
> Yeah, it all boils down to what we mean by `assert!()`.
> 
> When I implemented it back then, I intended to allow people to write
> the normal asserts they would write (which could help in case we were
> to e.g. import code from elsewhere, like a vendored library), as if
> would have "Rust-panicked", but not "kernel-panicked".
> 
> Viewed in that light, `BUG()` would not necessarily need to make
> `should_panic` succeed or not [*], but an `assert!(false)` would need
> to make the test succeed. And then we could have a
> `should_kernel_panic` and maybe expose a public `kunit_assert!` etc.
> to distinguish them and so on.

I'm pretty convinced that #[should_panic] should fail for anything other
than a deliberate panic!() (including panic!() implemented via an
assertion). Another failure (be it BUG(), an invalid memory access,
etc.) feels like a separate thing. The example I'd give is a test which
is deliberately trying to trigger a panic!() or assert!() to verify
that, e.g., an invalid input/state is caught: if the code tried to
handle the invalid state and crashed, that shouldn't be a pass.


For assert!(false), while I personally don't like that this will cause a
test to pass -- it feels confusing to me -- I'm okay with having this
work for compatibility with upstream Rust.

How about:
- If #[should_panic] is set, we don't override assert!()/assert_eq!():
instead of getting compiled down to a KUnit assertion, they use the core
implementation which will panic!() on failure, and get caught by the
panic_handler. This should match upstream Rust behaviour (because it is
upstream Rust behaviour), and avoid having to plumb a bunch of
should_panic things through the C assert macro implementations.
- If a test really wants a KUnit assertion, the option of using
kernel::kunit_assert!() is there, which will behave like the C
assertions: a failed assertion will cause the test to fail. Of course, I
don't think we need to implement this until someone actually needs it.

> Having said that, I am not sure how many tests would actually rely on
> such differences... I think the core requirement is that, as you say,
> we are able to test for Rust panics in functions we call within the
> test (and ideally also other Rust panics within the test body too,
> when viewed in the light above). But if something simplifies
> implementation significantly, then we can consider other things.

Yeah, I'm hesitant to do something which involves either a lot of
plumbing in C, or a significant difference in behaviour between tests
written in C and Rust unless we've got a real use-case here.

Disabling the assert{,_eq}!() overrides for tests with #[should_panic]
seems to be the appropriate compromise to me, though.

> [*] I asked an LLM to test what happens in userspace Rust when one
> aborts/traps/etc., and it differs in stable between a normal test and
> a doctest (with the default `panic=unwind`): essentially, the former
> dies, the latter succeeds -- it doesn't seem to be well-defined yet.
> There is an issue for the doctest side of it:
> 
>   https://github.com/rust-lang/rust/issues/143009
> 
> and there is a bunch of discussion on the different linked PRs.
> 
Thanks,
-- David

  reply	other threads:[~2026-11-06  5:31 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-29 16:59 [PATCH v2 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
2026-10-29 16:59 ` [PATCH v2 1/3] rust: kunit: add #[should_panic] support Nicolás Antinori via B4 Relay
2026-11-02 14:58   ` Nicolás Antinori
2026-11-02 15:51     ` Miguel Ojeda
2026-11-02 20:53       ` Nicolás Antinori
2026-11-02 21:25         ` Miguel Ojeda
2026-11-03 15:05           ` David Gow
2026-11-03 15:00     ` David Gow
2026-11-03 14:46   ` David Gow
2026-11-03 16:32     ` Miguel Ojeda
2026-11-06  5:31       ` David Gow [this message]
2026-11-04 17:58     ` Nicolás Antinori
2026-11-06  5:31       ` David Gow
2026-10-29 16:59 ` [PATCH v2 2/3] rust: kunit: allow same test name with different #[cfg(...)] Nicolás Antinori via B4 Relay
2026-11-03 14:45   ` David Gow
2026-10-29 16:59 ` [PATCH v2 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case Nicolás Antinori via B4 Relay
2026-11-03 14:46   ` David Gow
2026-11-03 14:45 ` [PATCH v2 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] David Gow
2026-11-05 13:46   ` Nicolás Antinori
2026-11-06  5:31     ` David Gow

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=06e83174-8fe3-4cca-a357-3c351d0873a2@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=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=nico.antinori.7@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=raemoar63@gmail.com \
    --cc=rust-for-linux@vger.kernel.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®