about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src/rustdoc.rs
diff options
context:
space:
mode:
authoryukang <moorekang@gmail.com>2024-04-27 16:21:10 +0800
committeryukang <moorekang@gmail.com>2024-04-28 11:17:09 +0800
commit6faedd381b045079245c66970e66a5aa79af3475 (patch)
tree8f5efbcadca6b505d9d990efbaaecff05956f1b2 /compiler/rustc_resolve/src/rustdoc.rs
parentef8b9dcf23700f2e2265317611460d3a65c19eff (diff)
downloadrust-6faedd381b045079245c66970e66a5aa79af3475.tar.gz
rust-6faedd381b045079245c66970e66a5aa79af3475.zip
Fix the assertion crash from rustdoc document indent widths
Diffstat (limited to 'compiler/rustc_resolve/src/rustdoc.rs')
-rw-r--r--compiler/rustc_resolve/src/rustdoc.rs25
1 files changed, 14 insertions, 11 deletions
diff --git a/compiler/rustc_resolve/src/rustdoc.rs b/compiler/rustc_resolve/src/rustdoc.rs
index 0bc7579918c..aace0f3fef9 100644
--- a/compiler/rustc_resolve/src/rustdoc.rs
+++ b/compiler/rustc_resolve/src/rustdoc.rs
@@ -6,8 +6,8 @@ use rustc_middle::ty::TyCtxt;
 use rustc_span::def_id::DefId;
 use rustc_span::symbol::{kw, sym, Symbol};
 use rustc_span::{InnerSpan, Span, DUMMY_SP};
+use std::mem;
 use std::ops::Range;
-use std::{cmp, mem};
 
 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
 pub enum DocFragmentKind {
@@ -129,17 +129,20 @@ pub fn unindent_doc_fragments(docs: &mut [DocFragment]) {
     let Some(min_indent) = docs
         .iter()
         .map(|fragment| {
-            fragment.doc.as_str().lines().fold(usize::MAX, |min_indent, line| {
-                if line.chars().all(|c| c.is_whitespace()) {
-                    min_indent
-                } else {
+            fragment
+                .doc
+                .as_str()
+                .lines()
+                .filter(|line| line.chars().any(|c| !c.is_whitespace()))
+                .map(|line| {
                     // Compare against either space or tab, ignoring whether they are
                     // mixed or not.
                     let whitespace = line.chars().take_while(|c| *c == ' ' || *c == '\t').count();
-                    cmp::min(min_indent, whitespace)
-                        + if fragment.kind == DocFragmentKind::SugaredDoc { 0 } else { add }
-                }
-            })
+                    whitespace
+                        + (if fragment.kind == DocFragmentKind::SugaredDoc { 0 } else { add })
+                })
+                .min()
+                .unwrap_or(usize::MAX)
         })
         .min()
     else {
@@ -151,13 +154,13 @@ pub fn unindent_doc_fragments(docs: &mut [DocFragment]) {
             continue;
         }
 
-        let min_indent = if fragment.kind != DocFragmentKind::SugaredDoc && min_indent > 0 {
+        let indent = if fragment.kind != DocFragmentKind::SugaredDoc && min_indent > 0 {
             min_indent - add
         } else {
             min_indent
         };
 
-        fragment.indent = min_indent;
+        fragment.indent = indent;
     }
 }