Skip to content

rustdoc: display doc(cfg(false)) properly #141747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/librustdoc/clean/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,15 @@ impl Cfg {

/// Renders the configuration for long display, as a long HTML description.
pub(crate) fn render_long_html(&self) -> String {
let on = if self.should_use_with_in_description() { "with" } else { "on" };
let on = if self.omit_preposition() {
Copy link
Member

Choose a reason for hiding this comment

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

The render_long_plain function below should also have this change applied. Really it seems like we could extract a shared function that they both delegate to since the only difference is the format AFAICT.

Copy link
Member

Choose a reason for hiding this comment

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

Good point, sending a PR.

""
} else if self.should_use_with_in_description() {
"with "
} else {
"on "
};

let mut msg =
format!("Available {on} <strong>{}</strong>", Display(self, Format::LongHtml));
let mut msg = format!("Available {on}<strong>{}</strong>", Display(self, Format::LongHtml));
if self.should_append_only_to_description() {
msg.push_str(" only");
}
Expand Down Expand Up @@ -244,6 +249,10 @@ impl Cfg {
Some(self.clone())
}
}

fn omit_preposition(&self) -> bool {
matches!(self, Cfg::True | Cfg::False)
}
}

impl ops::Not for Cfg {
Expand Down
13 changes: 13 additions & 0 deletions tests/rustdoc/cfg-bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(doc_cfg)]
#![crate_name = "foo"]

// regression test for https://github.com/rust-lang/rust/issues/138112

//@ has 'foo/fn.foo.html' '//div[@class="stab portability"]' 'Available nowhere'
#[doc(cfg(false))]
pub fn foo() {}

// a cfg(true) will simply be ommited, as it is the same as no cfg.
//@ !has 'foo/fn.bar.html' '//div[@class="stab portability"]' ''
#[doc(cfg(true))]
pub fn bar() {}
Loading