[PATCH 1/2] rust: doctest: fix incorrect pattern in replacement

0 views
Skip to first unread message

Gary Guo

unread,
Jun 13, 2026, 10:05:00 AMJun 13
to Brendan Higgins, David Gow, Rae Moar, Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich, Greg Kroah-Hartman, Igor Korotin, rust-fo...@vger.kernel.org, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org
From: Gary Guo <ga...@garyguo.net>

The `-> Result<(), impl core::fmt::Debug>` string is generated by rustdoc
and by adding "::" into the string it no longer finds anything, and making
the line useless. Remove the "::" in the pattern (but keep it in the
replacement result).

Fixes: de7cd3e4d638 ("rust: use absolute paths in macros referencing core and kernel")
Signed-off-by: Gary Guo <ga...@garyguo.net>
---
scripts/rustdoc_test_builder.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/rustdoc_test_builder.rs b/scripts/rustdoc_test_builder.rs
index f7540bcf595a..2b1f9ba01839 100644
--- a/scripts/rustdoc_test_builder.rs
+++ b/scripts/rustdoc_test_builder.rs
@@ -49,7 +49,7 @@ fn main() {

// 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}() -> Result<(), impl core::fmt::Debug> {{"),
&format!(
"{rustdoc_function_name}() -> ::core::result::Result<(), impl ::core::fmt::Debug> {{"
),

base-commit: abe651837cb394f76d738a7a747322fca3bf17ba
--
2.54.0

Gary Guo

unread,
Jun 13, 2026, 10:08:32 AMJun 13
to Brendan Higgins, David Gow, Rae Moar, Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich, Greg Kroah-Hartman, Igor Korotin, rust-fo...@vger.kernel.org, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org
From: Gary Guo <ga...@garyguo.net>

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 <ga...@garyguo.net>
---
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

Gary Guo

unread,
Jun 16, 2026, 9:27:04 AMJun 16
to Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Özkan, Brendan Higgins, David Gow, Rae Moar, Igor Korotin, Greg Kroah-Hartman, rust-fo...@vger.kernel.org, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org
From: Gary Guo <ga...@garyguo.net>

The `-> Result<(), impl core::fmt::Debug>` string is generated by rustdoc
and by adding "::" into the string it no longer finds anything, and making
the line useless.

Remove the "::" in the pattern. Omit it in the replacement too, for
consistency with upstream rustdoc.

Fixes: de7cd3e4d638 ("rust: use absolute paths in macros referencing core and kernel")
Signed-off-by: Gary Guo <ga...@garyguo.net>
--
Changes in v2:
- Update comments too
- Stops using :: in the replacement too to be consistent with upstream
rustdoc
---
scripts/rustdoc_test_builder.rs | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/scripts/rustdoc_test_builder.rs b/scripts/rustdoc_test_builder.rs
index f7540bcf595a..5457679c12f9 100644
--- a/scripts/rustdoc_test_builder.rs
+++ b/scripts/rustdoc_test_builder.rs
@@ -28,7 +28,7 @@ fn main() {
//
// ```
// fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_28_0() {
- // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_37_0() -> Result<(), impl ::core::fmt::Debug> {
+ // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_37_0() -> Result<(), impl core::fmt::Debug> {
// ```
//
// It should be unlikely that doctest code matches such lines (when code is formatted properly).
@@ -47,12 +47,14 @@ fn main() {
})
.expect("No test function found in `rustdoc`'s output.");

- // Qualify `Result` to avoid the collision with our own `Result` coming from the prelude.
+ // Replicate rustdoc 1.87+ behaviour by fully qualify `Result` (to avoid the collision with our
+ // own `Result` coming from the prelude).
+ //
+ // TODO: Remove this when MSRV is bumped above 1.87.
let body = body.replace(
- &format!("{rustdoc_function_name}() -> Result<(), impl ::core::fmt::Debug> {{"),
- &format!(
- "{rustdoc_function_name}() -> ::core::result::Result<(), impl ::core::fmt::Debug> {{"
- ),
+ &format!("{rustdoc_function_name}() -> Result<(), impl core::fmt::Debug> {{"),
+ // This intentionally does not use absolute paths to match rustdoc 1.87 behaviour.
+ &format!("{rustdoc_function_name}() -> core::result::Result<(), impl core::fmt::Debug> {{"),
);

// For tests that get generated with `Result`, like above, `rustdoc` generates an `unwrap()` on

base-commit: c425609d6ac4012c8bbf01ec2e10e801b1923a7b
prerequisite-patch-id: 0000000000000000000000000000000000000000
--
2.54.0

Gary Guo

unread,
Jun 16, 2026, 9:27:07 AMJun 16
to Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Özkan, Brendan Higgins, David Gow, Rae Moar, Igor Korotin, Greg Kroah-Hartman, rust-fo...@vger.kernel.org, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org
From: Gary Guo <ga...@garyguo.net>

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 <ga...@garyguo.net>
---
Changes in v2:
- Do an unconditional replacement to cover all cases.
---
scripts/rustdoc_test_builder.rs | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/scripts/rustdoc_test_builder.rs b/scripts/rustdoc_test_builder.rs
index 5457679c12f9..79cef75f5af3 100644
--- a/scripts/rustdoc_test_builder.rs
+++ b/scripts/rustdoc_test_builder.rs
@@ -70,6 +70,11 @@ 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
+ // undesireable and create overlong symbol names. Remap it to use relative path.
+ let trimmed_function_name = format!("_doctest_main_rust_kernel_{name}");
+ let body = body.replace(&rustdoc_function_name, &trimmed_function_name);
+

Miguel Ojeda

unread,
Jun 19, 2026, 3:35:36 AMJun 19
to Gary Guo, Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Özkan, Brendan Higgins, David Gow, Rae Moar, Igor Korotin, Greg Kroah-Hartman, rust-fo...@vger.kernel.org, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org
On Tue, Jun 16, 2026 at 3:27 PM Gary Guo <ga...@kernel.org> wrote:
>
> The `-> Result<(), impl core::fmt::Debug>` string is generated by rustdoc
> and by adding "::" into the string it no longer finds anything, and making
> the line useless.
>
> Remove the "::" in the pattern. Omit it in the replacement too, for
> consistency with upstream rustdoc.
>
> Fixes: de7cd3e4d638 ("rust: use absolute paths in macros referencing core and kernel")
> Signed-off-by: Gary Guo <ga...@garyguo.net>

Applied to `rust-fixes` -- thanks!

[ Improved comments for consistency. Reworded to drop changelog and to
fix typo. - Miguel ]

While it doesn't happen to hurt much at the moment, the change here
was not intended and we should be able to easily backport it, so:

Cc: sta...@vger.kernel.org

Cheers,
Miguel

Miguel Ojeda

unread,
Jun 19, 2026, 3:46:29 AMJun 19
to Gary Guo, Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Özkan, Brendan Higgins, David Gow, Rae Moar, Igor Korotin, Greg Kroah-Hartman, rust-fo...@vger.kernel.org, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org
On Fri, Jun 19, 2026 at 9:35 AM Miguel Ojeda
<miguel.oje...@gmail.com> wrote:
>
> [ Improved comments for consistency. Reworded to drop changelog and to
> fix typo. - Miguel ]

Actually, I just added a link to the comment to the upstream `rustdoc`
PR for context.

Cheers,
Miguel
Reply all
Reply to author
Forward
0 new messages