Skip to content

Commit bcaf49b

Browse files
authored
Rollup merge of rust-lang#58627 - euclio:rustdoc-pass-order, r=QuietMisdreavus
rustdoc: move collapse and unindent docs passes earlier Moves these passes as early as possible so later passes will see the same markdown that is passed to the test collector. Fixes rust-lang#58473, and a similar issue with the private-doc-tests lint. r? @QuietMisdreavus
2 parents 1c93801 + 1536852 commit bcaf49b

17 files changed

+106
-152
lines changed

src/librustdoc/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl Options {
214214
if matches.opt_strs("passes") == ["list"] {
215215
println!("Available passes for running rustdoc:");
216216
for pass in passes::PASSES {
217-
println!("{:>20} - {}", pass.name(), pass.description());
217+
println!("{:>20} - {}", pass.name, pass.description);
218218
}
219219
println!("\nDefault passes for rustdoc:");
220220
for &name in passes::DEFAULT_PASSES {

src/librustdoc/core.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,12 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
603603
passes::defaults(default_passes).iter().map(|p| p.to_string()).collect();
604604
passes.extend(manual_passes);
605605

606+
info!("Executing passes");
607+
606608
for pass in &passes {
607-
// the "unknown pass" error will be reported when late passes are run
608-
if let Some(pass) = passes::find_pass(pass).and_then(|p| p.early_fn()) {
609-
krate = pass(krate, &ctxt);
609+
match passes::find_pass(pass).map(|p| p.pass) {
610+
Some(pass) => krate = pass(krate, &ctxt),
611+
None => error!("unknown pass {}, skipping", *pass),
610612
}
611613
}
612614

src/librustdoc/lib.rs

-22
Original file line numberDiff line numberDiff line change
@@ -441,28 +441,6 @@ where R: 'static + Send,
441441

442442
krate.version = crate_version;
443443

444-
info!("Executing passes");
445-
446-
for pass in &passes {
447-
// determine if we know about this pass
448-
let pass = match passes::find_pass(pass) {
449-
Some(pass) => if let Some(pass) = pass.late_fn() {
450-
pass
451-
} else {
452-
// not a late pass, but still valid so don't report the error
453-
continue
454-
}
455-
None => {
456-
error!("unknown pass {}, skipping", *pass);
457-
458-
continue
459-
},
460-
};
461-
462-
// run it
463-
krate = pass(krate);
464-
}
465-
466444
tx.send(f(Output {
467445
krate: krate,
468446
renderinfo: renderinfo,

src/librustdoc/passes/check_code_block_syntax.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ use crate::fold::DocFolder;
1010
use crate::html::markdown::{self, RustCodeBlock};
1111
use crate::passes::Pass;
1212

13-
pub const CHECK_CODE_BLOCK_SYNTAX: Pass =
14-
Pass::early("check-code-block-syntax", check_code_block_syntax,
15-
"validates syntax inside Rust code blocks");
13+
pub const CHECK_CODE_BLOCK_SYNTAX: Pass = Pass {
14+
name: "check-code-block-syntax",
15+
pass: check_code_block_syntax,
16+
description: "validates syntax inside Rust code blocks",
17+
};
1618

1719
pub fn check_code_block_syntax(krate: clean::Crate, cx: &DocContext<'_, '_, '_>) -> clean::Crate {
1820
SyntaxChecker { cx }.fold_crate(krate)

src/librustdoc/passes/collapse_docs.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
use crate::clean::{self, DocFragment, Item};
2+
use crate::core::DocContext;
23
use crate::fold;
34
use crate::fold::{DocFolder};
45
use crate::passes::Pass;
56

67
use std::mem::replace;
78

8-
pub const COLLAPSE_DOCS: Pass =
9-
Pass::late("collapse-docs", collapse_docs,
10-
"concatenates all document attributes into one document attribute");
9+
pub const COLLAPSE_DOCS: Pass = Pass {
10+
name: "collapse-docs",
11+
pass: collapse_docs,
12+
description: "concatenates all document attributes into one document attribute",
13+
};
1114

1215
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1316
enum DocFragmentKind {
@@ -26,7 +29,7 @@ impl DocFragment {
2629
}
2730
}
2831

29-
pub fn collapse_docs(krate: clean::Crate) -> clean::Crate {
32+
pub fn collapse_docs(krate: clean::Crate, _: &DocContext<'_, '_, '_>) -> clean::Crate {
3033
Collapser.fold_crate(krate)
3134
}
3235

src/librustdoc/passes/collect_intra_doc_links.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ use crate::passes::{look_for_tests, Pass};
1919

2020
use super::span_of_attrs;
2121

22-
pub const COLLECT_INTRA_DOC_LINKS: Pass =
23-
Pass::early("collect-intra-doc-links", collect_intra_doc_links,
24-
"reads a crate's documentation to resolve intra-doc-links");
22+
pub const COLLECT_INTRA_DOC_LINKS: Pass = Pass {
23+
name: "collect-intra-doc-links",
24+
pass: collect_intra_doc_links,
25+
description: "reads a crate's documentation to resolve intra-doc-links",
26+
};
2527

2628
pub fn collect_intra_doc_links(krate: Crate, cx: &DocContext<'_, '_, '_>) -> Crate {
2729
if !UnstableFeatures::from_environment().is_nightly_build() {

src/librustdoc/passes/collect_trait_impls.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ use super::Pass;
66
use rustc::util::nodemap::FxHashSet;
77
use rustc::hir::def_id::DefId;
88

9-
pub const COLLECT_TRAIT_IMPLS: Pass =
10-
Pass::early("collect-trait-impls", collect_trait_impls,
11-
"retrieves trait impls for items in the crate");
9+
pub const COLLECT_TRAIT_IMPLS: Pass = Pass {
10+
name: "collect-trait-impls",
11+
pass: collect_trait_impls,
12+
description: "retrieves trait impls for items in the crate",
13+
};
1214

1315
pub fn collect_trait_impls(krate: Crate, cx: &DocContext<'_, '_, '_>) -> Crate {
1416
let mut synth = SyntheticImplCollector::new(cx);

src/librustdoc/passes/mod.rs

+19-90
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc::lint as lint;
66
use rustc::middle::privacy::AccessLevels;
77
use rustc::util::nodemap::DefIdSet;
88
use std::mem;
9-
use std::fmt;
109
use syntax::ast::NodeId;
1110
use syntax_pos::{DUMMY_SP, Span};
1211
use std::ops::Range;
@@ -46,84 +45,14 @@ pub use self::collect_trait_impls::COLLECT_TRAIT_IMPLS;
4645
mod check_code_block_syntax;
4746
pub use self::check_code_block_syntax::CHECK_CODE_BLOCK_SYNTAX;
4847

49-
/// Represents a single pass.
48+
/// A single pass over the cleaned documentation.
49+
///
50+
/// Runs in the compiler context, so it has access to types and traits and the like.
5051
#[derive(Copy, Clone)]
51-
pub enum Pass {
52-
/// An "early pass" is run in the compiler context, and can gather information about types and
53-
/// traits and the like.
54-
EarlyPass {
55-
name: &'static str,
56-
pass: fn(clean::Crate, &DocContext<'_, '_, '_>) -> clean::Crate,
57-
description: &'static str,
58-
},
59-
/// A "late pass" is run between crate cleaning and page generation.
60-
LatePass {
61-
name: &'static str,
62-
pass: fn(clean::Crate) -> clean::Crate,
63-
description: &'static str,
64-
},
65-
}
66-
67-
impl fmt::Debug for Pass {
68-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69-
let mut dbg = match *self {
70-
Pass::EarlyPass { .. } => f.debug_struct("EarlyPass"),
71-
Pass::LatePass { .. } => f.debug_struct("LatePass"),
72-
};
73-
74-
dbg.field("name", &self.name())
75-
.field("pass", &"...")
76-
.field("description", &self.description())
77-
.finish()
78-
}
79-
}
80-
81-
impl Pass {
82-
/// Constructs a new early pass.
83-
pub const fn early(name: &'static str,
84-
pass: fn(clean::Crate, &DocContext<'_, '_, '_>) -> clean::Crate,
85-
description: &'static str) -> Pass {
86-
Pass::EarlyPass { name, pass, description }
87-
}
88-
89-
/// Constructs a new late pass.
90-
pub const fn late(name: &'static str,
91-
pass: fn(clean::Crate) -> clean::Crate,
92-
description: &'static str) -> Pass {
93-
Pass::LatePass { name, pass, description }
94-
}
95-
96-
/// Returns the name of this pass.
97-
pub fn name(self) -> &'static str {
98-
match self {
99-
Pass::EarlyPass { name, .. } |
100-
Pass::LatePass { name, .. } => name,
101-
}
102-
}
103-
104-
/// Returns the description of this pass.
105-
pub fn description(self) -> &'static str {
106-
match self {
107-
Pass::EarlyPass { description, .. } |
108-
Pass::LatePass { description, .. } => description,
109-
}
110-
}
111-
112-
/// If this pass is an early pass, returns the pointer to its function.
113-
pub fn early_fn(self) -> Option<fn(clean::Crate, &DocContext<'_, '_, '_>) -> clean::Crate> {
114-
match self {
115-
Pass::EarlyPass { pass, .. } => Some(pass),
116-
_ => None,
117-
}
118-
}
119-
120-
/// If this pass is a late pass, returns the pointer to its function.
121-
pub fn late_fn(self) -> Option<fn(clean::Crate) -> clean::Crate> {
122-
match self {
123-
Pass::LatePass { pass, .. } => Some(pass),
124-
_ => None,
125-
}
126-
}
52+
pub struct Pass {
53+
pub name: &'static str,
54+
pub pass: fn(clean::Crate, &DocContext<'_, '_, '_>) -> clean::Crate,
55+
pub description: &'static str,
12756
}
12857

12958
/// The full list of passes.
@@ -141,27 +70,27 @@ pub const PASSES: &'static [Pass] = &[
14170
];
14271

14372
/// The list of passes run by default.
144-
pub const DEFAULT_PASSES: &'static [&'static str] = &[
73+
pub const DEFAULT_PASSES: &[&str] = &[
14574
"collect-trait-impls",
75+
"collapse-docs",
76+
"unindent-comments",
14677
"check-private-items-doc-tests",
14778
"strip-hidden",
14879
"strip-private",
14980
"collect-intra-doc-links",
15081
"check-code-block-syntax",
151-
"collapse-docs",
152-
"unindent-comments",
15382
"propagate-doc-cfg",
15483
];
15584

15685
/// The list of default passes run with `--document-private-items` is passed to rustdoc.
157-
pub const DEFAULT_PRIVATE_PASSES: &'static [&'static str] = &[
86+
pub const DEFAULT_PRIVATE_PASSES: &[&str] = &[
15887
"collect-trait-impls",
88+
"collapse-docs",
89+
"unindent-comments",
15990
"check-private-items-doc-tests",
16091
"strip-priv-imports",
16192
"collect-intra-doc-links",
16293
"check-code-block-syntax",
163-
"collapse-docs",
164-
"unindent-comments",
16594
"propagate-doc-cfg",
16695
];
16796

@@ -184,8 +113,8 @@ pub fn defaults(default_set: DefaultPassOption) -> &'static [&'static str] {
184113
}
185114

186115
/// If the given name matches a known pass, returns its information.
187-
pub fn find_pass(pass_name: &str) -> Option<Pass> {
188-
PASSES.iter().find(|p| p.name() == pass_name).cloned()
116+
pub fn find_pass(pass_name: &str) -> Option<&'static Pass> {
117+
PASSES.iter().find(|p| p.name == pass_name)
189118
}
190119

191120
struct Stripper<'a> {
@@ -438,11 +367,11 @@ crate fn source_span_for_markdown_range(
438367
.span_to_snippet(span_of_attrs(attrs))
439368
.ok()?;
440369

441-
let starting_line = markdown[..md_range.start].lines().count() - 1;
442-
let ending_line = markdown[..md_range.end].lines().count() - 1;
370+
let starting_line = markdown[..md_range.start].matches('\n').count();
371+
let ending_line = starting_line + markdown[md_range.start..md_range.end].matches('\n').count();
443372

444-
// We use `split_terminator('\n')` instead of `lines()` when counting bytes so that we only
445-
// we can treat CRLF and LF line endings the same way.
373+
// We use `split_terminator('\n')` instead of `lines()` when counting bytes so that we treat
374+
// CRLF and LF line endings the same way.
446375
let mut src_lines = snippet.split_terminator('\n');
447376
let md_lines = markdown.split_terminator('\n');
448377

src/librustdoc/passes/private_items_doc_tests.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use crate::core::DocContext;
33
use crate::fold::DocFolder;
44
use crate::passes::{look_for_tests, Pass};
55

6-
7-
pub const CHECK_PRIVATE_ITEMS_DOC_TESTS: Pass =
8-
Pass::early("check-private-items-doc-tests", check_private_items_doc_tests,
9-
"check private items doc tests");
6+
pub const CHECK_PRIVATE_ITEMS_DOC_TESTS: Pass = Pass {
7+
name: "check-private-items-doc-tests",
8+
pass: check_private_items_doc_tests,
9+
description: "check private items doc tests",
10+
};
1011

1112
struct PrivateItemDocTestLinter<'a, 'tcx: 'a, 'rcx: 'a> {
1213
cx: &'a DocContext<'a, 'tcx, 'rcx>,

src/librustdoc/passes/propagate_doc_cfg.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ use std::sync::Arc;
22

33
use crate::clean::{Crate, Item};
44
use crate::clean::cfg::Cfg;
5+
use crate::core::DocContext;
56
use crate::fold::DocFolder;
67
use crate::passes::Pass;
78

8-
pub const PROPAGATE_DOC_CFG: Pass =
9-
Pass::late("propagate-doc-cfg", propagate_doc_cfg,
10-
"propagates `#[doc(cfg(...))]` to child items");
9+
pub const PROPAGATE_DOC_CFG: Pass = Pass {
10+
name: "propagate-doc-cfg",
11+
pass: propagate_doc_cfg,
12+
description: "propagates `#[doc(cfg(...))]` to child items",
13+
};
1114

12-
pub fn propagate_doc_cfg(cr: Crate) -> Crate {
15+
pub fn propagate_doc_cfg(cr: Crate, _: &DocContext<'_, '_, '_>) -> Crate {
1316
CfgPropagator { parent_cfg: None }.fold_crate(cr)
1417
}
1518

src/librustdoc/passes/strip_hidden.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ use crate::core::DocContext;
77
use crate::fold::{DocFolder, StripItem};
88
use crate::passes::{ImplStripper, Pass};
99

10-
pub const STRIP_HIDDEN: Pass =
11-
Pass::early("strip-hidden", strip_hidden,
12-
"strips all doc(hidden) items from the output");
10+
pub const STRIP_HIDDEN: Pass = Pass {
11+
name: "strip-hidden",
12+
pass: strip_hidden,
13+
description: "strips all doc(hidden) items from the output",
14+
};
1315

1416
/// Strip items marked `#[doc(hidden)]`
1517
pub fn strip_hidden(krate: clean::Crate, _: &DocContext<'_, '_, '_>) -> clean::Crate {

src/librustdoc/passes/strip_priv_imports.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ use crate::fold::{DocFolder};
33
use crate::core::DocContext;
44
use crate::passes::{ImportStripper, Pass};
55

6-
pub const STRIP_PRIV_IMPORTS: Pass = Pass::early("strip-priv-imports", strip_priv_imports,
7-
"strips all private import statements (`use`, `extern crate`) from a crate");
6+
pub const STRIP_PRIV_IMPORTS: Pass = Pass {
7+
name: "strip-priv-imports",
8+
pass: strip_priv_imports,
9+
description: "strips all private import statements (`use`, `extern crate`) from a crate",
10+
};
811

912
pub fn strip_priv_imports(krate: clean::Crate, _: &DocContext<'_, '_, '_>) -> clean::Crate {
1013
ImportStripper.fold_crate(krate)

src/librustdoc/passes/strip_private.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ use crate::fold::{DocFolder};
55
use crate::core::DocContext;
66
use crate::passes::{ImplStripper, ImportStripper, Stripper, Pass};
77

8-
pub const STRIP_PRIVATE: Pass =
9-
Pass::early("strip-private", strip_private,
10-
"strips all private items from a crate which cannot be seen externally, \
11-
implies strip-priv-imports");
8+
pub const STRIP_PRIVATE: Pass = Pass {
9+
name: "strip-private",
10+
pass: strip_private,
11+
description: "strips all private items from a crate which cannot be seen externally, \
12+
implies strip-priv-imports",
13+
};
1214

1315
/// Strip private items from the point of view of a crate or externally from a
1416
/// crate, specified by the `xcrate` flag.

src/librustdoc/passes/unindent_comments.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ use std::string::String;
33
use std::usize;
44

55
use crate::clean::{self, DocFragment, Item};
6+
use crate::core::DocContext;
67
use crate::fold::{self, DocFolder};
78
use crate::passes::Pass;
89

9-
pub const UNINDENT_COMMENTS: Pass =
10-
Pass::late("unindent-comments", unindent_comments,
11-
"removes excess indentation on comments in order for markdown to like it");
10+
pub const UNINDENT_COMMENTS: Pass = Pass {
11+
name: "unindent-comments",
12+
pass: unindent_comments,
13+
description: "removes excess indentation on comments in order for markdown to like it",
14+
};
1215

13-
pub fn unindent_comments(krate: clean::Crate) -> clean::Crate {
16+
pub fn unindent_comments(krate: clean::Crate, _: &DocContext<'_, '_, '_>) -> clean::Crate {
1417
CommentCleaner.fold_crate(krate)
1518
}
1619

0 commit comments

Comments
 (0)