[PATCH v2 0/3] rust: doctest: unify with other kunit tests

Gary Guo posted 3 patches 3 weeks, 2 days ago
Documentation/rust/general-information.rst |  4 +-
init/Kconfig                               |  3 --
rust/Makefile                              |  4 +-
rust/kernel/kunit.rs                       | 11 +++--
rust/kernel/str.rs                         |  2 +-
rust/macros/helpers.rs                     | 16 -------
rust/macros/kunit.rs                       |  5 +-
rust/macros/lib.rs                         |  5 --
scripts/rustdoc_test_gen.rs                | 73 ++++++++----------------------
9 files changed, 30 insertions(+), 93 deletions(-)
[PATCH v2 0/3] rust: doctest: unify with other kunit tests
Posted by Gary Guo 3 weeks, 2 days ago
Currently in Rust, kunit tests are handled with `kunit_tests`, while the
doctests generate a lot `extern "C"` functions and invoke them through a
generated C wrapper.

This is currently needed because of the special treatment of file name and
line numbers from doctests. Convert them to override `file!()` and
`line!()` macro instead, and use them inside `kunit_assert!`.

With this it is possible to just generate `#[kunit_tests]` and `#[test]`
for doctests.

David mentioned in [1] that this should route via the Rust tree.

Link: https://lore.kernel.org/rust-for-linux/e30d16ec-f5a3-4cd8-9b25-96e0dfa8b9d2@davidgow.net/ [1]

Signed-off-by: Gary Guo <gary@garyguo.net>
---
Changes in v2:
- Rebased on v7.3-rc1
- Picked up tags
- Link to v1: https://patch.msgid.link/20260616-kunit-v1-0-b191604a8d6b@garyguo.net

---
Gary Guo (3):
      rust: kunit: use `file!()` inside `kunit_assert!`
      rust: kunit: use `line!()` inside `kunit_assert!`
      rust: doctest: generate Rust kunit test suites

 Documentation/rust/general-information.rst |  4 +-
 init/Kconfig                               |  3 --
 rust/Makefile                              |  4 +-
 rust/kernel/kunit.rs                       | 11 +++--
 rust/kernel/str.rs                         |  2 +-
 rust/macros/helpers.rs                     | 16 -------
 rust/macros/kunit.rs                       |  5 +-
 rust/macros/lib.rs                         |  5 --
 scripts/rustdoc_test_gen.rs                | 73 ++++++++----------------------
 9 files changed, 30 insertions(+), 93 deletions(-)
---
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260616-kunit-348f2c029b1e

Best regards,
--  
Gary Guo <gary@garyguo.net>
Re: [PATCH v2 0/3] rust: doctest: unify with other kunit tests
Posted by David Gow 3 weeks, 1 day ago
Le 03/09/2026 à 5:59 AM, Gary Guo a écrit :
> Currently in Rust, kunit tests are handled with `kunit_tests`, while the
> doctests generate a lot `extern "C"` functions and invoke them through a
> generated C wrapper.
> 
> This is currently needed because of the special treatment of file name and
> line numbers from doctests. Convert them to override `file!()` and
> `line!()` macro instead, and use them inside `kunit_assert!`.
> 
> With this it is possible to just generate `#[kunit_tests]` and `#[test]`
> for doctests.
> 
> David mentioned in [1] that this should route via the Rust tree.
> 
> Link: https://lore.kernel.org/rust-for-linux/e30d16ec-f5a3-4cd8-9b25-96e0dfa8b9d2@davidgow.net/ [1]
> 
> Signed-off-by: Gary Guo <gary@garyguo.net>
> ---
> Changes in v2:
> - Rebased on v7.3-rc1
> - Picked up tags
> - Link to v1: https://patch.msgid.link/20260616-kunit-v1-0-b191604a8d6b@garyguo.net
> 
> ---

Thanks again. This still looks good to me.

Cheers,
-- David

> Gary Guo (3):
>       rust: kunit: use `file!()` inside `kunit_assert!`
>       rust: kunit: use `line!()` inside `kunit_assert!`
>       rust: doctest: generate Rust kunit test suites
> 
>  Documentation/rust/general-information.rst |  4 +-
>  init/Kconfig                               |  3 --
>  rust/Makefile                              |  4 +-
>  rust/kernel/kunit.rs                       | 11 +++--
>  rust/kernel/str.rs                         |  2 +-
>  rust/macros/helpers.rs                     | 16 -------
>  rust/macros/kunit.rs                       |  5 +-
>  rust/macros/lib.rs                         |  5 --
>  scripts/rustdoc_test_gen.rs                | 73 ++++++++----------------------
>  9 files changed, 30 insertions(+), 93 deletions(-)
> ---
> base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
> change-id: 20260616-kunit-348f2c029b1e
> 
> Best regards,
> --  
> Gary Guo <gary@garyguo.net>
>