* [PATCH v2] rust: doctest: fix line numbers after crate-level attributes
@ 2026-11-07 16:36 Younes Akhouayri via B4 Relay
2026-11-24 7:03 ` David Gow
0 siblings, 1 reply; 2+ messages in thread
From: Younes Akhouayri via B4 Relay @ 2026-11-07 16:36 UTC (permalink / raw)
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, Shuah Khan
Cc: linux-kselftest, kunit-dev, rust-for-linux, linux-kernel,
Younes Akhouayri
From: Younes Akhouayri <git@younes.io>
When a doctest starts with a crate-level attribute such as
`#![expect(...)]`, KUnit reports failures one line too early, and
`line!()` returns a line number that is one too low.
This happens because `rustdoc` moves those attribute lines out of the
test body and puts them above the `main` function it generates. The
generator works out where the body starts by counting the lines above
`main`, but it does not know that some of those lines were moved there
from the body. So every line after a moved attribute is mapped one line
too early (or more, if there are several such attributes).
For example, adding `assert_eq!(36, 0);` as line 36 of
`rust/kernel/init.rs` (inside the doctest that starts with
`# #![expect(clippy::undocumented_unsafe_blocks)]`) gives:
# rust_doctest_kernel_init_rs_2: ASSERTION FAILED at rust/kernel/init.rs:35
Twelve in-tree doctests start like this, e.g. in `bits.rs`, `devres.rs`,
`init.rs` and `std_vendor.rs`.
Fix it by counting the attribute lines that `rustdoc` moved (every
`#![...]` line above `main`, except the `#![allow(unused)]` that
`rustdoc` adds itself) and taking them into account when computing
where the body starts.
`rustdoc` always starts its output with that `#![allow(unused)]`, so
check for it before relying on the layout. If a future `rustdoc` (or a
crate-level `#![doc(test(attr(...)))]`) changes it, print a warning once
and keep the previous behaviour rather than failing the build: the tests
still run, only the reported line numbers would be off.
Only single-line attributes are handled, which covers every doctest in
the tree today.
With this change, the example above reports `rust/kernel/init.rs:36`,
and doctests without such attributes keep reporting the same lines as
before.
Fixes: a66d733da801 ("rust: support running Rust documentation tests as KUnit ones")
Signed-off-by: Younes Akhouayri <git@younes.io>
---
Changes in v2:
- Check that the generated test starts with `#![allow(unused)]` before
subtracting the moved lines; otherwise warn once and keep the old
behaviour, instead of clamping with `saturating_sub` (Miguel).
- Note in the comment and commit message that only single-line
attributes are handled.
- Link to v1: https://patch.msgid.link/20261027-rust-doctest-line-fix-v1-1-0c13185a58b6@younes.io
---
scripts/rustdoc_test_gen.rs | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs
index 754c5aafcce4..01128d8c62f9 100644
--- a/scripts/rustdoc_test_gen.rs
+++ b/scripts/rustdoc_test_gen.rs
@@ -127,6 +127,7 @@ fn main() {
let mut tests = String::new();
let mut body = String::new();
+ let mut warned = false;
let mut last_file = String::new();
let mut number = 0;
let mut valid_paths: Vec<PathBuf> = Vec::new();
@@ -168,6 +169,33 @@ fn main() {
.count()
+ 1;
+ // `rustdoc` moves the crate-level attributes (`#![...]`) of the test out of its body and
+ // places them before `main`, after its own `#![allow(unused)]`. Subtract the moved lines,
+ // since the rest of the body is shifted by that amount with respect to the original source.
+ //
+ // Only single-line attributes are handled. If `rustdoc` stops starting its output with
+ // `#![allow(unused)]`, this computation needs to be revisited, so warn (once) rather than
+ // silently reporting wrong line numbers, but keep going since the tests themselves still
+ // work.
+ let hoisted_lines = if body.starts_with("#![allow(unused)]\n") {
+ body.lines()
+ .take(body_offset - 1)
+ .filter(|line| line.starts_with("#!["))
+ .count()
+ - 1
+ } else {
+ if !warned {
+ eprintln!(
+ "warning: `{name}` does not start with `#![allow(unused)]`: line numbers \
+ reported by doctests may be wrong, please update \
+ `scripts/rustdoc_test_gen.rs`."
+ );
+ warned = true;
+ }
+ 0
+ };
+ let body_offset = body_offset - hoisted_lines;
+
use std::fmt::Write;
write!(
tests,
---
base-commit: 21a7f44fd0765fcc5194130b28767786eba1d1e7
change-id: 20261027-rust-doctest-line-fix-cb0f663629fe
Best regards,
--
Younes Akhouayri <git@younes.io>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] rust: doctest: fix line numbers after crate-level attributes
2026-11-07 16:36 [PATCH v2] rust: doctest: fix line numbers after crate-level attributes Younes Akhouayri via B4 Relay
@ 2026-11-24 7:03 ` David Gow
0 siblings, 0 replies; 2+ messages in thread
From: David Gow @ 2026-11-24 7:03 UTC (permalink / raw)
To: git, 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, Rae Moar,
Shuah Khan
Cc: linux-kselftest, kunit-dev, rust-for-linux, linux-kernel
Le 08/11/2026 à 00:36, Younes Akhouayri via B4 Relay a écrit :
> From: Younes Akhouayri <git@younes.io>
>
> When a doctest starts with a crate-level attribute such as
> `#![expect(...)]`, KUnit reports failures one line too early, and
> `line!()` returns a line number that is one too low.
>
> This happens because `rustdoc` moves those attribute lines out of the
> test body and puts them above the `main` function it generates. The
> generator works out where the body starts by counting the lines above
> `main`, but it does not know that some of those lines were moved there
> from the body. So every line after a moved attribute is mapped one line
> too early (or more, if there are several such attributes).
>
> For example, adding `assert_eq!(36, 0);` as line 36 of
> `rust/kernel/init.rs` (inside the doctest that starts with
> `# #![expect(clippy::undocumented_unsafe_blocks)]`) gives:
>
> # rust_doctest_kernel_init_rs_2: ASSERTION FAILED at rust/kernel/init.rs:35
>
> Twelve in-tree doctests start like this, e.g. in `bits.rs`, `devres.rs`,
> `init.rs` and `std_vendor.rs`.
A quick note that the ones in bits.rs are nested in a macro, so the
location is dubious anyway, and this doesn't fix it. Others all looked good.
>
> Fix it by counting the attribute lines that `rustdoc` moved (every
> `#![...]` line above `main`, except the `#![allow(unused)]` that
> `rustdoc` adds itself) and taking them into account when computing
> where the body starts.
>
> `rustdoc` always starts its output with that `#![allow(unused)]`, so
> check for it before relying on the layout. If a future `rustdoc` (or a
> crate-level `#![doc(test(attr(...)))]`) changes it, print a warning once
> and keep the previous behaviour rather than failing the build: the tests
> still run, only the reported line numbers would be off.
>
> Only single-line attributes are handled, which covers every doctest in
> the tree today.
>
> With this change, the example above reports `rust/kernel/init.rs:36`,
> and doctests without such attributes keep reporting the same lines as
> before.
>
> Fixes: a66d733da801 ("rust: support running Rust documentation tests as KUnit ones")
> Signed-off-by: Younes Akhouayri <git@younes.io>
> ---
> Changes in v2:
> - Check that the generated test starts with `#![allow(unused)]` before
> subtracting the moved lines; otherwise warn once and keep the old
> behaviour, instead of clamping with `saturating_sub` (Miguel).
> - Note in the comment and commit message that only single-line
> attributes are handled.
> - Link to v1: https://patch.msgid.link/20261027-rust-doctest-line-fix-v1-1-0c13185a58b6@younes.io
> ---
Thanks very much. This looks good to me!
Reviewed-by: David Gow <david@davidgow.net>
Cheers,
-- David
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-24 7:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-07 16:36 [PATCH v2] rust: doctest: fix line numbers after crate-level attributes Younes Akhouayri via B4 Relay
2026-11-24 7:03 ` David Gow
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox