Skip to content

Commit 1fffb2a

Browse files
committed
Auto merge of #124431 - chenyukang:yukang-fix-rustdoc-124363, r=Nadrieril
Fix the assertion crash from rustdoc document indent widths Fixes #124363
2 parents 10505a1 + 6faedd3 commit 1fffb2a

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

compiler/rustc_resolve/src/rustdoc.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rustc_middle::ty::TyCtxt;
66
use rustc_span::def_id::DefId;
77
use rustc_span::symbol::{kw, sym, Symbol};
88
use rustc_span::{InnerSpan, Span, DUMMY_SP};
9+
use std::mem;
910
use std::ops::Range;
10-
use std::{cmp, mem};
1111

1212
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
1313
pub enum DocFragmentKind {
@@ -129,17 +129,20 @@ pub fn unindent_doc_fragments(docs: &mut [DocFragment]) {
129129
let Some(min_indent) = docs
130130
.iter()
131131
.map(|fragment| {
132-
fragment.doc.as_str().lines().fold(usize::MAX, |min_indent, line| {
133-
if line.chars().all(|c| c.is_whitespace()) {
134-
min_indent
135-
} else {
132+
fragment
133+
.doc
134+
.as_str()
135+
.lines()
136+
.filter(|line| line.chars().any(|c| !c.is_whitespace()))
137+
.map(|line| {
136138
// Compare against either space or tab, ignoring whether they are
137139
// mixed or not.
138140
let whitespace = line.chars().take_while(|c| *c == ' ' || *c == '\t').count();
139-
cmp::min(min_indent, whitespace)
140-
+ if fragment.kind == DocFragmentKind::SugaredDoc { 0 } else { add }
141-
}
142-
})
141+
whitespace
142+
+ (if fragment.kind == DocFragmentKind::SugaredDoc { 0 } else { add })
143+
})
144+
.min()
145+
.unwrap_or(usize::MAX)
143146
})
144147
.min()
145148
else {
@@ -151,13 +154,13 @@ pub fn unindent_doc_fragments(docs: &mut [DocFragment]) {
151154
continue;
152155
}
153156

154-
let min_indent = if fragment.kind != DocFragmentKind::SugaredDoc && min_indent > 0 {
157+
let indent = if fragment.kind != DocFragmentKind::SugaredDoc && min_indent > 0 {
155158
min_indent - add
156159
} else {
157160
min_indent
158161
};
159162

160-
fragment.indent = min_indent;
163+
fragment.indent = indent;
161164
}
162165
}
163166

tests/rustdoc/resolve-ice-124363.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
*/
3+
pub mod A {
4+
#![doc = "{
5+
Foo { },
6+
}"]
7+
}

0 commit comments

Comments
 (0)