Skip to content

[rustdoc] Correctly handle should_panic doctest attribute and fix --no-run test flag on the 2024 edition#143900

Closed
GuillaumeGomez wants to merge 10 commits intorust-lang:masterfrom
GuillaumeGomez:fix-no-run
Closed

[rustdoc] Correctly handle should_panic doctest attribute and fix --no-run test flag on the 2024 edition#143900
GuillaumeGomez wants to merge 10 commits intorust-lang:masterfrom
GuillaumeGomez:fix-no-run

Conversation

@GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Jul 13, 2025

Fixes #143009.
Fixes #143858.

Since it includes fixes from #143453, it's taking it over (commits 2, 3 and 4 are from #143453).

For --no-run, we forgot to check the "global" options in the 2024 edition, fixed in the first commit.

For should_panic fix, the exit code check has been fixed.

cc @TroyKomodo (thanks so much for providing such a complete test, made my life a lot easier!)
r? @notriddle

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jul 13, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 13, 2025

This PR modifies run-make tests.

cc @jieyouxu

@rustbot

This comment has been minimized.

@purplesyringa
Copy link
Contributor

For should_panic fix, instead of checking the exit code, we directly wrap the doctest code with catch_unwind and if a panic happened, then we return success for the test, otherwise we display the appropriate error message.

I... do not see anything like that in the patch? There's two checks for exit code 101.

@GuillaumeGomez
Copy link
Member Author

GuillaumeGomez commented Jul 14, 2025

Woups, copied/pasted comment from original PR which was outdated after discussion with you (on the previous PR). ^^'

EDIT: Updated comment.

@purplesyringa
Copy link
Contributor

purplesyringa commented Jul 14, 2025

if langstr.should_panic {
    if out.status.code() == Some(101) {
        return Ok(());
    } else if out.status.success() {
        return Err(TestFailure::UnexpectedRunPass);
    }
}
if !out.status.success() {
    return Err(TestFailure::ExecutionFailure(out));
}

Do you think something like this would make sense, so that it's easier for the user to differentiate between aborts and panic-less success?

@GuillaumeGomez
Copy link
Member Author

TestFailure::UnexpectedRunPass displays "Test didn't panic, but it's marked `should_panic`.", which means we'd need to add a new case. Do you think it's necessary to differentiate between success and non-panic failures?

@purplesyringa
Copy link
Contributor

purplesyringa commented Jul 14, 2025

Wouldn't changing UnexpectedRunPass to spell "Test executable succeeded, but it's marked should_panic." (i.e. rolling back the change in this PR) work? After all, if the change was to let UnexpectedRunPass represent non-panicking failures, it won't be necessary if we just use ExecutionFailure for that.

@GuillaumeGomez
Copy link
Member Author

That wouldn't cover exit(1) anymore if we did so.

@purplesyringa
Copy link
Contributor

I've amended my comment a moment before you replied, apologies.

@GuillaumeGomez
Copy link
Member Author

GuillaumeGomez commented Jul 14, 2025

Replied too fast then. 😅

The message for ExecutionFailure is too general imo ("Test executable failed"). It doesn't help to understand should_panic didn't panic. I think once this PR is merged, you can send a new one to create a new variant in case it failed but didn't panic, like that we can have a message that is covering this case. What do you think?

@purplesyringa
Copy link
Contributor

purplesyringa commented Jul 14, 2025

The message for ExecutionFailure is too general imo ("Test executable failed"). It doesn't help to understand should_panic didn't panic.

I think we're talking past each other. I suggest that:

  • If the program didn't panic and exited with code 0, we keep using UnexpectedRunPass ("test didn't panic"). This is the main use case for should_panic and has a perfectly legible description.
  • If the program didn't panic and exited with a non-zero code, we use ExecutionFailure ("executable failed"). I believe that it's highly unlikely this failure mode of a should_panic test can be attributed to anything but UB or an abort, exactly like with non-should_panic tests, so I don't think it's necessary to specify that it's happened in a should_panic test specifically.

Wouldn't that work?

@GuillaumeGomez
Copy link
Member Author

I still think it's too general. For end users, should_panic means the execution is supposed to fail. However it might be confusing to get "executable failed" when it's exactly what's expected (well, except it was expecting a panic and it's different). Hence why I think the current message is better and why, if we go down this road, we should add a new case to handle this case instead of relying on the too generated ExecutionFailure.

@purplesyringa
Copy link
Contributor

That makes sense, yeah. Let's delay it until after this PR lands then.

@bors
Copy link
Collaborator

bors commented Jul 30, 2025

☔ The latest upstream changes (presumably #144692) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot

This comment has been minimized.

samueltardieu added a commit to samueltardieu/rust that referenced this pull request Jul 31, 2025
…Amanieu

Make `libtest::ERROR_EXIT_CODE` const public to not redefine it in rustdoc

I think it's better to make this constant public so it can be used by crates using `libtest` as dependency.

As a side-note, I will update rust-lang#143900 to make use of this constant once this is current PR is merged.
@GuillaumeGomez
Copy link
Member Author

Ah, this time a cargo test failed. Checking what's wrong.

@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez
Copy link
Member Author

@bors try jobs=x86_64-gnu-aux,test-various

@rust-bors

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link
Contributor

rust-bors bot commented Oct 7, 2025

☀️ Try build successful (CI)
Build commit: 1514cdd (1514cddf55f57c7a67702bced791e2db666e031f, parent: 4a54b26d30dac43778afb0e503524b763fce0eee)

@GuillaumeGomez
Copy link
Member Author

This failure definitely illustrates why I should finish writing the lint in case the merged doctests failed compilation...

@GuillaumeGomez
Copy link
Member Author

@bors try jobs=x86_64-gnu-aux,test-various

@rust-bors

This comment has been minimized.

@rust-bors
Copy link
Contributor

rust-bors bot commented Oct 7, 2025

☀️ Try build successful (CI)
Build commit: 7c0af15 (7c0af154b8a6f0d2bb19a467cc9916ce269f3613, parent: 4a54b26d30dac43778afb0e503524b763fce0eee)

Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions and questions. As requested, feel free to r=me once they're all addressed in one way or another.

Although, I've got to admit that I feel slightly uneasy about some of these changes or rather the fact that this will ship "insta-stably". Anyhow, it's probably fine.

View changes since this review

@GuillaumeGomez
Copy link
Member Author

Let's go again then!

@bors r=fmease

@bors
Copy link
Collaborator

bors commented Oct 8, 2025

📌 Commit 6060bcc has been approved by fmease

It is now in the queue for this repository.

@Zalathar
Copy link
Member

Zalathar commented Oct 9, 2025

Possibly failed in rollup in armhf-gnu: #147505 (comment)

@bors r-

@Zalathar
Copy link
Member

Zalathar commented Oct 9, 2025

@bors try jobs=armhf-gnu

@rust-bors

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job armhf-gnu failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
---- library/alloc/src/slice.rs - slice::[T]::repeat (line 498) stdout end ----
---- library/alloc/src/vec/mod.rs - vec::Vec (line 253) stdout ----

thread 'library/alloc/src/vec/mod.rs - vec::Vec (line 253)' (11926) panicked at /tmp/rustdoctesthWSKOt/doctest_bundle_2024.rs:8785:17:
index out of bounds: the len is 4 but the index is 6
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_bounds_check
   3: <usize as core::slice::index::SliceIndex<[T]>>::index
   4: <alloc::vec::Vec<T,A> as core::ops::index::Index<I>>::index
   5: doctest_bundle_2024::__doctest_578::main
   6: doctest_bundle_2024::__doctest_578::__main_fn
   7: doctest_runner_2024::__doctest_578::TEST::{{closure}}
   8: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
---- library/alloc/src/vec/mod.rs - vec::Vec (line 253) stdout end ----

failures:

@rust-bors
Copy link
Contributor

rust-bors bot commented Oct 9, 2025

💔 Test for 4d1b974 failed: CI. Failed jobs:

@purplesyringa
Copy link
Contributor

This has been buggy since 796c4ef because new_doctests always receives false for should_panic. doctest_path returns None on this CI, so test::assert_test_result(doctest_bundle::{test_id}::__main_fn())) is invoked, and it panics; and new_doctest assumes the test shouldn't panic, so it says the test failed.

@GuillaumeGomez
Copy link
Member Author

Superseded by #147674.

@GuillaumeGomez
Copy link
Member Author

@purplesyringa: Thanks for the comment! So the problem is actually that should_panic tests are ignored by #[test] on some targets and rustdoc doesn't do this check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-run-make Area: port run-make Makefiles to rmake.rs relnotes Marks issues that should be documented in the release notes of the next release. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rustdoc --no-run runs when --edition=2024 is provided should_panic in doctests accepts crashes, aborts, std::process::exit