From af6c3201fc3d5ec2559836454ea4f43eec583fa2 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 22 Apr 2021 14:50:56 -0400 Subject: [PATCH] rustdoc: Hide `#text` in doc-tests Since `#![attr]` and `#[attr]` are the only valid syntax that start with `#`, we can just special case those two tokens. --- src/librustdoc/html/markdown.rs | 19 +++++++++++++------ src/test/rustdoc-ui/test-hidden.rs | 25 +++++++++++++++++++++++++ src/test/rustdoc-ui/test-hidden.stdout | 6 ++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/test/rustdoc-ui/test-hidden.rs create mode 100644 src/test/rustdoc-ui/test-hidden.stdout diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 509f1730557..f1c9550b5ac 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -147,12 +147,19 @@ fn map_line(s: &str) -> Line<'_> { let trimmed = s.trim(); if trimmed.starts_with("##") { Line::Shown(Cow::Owned(s.replacen("##", "#", 1))) - } else if let Some(stripped) = trimmed.strip_prefix("# ") { - // # text - Line::Hidden(&stripped) - } else if trimmed == "#" { - // We cannot handle '#text' because it could be #[attr]. - Line::Hidden("") + } else if trimmed.starts_with('#') { + let mut without_hash = trimmed[1..].trim_start(); + if without_hash.starts_with('!') { + // #! text + without_hash = without_hash[1..].trim_start_matches(' '); + } + if without_hash.starts_with('[') { + // #[attr] or #![attr] + Line::Shown(Cow::Borrowed(s)) + } else { + // #text + Line::Hidden(without_hash) + } } else { Line::Shown(Cow::Borrowed(s)) } diff --git a/src/test/rustdoc-ui/test-hidden.rs b/src/test/rustdoc-ui/test-hidden.rs new file mode 100644 index 00000000000..9e068d4db4b --- /dev/null +++ b/src/test/rustdoc-ui/test-hidden.rs @@ -0,0 +1,25 @@ +// check-pass +// compile-flags:--test +// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME" +// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR" + +/// ``` +// If `const_err` becomes a hard error in the future, please replace this with another +// deny-by-default lint instead of removing it altogether +/// # ! [allow(const_err)] +/// const C: usize = 1/0; +/// +/// # use std::path::PathBuf; +/// #use std::path::Path; +/// let x = Path::new("y.rs"); +/// let x = PathBuf::from("y.rs"); +/// +/// #[cfg(FALSE)] +/// assert!(false); +/// +/// # [cfg(FALSE)] +/// assert!(false); +/// ``` +fn main() { + panic!(); +} diff --git a/src/test/rustdoc-ui/test-hidden.stdout b/src/test/rustdoc-ui/test-hidden.stdout new file mode 100644 index 00000000000..89d98d6d688 --- /dev/null +++ b/src/test/rustdoc-ui/test-hidden.stdout @@ -0,0 +1,6 @@ + +running 1 test +test $DIR/test-hidden.rs - main (line 6) ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME +