8561: Accept `E<error_number>` notation in doctests r=Veykril a=ChayimFriedman2

````
```compile_fail,E0000
```
````

The code was stolen from rustdoc at 392ba2ba1a/src/librustdoc/html/markdown.rs (L866-L867)

Co-authored-by: Chayim Refael Friedman <chayimfr@gmail.com>
This commit is contained in:
bors[bot] 2021-04-18 09:57:43 +00:00 committed by GitHub
commit e8e145f13c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View file

@ -90,6 +90,13 @@ const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
"edition2021", "edition2021",
]; ];
fn is_rustdoc_fence_token(token: &str) -> bool {
if RUSTDOC_FENCE_TOKENS.contains(&token) {
return true;
}
token.starts_with('E') && token.len() == 5 && token[1..].parse::<u32>().is_ok()
}
/// Injection of syntax highlighting of doctests. /// Injection of syntax highlighting of doctests.
pub(super) fn doc_comment( pub(super) fn doc_comment(
hl: &mut Highlights, hl: &mut Highlights,
@ -174,8 +181,7 @@ pub(super) fn doc_comment(
is_codeblock = !is_codeblock; is_codeblock = !is_codeblock;
// Check whether code is rust by inspecting fence guards // Check whether code is rust by inspecting fence guards
let guards = &line[idx + RUSTDOC_FENCE.len()..]; let guards = &line[idx + RUSTDOC_FENCE.len()..];
let is_rust = let is_rust = guards.split(',').all(|sub| is_rustdoc_fence_token(sub.trim()));
guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim()));
is_doctest = is_codeblock && is_rust; is_doctest = is_codeblock && is_rust;
continue; continue;
} }

View file

@ -27,9 +27,8 @@ pub(crate) fn format_docs(src: &str) -> String {
in_code_block ^= true; in_code_block ^= true;
if in_code_block { if in_code_block {
is_rust = header is_rust =
.split(',') header.split(',').all(|sub| is_rust_specific_code_block_attribute(sub.trim()));
.all(|sub| RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC.contains(&sub.trim()));
if is_rust { if is_rust {
line = "```rust"; line = "```rust";
@ -42,6 +41,13 @@ pub(crate) fn format_docs(src: &str) -> String {
processed_lines.join("\n") processed_lines.join("\n")
} }
fn is_rust_specific_code_block_attribute(attr: &str) -> bool {
if RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC.contains(&attr) {
return true;
}
attr.starts_with('E') && attr.len() == 5 && attr[1..].parse::<u32>().is_ok()
}
fn code_line_ignored_by_rustdoc(line: &str) -> bool { fn code_line_ignored_by_rustdoc(line: &str) -> bool {
let trimmed = line.trim(); let trimmed = line.trim();
trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t") trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
@ -81,6 +87,12 @@ mod tests {
assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```"); assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
} }
#[test]
fn test_format_docs_handles_error_codes() {
let comment = "```compile_fail,E0641\nlet b = 0 as *const _;\n```";
assert_eq!(format_docs(comment), "```rust\nlet b = 0 as *const _;\n```");
}
#[test] #[test]
fn test_format_docs_skips_comments_in_rust_block() { fn test_format_docs_skips_comments_in_rust_block() {
let comment = let comment =