Skip to content

Commit 7d5e2ac

Browse files
authored
Rollup merge of #93715 - GuillaumeGomez:horizontal-trim, r=notriddle
Fix horizontal trim for block doc comments Fixes #93662. r? `@notriddle`
2 parents b7f7850 + a476ec8 commit 7d5e2ac

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

compiler/rustc_ast/src/util/comments.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
4343
if i != 0 || j != lines.len() { Some((i, j)) } else { None }
4444
}
4545

46-
fn get_horizontal_trim(lines: &[&str], kind: CommentKind) -> Option<usize> {
46+
fn get_horizontal_trim<'a>(lines: &'a [&str], kind: CommentKind) -> Option<String> {
4747
let mut i = usize::MAX;
4848
let mut first = true;
4949

5050
// In case we have doc comments like `/**` or `/*!`, we want to remove stars if they are
5151
// present. However, we first need to strip the empty lines so they don't get in the middle
5252
// when we try to compute the "horizontal trim".
5353
let lines = if kind == CommentKind::Block {
54-
let mut i = 0;
54+
// Whatever happens, we skip the first line.
55+
let mut i = if lines[0].trim_start().starts_with('*') { 0 } else { 1 };
5556
let mut j = lines.len();
5657

5758
while i < j && lines[i].trim().is_empty() {
@@ -84,7 +85,7 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
8485
return None;
8586
}
8687
}
87-
Some(i)
88+
if lines.is_empty() { None } else { Some(lines[0][..i].into()) }
8889
}
8990

9091
let data_s = data.as_str();
@@ -102,8 +103,13 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
102103
changes = true;
103104
// remove a "[ \t]*\*" block from each line, if possible
104105
for line in lines.iter_mut() {
105-
if horizontal + 1 < line.len() {
106-
*line = &line[horizontal + 1..];
106+
if let Some(tmp) = line.strip_prefix(&horizontal) {
107+
*line = tmp;
108+
if kind == CommentKind::Block
109+
&& (*line == "*" || line.starts_with("* ") || line.starts_with("**"))
110+
{
111+
*line = &line[1..];
112+
}
107113
}
108114
}
109115
}

compiler/rustc_ast/src/util/comments/tests.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn test_block_doc_comment_3() {
2424
create_default_session_globals_then(|| {
2525
let comment = "\n let a: *i32;\n *a = 5;\n";
2626
let stripped = beautify_doc_string(Symbol::intern(comment), CommentKind::Block);
27-
assert_eq!(stripped.as_str(), " let a: *i32;\n *a = 5;");
27+
assert_eq!(stripped.as_str(), "let a: *i32;\n*a = 5;");
2828
})
2929
}
3030

@@ -41,3 +41,21 @@ fn test_line_doc_comment() {
4141
assert_eq!(stripped.as_str(), "!test");
4242
})
4343
}
44+
45+
#[test]
46+
fn test_doc_blocks() {
47+
create_default_session_globals_then(|| {
48+
let stripped =
49+
beautify_doc_string(Symbol::intern(" # Returns\n *\n "), CommentKind::Block);
50+
assert_eq!(stripped.as_str(), " # Returns\n\n");
51+
52+
let stripped = beautify_doc_string(
53+
Symbol::intern("\n * # Returns\n *\n "),
54+
CommentKind::Block,
55+
);
56+
assert_eq!(stripped.as_str(), " # Returns\n\n");
57+
58+
let stripped = beautify_doc_string(Symbol::intern("\n * a\n "), CommentKind::Block);
59+
assert_eq!(stripped.as_str(), " a\n");
60+
})
61+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
// compile-flags:--test
3+
4+
// This test ensures that no code block is detected in the doc comments.
5+
6+
pub mod Wormhole {
7+
/** # Returns
8+
*
9+
*/
10+
pub fn foofoo() {}
11+
/**
12+
* # Returns
13+
*
14+
*/
15+
pub fn barbar() {}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
running 0 tests
3+
4+
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
5+

src/test/rustdoc/strip-block-doc-comments-stars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![crate_name = "foo"]
22

3-
// The goal of this test is to answer that it won't be generated as a list because
3+
// The goal of this test is to ensure that it won't be generated as a list because
44
// block doc comments can have their lines starting with a star.
55

66
// @has foo/fn.foo.html

0 commit comments

Comments
 (0)