Skip to content

[rustdoc] Do not take doc(cfg()) into account when filtering doctests - #159014

Open
GuillaumeGomez wants to merge 3 commits into
rust-lang:mainfrom
GuillaumeGomez:doc-cfg-filter-doctest
Open

[rustdoc] Do not take doc(cfg()) into account when filtering doctests#159014
GuillaumeGomez wants to merge 3 commits into
rust-lang:mainfrom
GuillaumeGomez:doc-cfg-filter-doctest

Conversation

@GuillaumeGomez

@GuillaumeGomez GuillaumeGomez commented Jul 9, 2026

Copy link
Copy Markdown
Member

View all comments

Part of #147033.

Because it was using the extract_cfg_from_attrs common function, it was taking into account the doc(cfg()) attributes the same as if they were a cfg.

I didn't mark this PR as "fix" because I didn't handle the case of the doctest not being marked as ignored because I'm not sure if we should revisit the fact that we ignore these doctests or if we should just mark them as ignored (because of target_feature(enable = "...")).

Setting @fmease as reviewer as they are likely the only one with context about this issue. 😆

r? @fmease

@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-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jul 9, 2026
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

And now it fails because we (doc) inlined some functions. Fixing that then. :)

@rustbot rustbot added O-wasi Operating system: Wasi, Webassembly System Interface O-wasm Target: WASM (WebAssembly), http://webassembly.org/ O-windows Operating system: Windows labels Jul 9, 2026
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez
GuillaumeGomez force-pushed the doc-cfg-filter-doctest branch from f5059c8 to 062cfea Compare July 9, 2026 14:58
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez
GuillaumeGomez force-pushed the doc-cfg-filter-doctest branch from 062cfea to 1d5692c Compare July 9, 2026 15:43
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Fixed CI \o/

Comment thread src/librustdoc/clean/cfg.rs
@GuillaumeGomez
GuillaumeGomez force-pushed the doc-cfg-filter-doctest branch from 1d5692c to a0bf3c9 Compare July 15, 2026 13:10
@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Reverted changes I made to extract_cfg_from_attrs and instead only look for the target_feature attributes.

@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Ah, new stuff gets added.

@GuillaumeGomez
GuillaumeGomez force-pushed the doc-cfg-filter-doctest branch from a0bf3c9 to 59a72c3 Compare July 15, 2026 14:50
@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

stdarch is developed in its own repository. If possible, consider making this change to rust-lang/stdarch instead.

cc @Amanieu, @folkertdev, @sayantn

@GuillaumeGomez
GuillaumeGomez force-pushed the doc-cfg-filter-doctest branch from 59a72c3 to 885af7e Compare July 15, 2026 15:53
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez
GuillaumeGomez force-pushed the doc-cfg-filter-doctest branch from 885af7e to 21e8051 Compare July 15, 2026 20:35
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Seems like CI is happy now.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Setting a new reviewer as fmease seems busy.

r? @Urgau

@rustbot rustbot assigned Urgau and unassigned fmease Jul 24, 2026
Comment thread library/core/src/os/darwin/objc.rs Outdated
Comment thread library/std/src/os/windows/fs.rs
Comment thread tests/run-make/rustdoc-filter-doc_cfg-doctest/file.rs Outdated

@Urgau Urgau Jul 24, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can't this test be made a regular UI test with two revisions for the editions?

//@ revisions: e2015 e2024
//@[e0215] edition: 2015
//@[e0215] edition: 2024

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes but I wanted to enforce the output, which cannot be done with ui-tests sadly. I want to be absolutely sure that we have 2 passed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that can be done with //@ regex-error-pattern: 2 passed.

@Urgau Urgau Jul 29, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did you look at using regex-error-pattern? The UI test would have the advantage of checking the whole output, even if we are not asserting something.

If not, I'm fine approving it without it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

error in revision `e2015`: error pattern 'running 2 tests' not found!
error in revision `e2024`: error pattern 'running 2 tests' not found!

It doesn't check stdout as far as I can see.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For reference, tested with:

//@ revisions: e2015 e2024
//@[e0215] edition: 2015
//@[e0215] edition: 2024
//@ compile-flags: --test
//@ regex-error-pattern: running 2 tests
//@ regex-error-pattern: test .+/doc-cfg-filter.rs - f (line 3) \.\.\. ok
//@ regex-error-pattern: test .+/doc-cfg-filter.rs - dummy::f2 \(line 11\) \.\.\. ok
//@ regex-error-pattern: test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;
//@ check-pass

#![feature(doc_cfg)]

/// ```
/// assert!(true);
/// ```
#[doc(cfg(spec))]
fn f() {}

#[doc(cfg(false))]
mod dummy {
    /// ```
    /// assert!(true);
    /// ```
    fn f2() {}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It doesn't check stdout as far as I can see.

Ah, nevermind then.

Comment thread src/librustdoc/doctest/rust.rs Outdated
Comment thread src/librustdoc/doctest/rust.rs
@Urgau Urgau added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 25, 2026
@GuillaumeGomez
GuillaumeGomez force-pushed the doc-cfg-filter-doctest branch from 21e8051 to 329ac5f Compare July 25, 2026 11:03
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Thanks for the review, updated!

@fmease fmease left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I wasn't really available for the last 3 weeks. I haven't looked at your latest implementation, I'll let Urgau do the reviewing, I'm just chiming in for some comments.

IMO, we (probably I) should definitely tackle #[target_feature(enable = …)] again next since I don't want the two drifting apart behavior-wise (thereby fully fixing #147033). Of course, that'd involve crater & an FCP.

View changes since this review

Comment on lines +70 to +71
#[cfg_attr(target_os = "macos", doc = "```no_run")]
#[cfg_attr(not(target_os = "macos"), doc = "```ignore (needs macos)")]

@fmease fmease Jul 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All these changes to libcore etc. make me question whether this change is actually desired / my bug report #147033 is valid ^^' Please instill certainty that it is :'D

Clearly, the user code is worse: We have to give up on the doc comments, check the cfg condition again despite being in a module that only gets compiled on macOS or doc.

Since this looks like a common code pattern I wish there was an official way to express this, don't y'all think? Unfortunately, ignore-* code block attributes won't help here. We'd need

/// ```no_run,only-macos

or arbitrary cfg exprs

/// ```no_run,ignore2-not(macos)

unfortunately, #[doc(test(attr(cfg(…))))] won't help either since it just butchers the generated test harness code. If that had worked that would've been beyond awesome :'(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

unfortunately, #[doc(test(attr(cfg(…))))] won't help either since it just butchers the generated test harness code. If that had worked that would've been beyond awesome :'(

What's the problem exactly with that approach?

@fmease fmease Jul 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

#![doc(test(attr(cfg(false))))]
//! ```
//! assert!(true);
//! ```
running 1 test
test q.rs - (line 2) ... FAILED

failures:

---- q.rs - (line 2) stdout ----
error[E0601]: `main` function not found in crate `rust_out`
 --> q.rs:4:29
  |
4 | } _doctest_main_q_rs_2_0() }
  |                             ^ consider adding a `main` function to `q.rs`
<12 lines omitted>

#![doc(test(attr(cfg(false))))]
//! ```
//! fn main() {
//!     assert!(true);
//! }
//! ```
running 1 test
test q.rs - (line 2) ... FAILED

failures:

---- q.rs - (line 2) stdout ----
error[E0601]: `main` function not found in crate `rust_out`
 --> q.rs:5:2
  |
5 | }
  |  ^ consider adding a `main` function to `q.rs`
<12 lines omitted>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it's up to the users to ensure their doctest is run under the right conditions. The way core/std are making all items available might not be the best one. ^^'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

error[E0601]: `main` function not found in crate `rust_out`
 --> q.rs:5:2
  |
5 | }
  |  ^ consider adding a `main` function to `q.rs`
<12 lines omitted>

Hum, seems like we are not taking into account cfgs and are blanket applying to the whole doctest crate. I wonder if we should do something about it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We are, otherwise we would see all items, whatever their cfg.

Comment thread library/core/src/os/darwin/objc.rs
@GuillaumeGomez
GuillaumeGomez force-pushed the doc-cfg-filter-doctest branch 2 times, most recently from 44fe1ed to 329ac5f Compare July 25, 2026 22:42
@GuillaumeGomez

Copy link
Copy Markdown
Member Author
  warning: unexpected `cfg` condition name: `macos`
    --> library/core/src/os/darwin/objc.rs:97:12
     |
  97 | #[cfg_attr(macos, doc = "```no_run")]
     |            ^^^^^ help: found config with similar value: `target_os = "macos"`

So I'll remove my last commit.

@rust-log-analyzer

This comment was marked as resolved.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Restarted flaky CI and now it passed.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 26, 2026

@Urgau Urgau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Urgau

Urgau commented Jul 29, 2026

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 329ac5f has been approved by Urgau

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 29, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 29, 2026
…est, r=Urgau

[rustdoc] Do not take `doc(cfg())` into account when filtering doctests

Part of rust-lang#147033.

Because it was using the `extract_cfg_from_attrs` common function, it was taking into account the `doc(cfg())` attributes the same as if they were a `cfg`.

I didn't mark this PR as "fix" because I didn't handle the case of the doctest not being marked as ignored because I'm not sure if we should revisit the fact that we ignore these doctests or if we should just mark them as ignored (because of `target_feature(enable = "...")`).

Setting @fmease as reviewer as they are likely the only one with context about this issue. 😆

r? @fmease
rust-bors Bot pushed a commit that referenced this pull request Jul 29, 2026
Rollup of 17 pull requests

Successful merges:

 - #159014 ([rustdoc] Do not take `doc(cfg())` into account when filtering doctests)
 - #159130 (a bit optimize four-digit chunks in integer formatting)
 - #159592 (core: implement bounded random sampling)
 - #159898 (Add intrinsic-test alias and set  sample rate)
 - #158247 (hermit/fs: Return `unsupported()` instead of `from_raw_os_error(22)`)
 - #158649 (Hermit: fix `readdir()` )
 - #159049 (Avoid ICE in From/TryFrom cast suggestion when encountering HRTBs)
 - #160053 (test: add test suite for the 85681 issue)
 - #160087 (Add regression test for nested associated-type projection ICE)
 - #160090 (rustc_resolve: Further reduce mutability in resolver)
 - #160099 (Resolver: split module resolutions into local and external resolutions)
 - #160106 (Add suggestions for `must_implement_one_of`)
 - #160117 (Remove unnecessary format usage)
 - #160134 (Work around Wine bug 60084 by calling WSAStartup at most once)
 - #160142 (bootstrap: remove use-lld config alias)
 - #160148 (Rename `errors.rs` file to `diagnostics.rs` (15/N))
 - #160151 (Mark a doctest as requiring unwinding)
@jhpratt

jhpratt commented Jul 29, 2026

Copy link
Copy Markdown
Member

@bors r- #160168 (comment)

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 29, 2026
@rust-bors

rust-bors Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

This PR was contained in a rollup (#160168), which was unapproved.

View changes since this unapproval

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 O-wasi Operating system: Wasi, Webassembly System Interface O-wasm Target: WASM (WebAssembly), http://webassembly.org/ O-windows Operating system: Windows S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler 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.

6 participants