[PATCH 2/2] rust: doctest: trim function name for reproducbility

From: Gary Guo

Date: Sat Jun 13 2026 - 10:08:48 EST


From: Gary Guo <gary@xxxxxxxxxxx>

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.

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 <gary@xxxxxxxxxxx>
---
scripts/rustdoc_test_builder.rs | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/scripts/rustdoc_test_builder.rs b/scripts/rustdoc_test_builder.rs
index 2b1f9ba01839..fda7284355f8 100644
--- a/scripts/rustdoc_test_builder.rs
+++ b/scripts/rustdoc_test_builder.rs
@@ -47,11 +47,18 @@ fn main() {
})
.expect("No test function found in `rustdoc`'s output.");

+ // 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
+ // undesireable and create overlong symbol names. Remap it to relative path.
+ let trimmed_function_name = format!("_doctest_main_rust_kernel_{name}");
+
// Qualify `Result` to avoid the collision with our own `Result` coming from the prelude.
let body = body.replace(
&format!("{rustdoc_function_name}() -> Result<(), impl core::fmt::Debug> {{"),
&format!(
- "{rustdoc_function_name}() -> ::core::result::Result<(), impl ::core::fmt::Debug> {{"
+ "{trimmed_function_name}() -> ::core::result::Result<(), impl ::core::fmt::Debug> {{"
),
);

@@ -62,12 +69,9 @@ fn main() {
// We save the result in a variable so that the failed assertion message looks nicer.
let body = body.replace(
&format!("}} {rustdoc_function_name}().unwrap() }}"),
- &format!("}} let test_return_value = {rustdoc_function_name}(); assert!(test_return_value.is_ok()); }}"),
+ &format!("}} let test_return_value = {trimmed_function_name}(); assert!(test_return_value.is_ok()); }}"),
);

- // Figure out a smaller test name based on the generated function name.
- let name = rustdoc_function_name.split_once("_rust_kernel_").unwrap().1;
-
let path = format!("rust/test/doctests/kernel/{name}");

std::fs::write(path, body.as_bytes()).unwrap();
--
2.54.0