Fix the assertion crash from rustdoc document indent widths

This commit is contained in:
yukang 2024-04-27 16:21:10 +08:00
parent ef8b9dcf23
commit 6faedd381b
2 changed files with 21 additions and 11 deletions

View file

@ -6,8 +6,8 @@ use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::DefId; use rustc_span::def_id::DefId;
use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::{InnerSpan, Span, DUMMY_SP}; use rustc_span::{InnerSpan, Span, DUMMY_SP};
use std::mem;
use std::ops::Range; use std::ops::Range;
use std::{cmp, mem};
#[derive(Clone, Copy, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DocFragmentKind { pub enum DocFragmentKind {
@ -129,17 +129,20 @@ pub fn unindent_doc_fragments(docs: &mut [DocFragment]) {
let Some(min_indent) = docs let Some(min_indent) = docs
.iter() .iter()
.map(|fragment| { .map(|fragment| {
fragment.doc.as_str().lines().fold(usize::MAX, |min_indent, line| { fragment
if line.chars().all(|c| c.is_whitespace()) { .doc
min_indent .as_str()
} else { .lines()
.filter(|line| line.chars().any(|c| !c.is_whitespace()))
.map(|line| {
// Compare against either space or tab, ignoring whether they are // Compare against either space or tab, ignoring whether they are
// mixed or not. // mixed or not.
let whitespace = line.chars().take_while(|c| *c == ' ' || *c == '\t').count(); let whitespace = line.chars().take_while(|c| *c == ' ' || *c == '\t').count();
cmp::min(min_indent, whitespace) whitespace
+ if fragment.kind == DocFragmentKind::SugaredDoc { 0 } else { add } + (if fragment.kind == DocFragmentKind::SugaredDoc { 0 } else { add })
}
}) })
.min()
.unwrap_or(usize::MAX)
}) })
.min() .min()
else { else {
@ -151,13 +154,13 @@ pub fn unindent_doc_fragments(docs: &mut [DocFragment]) {
continue; 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 min_indent - add
} else { } else {
min_indent min_indent
}; };
fragment.indent = min_indent; fragment.indent = indent;
} }
} }

View file

@ -0,0 +1,7 @@
/**
*/
pub mod A {
#![doc = "{
Foo { },
}"]
}