From c82c75ae11fae66cf070562f9b5241a4663f30cb Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Thu, 24 Sep 2026 01:50:51 +0100 Subject: rust: doctest: trim function name for reproducibility Currently rustdoc will generate function names like "_doctest_main__home_gary_Projects_linux_rust_kernel_io_rs_824_0" for a doctest located at rust/kernel/io.rs:824, when building with separate outdir using `O=`. This creates overlong symbol names and is also not reproducible. [ `rustdoc` already remaps these paths when we use Rust >= 1.95, thus this fix is needed for older toolchains. - Miguel ] Fix it by doing a custom remapping to trim it to something like `_doctest_main_rust_kernel_io_rs_824_0`. Signed-off-by: Gary Guo Link: https://patch.msgid.link/20260924005052.2805129-1-gary@kernel.org [ Added `TODO` comment about the minimum version bump. Reworded title and comment for typos. - Miguel ] Signed-off-by: Miguel Ojeda --- scripts/rustdoc_test_builder.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'scripts') diff --git a/scripts/rustdoc_test_builder.rs b/scripts/rustdoc_test_builder.rs index df864437cef7..0361e22853f2 100644 --- a/scripts/rustdoc_test_builder.rs +++ b/scripts/rustdoc_test_builder.rs @@ -72,6 +72,13 @@ fn main() { // Figure out a smaller test name based on the generated function name. let name = rustdoc_function_name.split_once("_rust_kernel_").unwrap().1; + // The `rustdoc` function name can include the absolute path when building with `O=` which is + // undesirable and creates overlong symbol names. Remap it to use a relative path. + // TODO: Remove when bumping the minimum version since with Rust >= 1.95 we already do the + // path remapping via the `rustdoc` `--remap-path-prefix` flag. + let trimmed_function_name = format!("_doctest_main_rust_kernel_{name}"); + let body = body.replace(&rustdoc_function_name, &trimmed_function_name); + let path = format!("rust/test/doctests/kernel/{name}"); std::fs::write(path, body.as_bytes()).unwrap(); -- cgit v1.2.3 From 3504d1bce9920b23499ab1af682cdf1eceba7c75 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Wed, 2 Sep 2026 22:59:59 +0100 Subject: rust: kunit: use `file!()` inside `kunit_assert!` This parameter is needed currently because doctests want to override the file. Simplify it by changing it to use `file!()`. Have doctests override `file!()` macro to achieve the current behavior. This allows us to remove the `file` helper and associated Kconfig options. Reviewed-by: David Gow Signed-off-by: Gary Guo Link: https://patch.msgid.link/20260902-kunit-v2-1-eda3ed6c00c5@garyguo.net Signed-off-by: Miguel Ojeda --- Documentation/rust/general-information.rst | 4 ++-- init/Kconfig | 3 --- rust/kernel/kunit.rs | 9 +++++---- rust/macros/helpers.rs | 16 ---------------- rust/macros/kunit.rs | 5 ++--- rust/macros/lib.rs | 5 ----- scripts/rustdoc_test_gen.rs | 10 ++++++++-- 7 files changed, 17 insertions(+), 35 deletions(-) (limited to 'scripts') diff --git a/Documentation/rust/general-information.rst b/Documentation/rust/general-information.rst index 09234bed272c..20e3178ebfd4 100644 --- a/Documentation/rust/general-information.rst +++ b/Documentation/rust/general-information.rst @@ -157,5 +157,5 @@ numerical comparisons, one may define a new Kconfig symbol: .. code-block:: kconfig - config RUSTC_HAS_SPAN_FILE - def_bool RUSTC_VERSION >= 108800 + config RUSTC_HAS_FILE_AS_C_STR + def_bool RUSTC_VERSION >= 109100 diff --git a/init/Kconfig b/init/Kconfig index 8583d9f06c52..24b019104d88 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -178,9 +178,6 @@ config LD_CAN_USE_KEEP_IN_OVERLAY # https://github.com/llvm/llvm-project/pull/130661 def_bool LD_IS_BFD || LLD_VERSION >= 210000 -config RUSTC_HAS_SPAN_FILE - def_bool RUSTC_VERSION >= 108800 - config RUSTC_HAS_UNNECESSARY_TRANSMUTES def_bool RUSTC_VERSION >= 108800 diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index 91eaff8c186a..12084873b51e 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -58,14 +58,15 @@ pub fn info(args: fmt::Arguments<'_>) { #[doc(hidden)] #[macro_export] macro_rules! kunit_assert { - ($name:literal, $file:literal, $diff:expr, $condition:expr $(,)?) => { + ($name:literal, $diff:expr, $condition:expr $(,)?) => { 'out: { // Do nothing if the condition is `true`. if $condition { break 'out; } - static FILE: &'static $crate::str::CStr = $file; + // Use `file!()` instead of `::core::file!()` here so it can be overridden. + static FILE: &'static $crate::str::CStr = $crate::c_str!(file!()); static LINE: i32 = ::core::line!() as i32 - $diff; static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition)); @@ -164,10 +165,10 @@ macro_rules! kunit_assert { #[doc(hidden)] #[macro_export] macro_rules! kunit_assert_eq { - ($name:literal, $file:literal, $diff:expr, $left:expr, $right:expr $(,)?) => {{ + ($name:literal, $diff:expr, $left:expr, $right:expr $(,)?) => {{ // For the moment, we just forward to the expression assert because, for binary asserts, // KUnit supports only a few types (e.g. integers). - $crate::kunit_assert!($name, $file, $diff, $left == $right); + $crate::kunit_assert!($name, $diff, $left == $right); }}; } diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs index d18fbf4daa0a..8cc9a20f470e 100644 --- a/rust/macros/helpers.rs +++ b/rust/macros/helpers.rs @@ -38,22 +38,6 @@ impl AsciiLitStr { } } -pub(crate) fn file() -> String { - #[cfg(not(CONFIG_RUSTC_HAS_SPAN_FILE))] - { - proc_macro::Span::call_site() - .source_file() - .path() - .to_string_lossy() - .into_owned() - } - - #[cfg(CONFIG_RUSTC_HAS_SPAN_FILE)] - { - proc_macro::Span::call_site().file() - } -} - /// Obtain all `#[cfg]` attributes. pub(crate) fn gather_cfg_attrs(attr: &[Attribute]) -> impl Iterator + '_ { attr.iter().filter(|a| a.path().is_ident("cfg")) diff --git a/rust/macros/kunit.rs b/rust/macros/kunit.rs index ae20ed6768f1..936eff014870 100644 --- a/rust/macros/kunit.rs +++ b/rust/macros/kunit.rs @@ -109,12 +109,11 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result {{ - kernel::kunit_assert!(#test_str, #path, 0, $cond); + kernel::kunit_assert!(#test_str, 0, $cond); }} } }); @@ -122,7 +121,7 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result {{ - kernel::kunit_assert_eq!(#test_str, #path, 0, $left, $right); + kernel::kunit_assert_eq!(#test_str, 0, $left, $right); }} } }); diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index b84db5c6bdc4..68386b2ed55f 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -8,11 +8,6 @@ // Stable since Rust 1.87.0. #![feature(extract_if)] -// -// Stable since Rust 1.88.0 under a different name, `proc_macro_span_file`, -// which was added in Rust 1.88.0. This is why `cfg_attr` is used here, i.e. -// to avoid depending on the full `proc_macro_span` on Rust >= 1.88.0. -#![cfg_attr(not(CONFIG_RUSTC_HAS_SPAN_FILE), feature(proc_macro_span))] mod concat_idents; mod export; diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs index d087c0d9fcb3..bb57745ec339 100644 --- a/scripts/rustdoc_test_gen.rs +++ b/scripts/rustdoc_test_gen.rs @@ -176,12 +176,18 @@ fn main() { r#"/// Generated `{name}` KUnit test case from a Rust documentation test. #[no_mangle] pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{ + /// Overrides the usual [`file!`] macro with one that expands to the real path. + #[allow(unused)] + macro_rules! file {{ + () => {{ "{real_path}" }} + }} + /// Overrides the usual [`assert!`] macro with one that calls KUnit instead. #[allow(unused)] macro_rules! assert {{ ($cond:expr $(,)?) => {{{{ ::kernel::kunit_assert!( - "{kunit_name}", c"{real_path}", __DOCTEST_ANCHOR - {line}, $cond + "{kunit_name}", __DOCTEST_ANCHOR - {line}, $cond ); }}}} }} @@ -191,7 +197,7 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{ macro_rules! assert_eq {{ ($left:expr, $right:expr $(,)?) => {{{{ ::kernel::kunit_assert_eq!( - "{kunit_name}", c"{real_path}", __DOCTEST_ANCHOR - {line}, $left, $right + "{kunit_name}", __DOCTEST_ANCHOR - {line}, $left, $right ); }}}} }} -- cgit v1.2.3 From e9a35442ef91f7f599a95e04928e80748fba2998 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Wed, 2 Sep 2026 23:00:00 +0100 Subject: rust: kunit: use `line!()` inside `kunit_assert!` The `diff` parameter is needed currently because doctests want to override the line number. Simplify it by changing it to use `line!()`. Have doctests override `line!()` macro to achieve the current behavior. An existing doctest requires `line!()` to expand to a literal; so update it to use `::core::line!()` instead. Reviewed-by: David Gow Signed-off-by: Gary Guo Link: https://patch.msgid.link/20260902-kunit-v2-2-eda3ed6c00c5@garyguo.net [ Removed spurious `///`-to-`//` change and adjusted `line!` to match. Added a comment to match `file!`'s. Made `NOTE:` more precise. Reworded to say it is only one doctest being updated. - Miguel ] Signed-off-by: Miguel Ojeda --- rust/kernel/kunit.rs | 9 +++++---- rust/kernel/str.rs | 2 +- rust/macros/kunit.rs | 4 ++-- scripts/rustdoc_test_gen.rs | 14 +++++++++++--- 4 files changed, 19 insertions(+), 10 deletions(-) (limited to 'scripts') diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index 12084873b51e..25ff272d29b2 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -58,7 +58,7 @@ pub fn info(args: fmt::Arguments<'_>) { #[doc(hidden)] #[macro_export] macro_rules! kunit_assert { - ($name:literal, $diff:expr, $condition:expr $(,)?) => { + ($name:literal, $condition:expr $(,)?) => { 'out: { // Do nothing if the condition is `true`. if $condition { @@ -67,7 +67,8 @@ macro_rules! kunit_assert { // Use `file!()` instead of `::core::file!()` here so it can be overridden. static FILE: &'static $crate::str::CStr = $crate::c_str!(file!()); - static LINE: i32 = ::core::line!() as i32 - $diff; + // Use `line!()` instead of `::core::line!()` here so it can be overridden. + static LINE: i32 = line!() as i32; static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition)); // SAFETY: FFI call without safety requirements. @@ -165,10 +166,10 @@ macro_rules! kunit_assert { #[doc(hidden)] #[macro_export] macro_rules! kunit_assert_eq { - ($name:literal, $diff:expr, $left:expr, $right:expr $(,)?) => {{ + ($name:literal, $left:expr, $right:expr $(,)?) => {{ // For the moment, we just forward to the expression assert because, for binary asserts, // KUnit supports only a few types (e.g. integers). - $crate::kunit_assert!($name, $diff, $left == $right); + $crate::kunit_assert!($name, $left == $right); }}; } diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index b3caa9a1c898..644b4279a116 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -411,7 +411,7 @@ impl AsRef for CStr { /// const BAD: &CStr = c_str!("literal"); /// /// // `c_str!` is still needed for static non-literal C strings. -/// const GOOD: &CStr = c_str!(concat!(file!(), ":", line!(), ": My CStr!")); +/// const GOOD: &CStr = c_str!(concat!(file!(), ":", ::core::line!(), ": My CStr!")); /// ``` #[macro_export] macro_rules! c_str { diff --git a/rust/macros/kunit.rs b/rust/macros/kunit.rs index 936eff014870..e9152b9d51f9 100644 --- a/rust/macros/kunit.rs +++ b/rust/macros/kunit.rs @@ -113,7 +113,7 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result {{ - kernel::kunit_assert!(#test_str, 0, $cond); + kernel::kunit_assert!(#test_str, $cond); }} } }); @@ -121,7 +121,7 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result {{ - kernel::kunit_assert_eq!(#test_str, 0, $left, $right); + kernel::kunit_assert_eq!(#test_str, $left, $right); }} } }); diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs index bb57745ec339..7159cd6e726d 100644 --- a/scripts/rustdoc_test_gen.rs +++ b/scripts/rustdoc_test_gen.rs @@ -182,12 +182,20 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{ () => {{ "{real_path}" }} }} + /// Overrides the usual [`line!`] macro with one that expands to the real line number. + #[allow(unused)] + macro_rules! line {{ + // NOTE: This does not expand to a literal, but a constant expression. + // Therefore code that depends on `line!()` expanding to a literal needs special adjustment. + () => {{ const {{ ::core::line!() - __DOCTEST_ANCHOR + {line} }} }} + }} + /// Overrides the usual [`assert!`] macro with one that calls KUnit instead. #[allow(unused)] macro_rules! assert {{ ($cond:expr $(,)?) => {{{{ ::kernel::kunit_assert!( - "{kunit_name}", __DOCTEST_ANCHOR - {line}, $cond + "{kunit_name}", $cond ); }}}} }} @@ -197,7 +205,7 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{ macro_rules! assert_eq {{ ($left:expr, $right:expr $(,)?) => {{{{ ::kernel::kunit_assert_eq!( - "{kunit_name}", __DOCTEST_ANCHOR - {line}, $left, $right + "{kunit_name}", $left, $right ); }}}} }} @@ -219,7 +227,7 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{ /// The anchor where the test code body starts. #[allow(unused)] - static __DOCTEST_ANCHOR: i32 = ::core::line!() as i32 + {body_offset} + 2; + static __DOCTEST_ANCHOR: u32 = ::core::line!() + {body_offset} + 2; {{ #![allow(unreachable_pub, clippy::disallowed_names)] {body} -- cgit v1.2.3 From a41ec2936d2fecdb958e4f4417781771bcfa683d Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Wed, 2 Sep 2026 23:00:01 +0100 Subject: rust: doctest: generate Rust kunit test suites For doctest, instead of generating C FFI functions, generate a Rust test suite with `#[kunit_tests]` and `#[test]` attributes. This makes the C glue no longer needed. Reviewed-by: David Gow Signed-off-by: Gary Guo Link: https://patch.msgid.link/20260902-kunit-v2-3-eda3ed6c00c5@garyguo.net [ Removed generated file from `rust/.gitignore` and added it to `scripts/remove-stale-files`. - Miguel ] Signed-off-by: Miguel Ojeda --- rust/.gitignore | 1 - rust/Makefile | 4 +-- scripts/remove-stale-files | 2 ++ scripts/rustdoc_test_gen.rs | 69 ++++++--------------------------------------- 4 files changed, 12 insertions(+), 64 deletions(-) (limited to 'scripts') diff --git a/rust/.gitignore b/rust/.gitignore index d3829ffab80b..5ba6b490d1e9 100644 --- a/rust/.gitignore +++ b/rust/.gitignore @@ -3,7 +3,6 @@ bindings_generated.rs bindings_helpers_generated.rs doctests_kernel_generated.rs -doctests_kernel_generated_kunit.c uapi_generated.rs exports_*_generated.h doc/ diff --git a/rust/Makefile b/rust/Makefile index 152649f209d8..97f577ed0b8f 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -39,10 +39,8 @@ obj-$(CONFIG_RUST) += exports.o always-$(CONFIG_RUST) += host/libproc_macro2.rlib host/libquote.rlib host/libsyn.rlib always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs -always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o -obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o always-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/generated_arch_static_branch_asm.rs ifndef CONFIG_UML @@ -402,7 +400,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $< $< $(rustdoc_test_kernel_quiet); \ $(objtree)/scripts/rustdoc_test_gen -%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \ +%/doctests_kernel_generated.rs: \ $(src)/kernel/lib.rs $(obj)/kernel.o \ $(objtree)/scripts/rustdoc_test_builder \ $(objtree)/scripts/rustdoc_test_gen FORCE diff --git a/scripts/remove-stale-files b/scripts/remove-stale-files index 6e39fa8540df..f1f776caae00 100755 --- a/scripts/remove-stale-files +++ b/scripts/remove-stale-files @@ -26,3 +26,5 @@ rm -f scripts/selinux/genheaders/genheaders rm -f *.spec rm -f lib/test_fortify.log + +rm -f rust/doctests_kernel_generated_kunit.c diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs index 7159cd6e726d..754c5aafcce4 100644 --- a/scripts/rustdoc_test_gen.rs +++ b/scripts/rustdoc_test_gen.rs @@ -125,9 +125,7 @@ fn main() { // Sort paths. paths.sort(); - let mut rust_tests = String::new(); - let mut c_test_declarations = String::new(); - let mut c_test_cases = String::new(); + let mut tests = String::new(); let mut body = String::new(); let mut last_file = String::new(); let mut number = 0; @@ -172,10 +170,10 @@ fn main() { use std::fmt::Write; write!( - rust_tests, + tests, r#"/// Generated `{name}` KUnit test case from a Rust documentation test. -#[no_mangle] -pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{ +#[test] +fn {kunit_name}() {{ /// Overrides the usual [`file!`] macro with one that expands to the real path. #[allow(unused)] macro_rules! file {{ @@ -190,26 +188,6 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{ () => {{ const {{ ::core::line!() - __DOCTEST_ANCHOR + {line} }} }} }} - /// Overrides the usual [`assert!`] macro with one that calls KUnit instead. - #[allow(unused)] - macro_rules! assert {{ - ($cond:expr $(,)?) => {{{{ - ::kernel::kunit_assert!( - "{kunit_name}", $cond - ); - }}}} - }} - - /// Overrides the usual [`assert_eq!`] macro with one that calls KUnit instead. - #[allow(unused)] - macro_rules! assert_eq {{ - ($left:expr, $right:expr $(,)?) => {{{{ - ::kernel::kunit_assert_eq!( - "{kunit_name}", $left, $right - ); - }}}} - }} - // Many tests need the prelude, so provide it by default. #[allow(unused)] use ::kernel::prelude::*; @@ -238,14 +216,9 @@ pub extern "C" fn {kunit_name}(__kunit_test: *mut ::kernel::bindings::kunit) {{ "# ) .unwrap(); - - write!(c_test_declarations, "void {kunit_name}(struct kunit *);\n").unwrap(); - write!(c_test_cases, " KUNIT_CASE({kunit_name}),\n").unwrap(); } - let rust_tests = rust_tests.trim(); - let c_test_declarations = c_test_declarations.trim(); - let c_test_cases = c_test_cases.trim(); + let tests = tests.trim(); write!( BufWriter::new(File::create("rust/doctests_kernel_generated.rs").unwrap()), @@ -271,34 +244,10 @@ impl ModuleMetadata for LocalModule {{ }}; }} -{rust_tests} -"# - ) - .unwrap(); - - write!( - BufWriter::new(File::create("rust/doctests_kernel_generated_kunit.c").unwrap()), - r#"/* - * `kernel` crate documentation tests. - */ - -#include - -{c_test_declarations} - -static struct kunit_case test_cases[] = {{ - {c_test_cases} - {{ }} -}}; - -static struct kunit_suite test_suite = {{ - .name = "rust_doctests_kernel", - .test_cases = test_cases, -}}; - -kunit_test_suite(test_suite); - -MODULE_LICENSE("GPL"); +#[kernel::macros::kunit_tests(rust_doctests_kernel)] +mod tests {{ +{tests} +}} "# ) .unwrap(); -- cgit v1.2.3