Skip to content

Commit 5f76b95

Browse files
committed
Change DocFragments from enum variant fields to structs with a nested enum
This makes the code a lot easier to work with. It also makes it easier to add new fields without updating each variant and `match` individually. - Name the `Kind` variant after `DocFragmentKind` from `collapse_docs` - Remove unneeded impls
1 parent 7820135 commit 5f76b95

File tree

5 files changed

+48
-88
lines changed

5 files changed

+48
-88
lines changed

src/librustdoc/clean/types.rs

+24-36
Original file line numberDiff line numberDiff line change
@@ -370,32 +370,22 @@ impl<I: IntoIterator<Item = ast::NestedMetaItem>> NestedAttributesExt for I {
370370
/// information can be given when a doctest fails. Sugared doc comments and "raw" doc comments are
371371
/// kept separate because of issue #42760.
372372
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
373-
pub enum DocFragment {
373+
pub struct DocFragment {
374+
pub line: usize,
375+
pub span: rustc_span::Span,
376+
pub doc: String,
377+
pub kind: DocFragmentKind,
378+
}
379+
380+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
381+
pub enum DocFragmentKind {
374382
/// A doc fragment created from a `///` or `//!` doc comment.
375-
SugaredDoc(usize, rustc_span::Span, String),
383+
SugaredDoc,
376384
/// A doc fragment created from a "raw" `#[doc=""]` attribute.
377-
RawDoc(usize, rustc_span::Span, String),
385+
RawDoc,
378386
/// A doc fragment created from a `#[doc(include="filename")]` attribute. Contains both the
379387
/// given filename and the file contents.
380-
Include(usize, rustc_span::Span, String, String),
381-
}
382-
383-
impl DocFragment {
384-
pub fn as_str(&self) -> &str {
385-
match *self {
386-
DocFragment::SugaredDoc(_, _, ref s) => &s[..],
387-
DocFragment::RawDoc(_, _, ref s) => &s[..],
388-
DocFragment::Include(_, _, _, ref s) => &s[..],
389-
}
390-
}
391-
392-
pub fn span(&self) -> rustc_span::Span {
393-
match *self {
394-
DocFragment::SugaredDoc(_, span, _)
395-
| DocFragment::RawDoc(_, span, _)
396-
| DocFragment::Include(_, span, _, _) => span,
397-
}
398-
}
388+
Include { filename: String },
399389
}
400390

401391
impl<'a> FromIterator<&'a DocFragment> for String {
@@ -407,12 +397,7 @@ impl<'a> FromIterator<&'a DocFragment> for String {
407397
if !acc.is_empty() {
408398
acc.push('\n');
409399
}
410-
match *frag {
411-
DocFragment::SugaredDoc(_, _, ref docs)
412-
| DocFragment::RawDoc(_, _, ref docs)
413-
| DocFragment::Include(_, _, _, ref docs) => acc.push_str(docs),
414-
}
415-
400+
acc.push_str(&frag.doc);
416401
acc
417402
})
418403
}
@@ -547,15 +532,15 @@ impl Attributes {
547532
.filter_map(|attr| {
548533
if let Some(value) = attr.doc_str() {
549534
let value = beautify_doc_string(value);
550-
let mk_fragment: fn(_, _, _) -> _ = if attr.is_doc_comment() {
551-
DocFragment::SugaredDoc
535+
let kind = if attr.is_doc_comment() {
536+
DocFragmentKind::SugaredDoc
552537
} else {
553-
DocFragment::RawDoc
538+
DocFragmentKind::RawDoc
554539
};
555540

556541
let line = doc_line;
557542
doc_line += value.lines().count();
558-
doc_strings.push(mk_fragment(line, attr.span, value));
543+
doc_strings.push(DocFragment { line, span: attr.span, doc: value, kind });
559544

560545
if sp.is_none() {
561546
sp = Some(attr.span);
@@ -575,9 +560,12 @@ impl Attributes {
575560
{
576561
let line = doc_line;
577562
doc_line += contents.lines().count();
578-
doc_strings.push(DocFragment::Include(
579-
line, attr.span, filename, contents,
580-
));
563+
doc_strings.push(DocFragment {
564+
line,
565+
span: attr.span,
566+
doc: contents,
567+
kind: DocFragmentKind::Include { filename },
568+
});
581569
}
582570
}
583571
}
@@ -621,7 +609,7 @@ impl Attributes {
621609
/// Finds the `doc` attribute as a NameValue and returns the corresponding
622610
/// value found.
623611
pub fn doc_value(&self) -> Option<&str> {
624-
self.doc_strings.first().map(|s| s.as_str())
612+
self.doc_strings.first().map(|s| s.doc.as_str())
625613
}
626614

627615
/// Finds all `doc` attributes as NameValues and returns their corresponding values, joined

src/librustdoc/passes/calculate_doc_coverage.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,12 @@ impl fold::DocFolder for CoverageCalculator {
232232
let mut tests = Tests { found_tests: 0 };
233233

234234
find_testable_code(
235-
&i.attrs.doc_strings.iter().map(|d| d.as_str()).collect::<Vec<_>>().join("\n"),
235+
&i.attrs
236+
.doc_strings
237+
.iter()
238+
.map(|d| d.doc.as_str())
239+
.collect::<Vec<_>>()
240+
.join("\n"),
236241
&mut tests,
237242
ErrorCodes::No,
238243
false,

src/librustdoc/passes/collapse_docs.rs

+12-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::clean::{self, DocFragment, Item};
1+
use crate::clean::{self, DocFragment, DocFragmentKind, Item};
22
use crate::core::DocContext;
33
use crate::fold;
44
use crate::fold::DocFolder;
@@ -12,23 +12,6 @@ pub const COLLAPSE_DOCS: Pass = Pass {
1212
description: "concatenates all document attributes into one document attribute",
1313
};
1414

15-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
16-
enum DocFragmentKind {
17-
Sugared,
18-
Raw,
19-
Include,
20-
}
21-
22-
impl DocFragment {
23-
fn kind(&self) -> DocFragmentKind {
24-
match *self {
25-
DocFragment::SugaredDoc(..) => DocFragmentKind::Sugared,
26-
DocFragment::RawDoc(..) => DocFragmentKind::Raw,
27-
DocFragment::Include(..) => DocFragmentKind::Include,
28-
}
29-
}
30-
}
31-
3215
pub fn collapse_docs(krate: clean::Crate, _: &DocContext<'_>) -> clean::Crate {
3316
let mut krate = Collapser.fold_crate(krate);
3417
krate.collapsed = true;
@@ -50,30 +33,22 @@ fn collapse(doc_strings: &mut Vec<DocFragment>) {
5033

5134
for frag in take(doc_strings) {
5235
if let Some(mut curr_frag) = last_frag.take() {
53-
let curr_kind = curr_frag.kind();
54-
let new_kind = frag.kind();
36+
let curr_kind = &curr_frag.kind;
37+
let new_kind = &frag.kind;
5538

56-
if curr_kind == DocFragmentKind::Include || curr_kind != new_kind {
57-
match curr_frag {
58-
DocFragment::SugaredDoc(_, _, ref mut doc_string)
59-
| DocFragment::RawDoc(_, _, ref mut doc_string) => {
60-
// add a newline for extra padding between segments
61-
doc_string.push('\n');
62-
}
63-
_ => {}
39+
if matches!(*curr_kind, DocFragmentKind::Include { .. }) || curr_kind != new_kind {
40+
if *curr_kind == DocFragmentKind::SugaredDoc
41+
|| *curr_kind == DocFragmentKind::RawDoc
42+
{
43+
// add a newline for extra padding between segments
44+
curr_frag.doc.push('\n');
6445
}
6546
docs.push(curr_frag);
6647
last_frag = Some(frag);
6748
} else {
68-
match curr_frag {
69-
DocFragment::SugaredDoc(_, ref mut span, ref mut doc_string)
70-
| DocFragment::RawDoc(_, ref mut span, ref mut doc_string) => {
71-
doc_string.push('\n');
72-
doc_string.push_str(frag.as_str());
73-
*span = span.to(frag.span());
74-
}
75-
_ => unreachable!(),
76-
}
49+
curr_frag.doc.push('\n');
50+
curr_frag.doc.push_str(&frag.doc);
51+
curr_frag.span = curr_frag.span.to(frag.span);
7752
last_frag = Some(curr_frag);
7853
}
7954
} else {

src/librustdoc/passes/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::mem;
88
use std::ops::Range;
99

1010
use self::Condition::*;
11-
use crate::clean::{self, GetDefId, Item};
11+
use crate::clean::{self, DocFragmentKind, GetDefId, Item};
1212
use crate::core::DocContext;
1313
use crate::fold::{DocFolder, StripItem};
1414

@@ -314,11 +314,11 @@ crate fn span_of_attrs(attrs: &clean::Attributes) -> Option<Span> {
314314
if attrs.doc_strings.is_empty() {
315315
return None;
316316
}
317-
let start = attrs.doc_strings[0].span();
317+
let start = attrs.doc_strings[0].span;
318318
if start == DUMMY_SP {
319319
return None;
320320
}
321-
let end = attrs.doc_strings.last().expect("no doc strings provided").span();
321+
let end = attrs.doc_strings.last().expect("no doc strings provided").span;
322322
Some(start.to(end))
323323
}
324324

@@ -333,10 +333,8 @@ crate fn source_span_for_markdown_range(
333333
md_range: &Range<usize>,
334334
attrs: &clean::Attributes,
335335
) -> Option<Span> {
336-
let is_all_sugared_doc = attrs.doc_strings.iter().all(|frag| match frag {
337-
clean::DocFragment::SugaredDoc(..) => true,
338-
_ => false,
339-
});
336+
let is_all_sugared_doc =
337+
attrs.doc_strings.iter().all(|frag| frag.kind == DocFragmentKind::SugaredDoc);
340338

341339
if !is_all_sugared_doc {
342340
return None;

src/librustdoc/passes/unindent_comments.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ impl clean::Attributes {
3636

3737
fn unindent_fragments(docs: &mut Vec<DocFragment>) {
3838
for fragment in docs {
39-
match *fragment {
40-
DocFragment::SugaredDoc(_, _, ref mut doc_string)
41-
| DocFragment::RawDoc(_, _, ref mut doc_string)
42-
| DocFragment::Include(_, _, _, ref mut doc_string) => {
43-
*doc_string = unindent(doc_string)
44-
}
45-
}
39+
fragment.doc = unindent(&fragment.doc);
4640
}
4741
}
4842

0 commit comments

Comments
 (0)