mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Gow <david@davidgow.net>
To: git@younes.io, "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Brendan Higgins" <brendan.higgins@linux.dev>,
	"Rae Moar" <raemoar63@gmail.com>,
	"Shuah Khan" <skhan@linuxfoundation.org>
Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] rust: doctest: fix line numbers after crate-level attributes
Date: Tue, 24 Nov 2026 15:03:00 +0800	[thread overview]
Message-ID: <b3285ace-e89b-4d2e-bbd0-14e7a80609ac@davidgow.net> (raw)
In-Reply-To: <20261107-rust-doctest-line-fix-v2-1-a66528f0a7c3@younes.io>

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

      reply	other threads:[~2026-11-24  7:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-07 16:36 Younes Akhouayri via B4 Relay
2026-11-24  7:03 ` David Gow [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b3285ace-e89b-4d2e-bbd0-14e7a80609ac@davidgow.net \
    --to=david@davidgow.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=brendan.higgins@linux.dev \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=git@younes.io \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=raemoar63@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®