Skip to content

Commit 6d77797

Browse files
committed
rustdoc: Defossilize the passes infrastructure
1 parent 497937d commit 6d77797

15 files changed

+71
-158
lines changed

src/librustdoc/core.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
3434
use crate::formats::cache::Cache;
3535
use crate::html::macro_expansion::{ExpandedCode, source_macro_expansion};
3636
use crate::passes;
37-
use crate::passes::Condition::*;
3837
use crate::passes::collect_intra_doc_links::LinkCollector;
3938

4039
pub(crate) struct DocContext<'tcx> {
@@ -423,39 +422,40 @@ pub(crate) fn run_global_ctxt(
423422
);
424423
}
425424

426-
info!("Executing passes");
427-
428-
let mut visited = FxHashMap::default();
429-
let mut ambiguous = FxIndexMap::default();
430-
431-
for p in passes::defaults(show_coverage) {
432-
let run = match p.condition {
433-
Always => true,
434-
WhenDocumentPrivate => ctxt.document_private(),
435-
WhenNotDocumentPrivate => !ctxt.document_private(),
436-
WhenNotDocumentHidden => !ctxt.document_hidden(),
437-
};
438-
if run {
439-
debug!("running pass {}", p.pass.name);
440-
if let Some(run_fn) = p.pass.run {
441-
krate = tcx.sess.time(p.pass.name, || run_fn(krate, &mut ctxt));
442-
} else {
443-
let (k, LinkCollector { visited_links, ambiguous_links, .. }) =
444-
passes::collect_intra_doc_links::collect_intra_doc_links(krate, &mut ctxt);
445-
krate = k;
446-
visited = visited_links;
447-
ambiguous = ambiguous_links;
448-
}
449-
}
425+
info!("running passes");
426+
427+
let mut visited_links = FxHashMap::default();
428+
let mut ambiguous_links = FxIndexMap::default();
429+
430+
if !show_coverage {
431+
krate = passes::track!(tcx, collect_trait_impls(krate, &mut ctxt));
432+
krate = passes::track!(tcx, check_doc_test_visibility(krate, &mut ctxt));
433+
krate = passes::track!(tcx, check_doc_cfg(krate, &mut ctxt));
434+
krate = passes::track!(tcx, strip_aliased_non_local(krate, &mut ctxt));
435+
krate = passes::track!(tcx, propagate_doc_cfg(krate, &mut ctxt));
436+
}
437+
438+
krate = passes::track!(tcx, strip_hidden(krate, &mut ctxt));
439+
krate = passes::track!(tcx, strip_private(krate, &mut ctxt));
440+
441+
if show_coverage {
442+
krate = passes::track!(tcx, calculate_doc_coverage(krate, &mut ctxt));
443+
}
444+
445+
if !show_coverage {
446+
krate = passes::track!(tcx, strip_priv_imports(krate, &mut ctxt));
447+
(krate, LinkCollector { visited_links, ambiguous_links, .. }) =
448+
passes::track!(tcx, collect_intra_doc_links(krate, &mut ctxt));
449+
krate = passes::track!(tcx, propagate_stability(krate, &mut ctxt));
450+
krate = passes::track!(tcx, lint(krate, &mut ctxt));
450451
}
451452

452453
tcx.sess.time("check_lint_expectations", || tcx.check_expectations(Some(sym::rustdoc)));
453454

454455
krate =
455456
tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate, &render_options));
456457

457-
let mut collector =
458-
LinkCollector { cx: &mut ctxt, visited_links: visited, ambiguous_links: ambiguous };
458+
let mut collector = LinkCollector { cx: &mut ctxt, visited_links, ambiguous_links };
459459
collector.resolve_ambiguities();
460460

461461
tcx.dcx().abort_if_errors();

src/librustdoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(assert_matches)]
1010
#![feature(box_into_inner)]
1111
#![feature(box_patterns)]
12+
#![feature(decl_macro)]
1213
#![feature(file_buffered)]
1314
#![feature(formatting_options)]
1415
#![feature(if_let_guard)]

src/librustdoc/passes/calculate_doc_coverage.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ use tracing::debug;
1414
use crate::clean;
1515
use crate::core::DocContext;
1616
use crate::html::markdown::{ErrorCodes, find_testable_code};
17-
use crate::passes::Pass;
1817
use crate::passes::check_doc_test_visibility::{Tests, should_have_doc_example};
1918
use crate::visit::DocVisitor;
2019

21-
pub(crate) const CALCULATE_DOC_COVERAGE: Pass =
22-
Pass { name: "calculate-doc-coverage", run: Some(calculate_doc_coverage) };
23-
24-
fn calculate_doc_coverage(krate: clean::Crate, ctx: &mut DocContext<'_>) -> clean::Crate {
20+
pub(crate) fn calculate_doc_coverage(
21+
krate: clean::Crate,
22+
ctx: &mut DocContext<'_>,
23+
) -> clean::Crate {
2524
let mut calc = CoverageCalculator { items: Default::default(), ctx };
2625
calc.visit_crate(&krate);
2726

src/librustdoc/passes/check_doc_cfg.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ use rustc_hir::def_id::LocalDefId;
33
use rustc_middle::ty::TyCtxt;
44
use rustc_span::sym;
55

6-
use super::Pass;
76
use crate::clean::{Attributes, Crate, Item};
87
use crate::core::DocContext;
98
use crate::visit::DocVisitor;
109

11-
pub(crate) const CHECK_DOC_CFG: Pass = Pass { name: "check-doc-cfg", run: Some(check_doc_cfg) };
12-
1310
pub(crate) fn check_doc_cfg(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
1411
let mut checker = DocCfgChecker { cx };
1512
checker.visit_crate(&krate);

src/librustdoc/passes/check_doc_test_visibility.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@ use rustc_middle::lint::{LevelAndSource, LintLevelSource};
1010
use rustc_session::lint;
1111
use tracing::debug;
1212

13-
use super::Pass;
14-
use crate::clean;
1513
use crate::clean::utils::inherits_doc_hidden;
16-
use crate::clean::*;
14+
use crate::clean::{self, *};
1715
use crate::core::DocContext;
1816
use crate::html::markdown::{ErrorCodes, Ignore, LangString, MdRelLine, find_testable_code};
1917
use crate::visit::DocVisitor;
2018

21-
pub(crate) const CHECK_DOC_TEST_VISIBILITY: Pass =
22-
Pass { name: "check_doc_test_visibility", run: Some(check_doc_test_visibility) };
23-
2419
struct DocTestVisibilityLinter<'a, 'tcx> {
2520
cx: &'a mut DocContext<'tcx>,
2621
}

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ use crate::clean::{self, Crate, Item, ItemId, ItemLink, PrimitiveType};
3434
use crate::core::DocContext;
3535
use crate::html::markdown::{MarkdownLink, MarkdownLinkRange, markdown_links};
3636
use crate::lint::{BROKEN_INTRA_DOC_LINKS, PRIVATE_INTRA_DOC_LINKS};
37-
use crate::passes::Pass;
3837
use crate::visit::DocVisitor;
3938

40-
pub(crate) const COLLECT_INTRA_DOC_LINKS: Pass =
41-
Pass { name: "collect-intra-doc-links", run: None };
42-
4339
pub(crate) fn collect_intra_doc_links<'a, 'tcx>(
4440
krate: Crate,
4541
cx: &'a mut DocContext<'tcx>,

src/librustdoc/passes/collect_trait_impls.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ use rustc_middle::ty;
88
use rustc_span::symbol::sym;
99
use tracing::debug;
1010

11-
use super::Pass;
1211
use crate::clean::*;
1312
use crate::core::DocContext;
1413
use crate::formats::cache::Cache;
1514
use crate::visit::DocVisitor;
1615

17-
pub(crate) const COLLECT_TRAIT_IMPLS: Pass =
18-
Pass { name: "collect-trait-impls", run: Some(collect_trait_impls) };
19-
2016
pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate {
2117
let tcx = cx.tcx;
2218
// We need to check if there are errors before running this pass because it would crash when

src/librustdoc/passes/lint.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ mod html_tags;
77
mod redundant_explicit_links;
88
mod unescaped_backticks;
99

10-
use super::Pass;
1110
use crate::clean::*;
1211
use crate::core::DocContext;
1312
use crate::visit::DocVisitor;
1413

15-
pub(crate) const RUN_LINTS: Pass = Pass { name: "run-lints", run: Some(run_lints) };
16-
1714
struct Linter<'a, 'tcx> {
1815
cx: &'a mut DocContext<'tcx>,
1916
}
2017

21-
pub(crate) fn run_lints(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
18+
pub(crate) fn lint(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
2219
Linter { cx }.visit_crate(&krate);
2320
krate
2421
}

src/librustdoc/passes/mod.rs

Lines changed: 17 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,22 @@
1-
//! Contains information about "passes", used to modify crate information during the documentation
2-
//! process.
3-
4-
use self::Condition::*;
5-
use crate::clean;
6-
use crate::core::DocContext;
1+
//! The definitions of *passes* which transform crate information.
72
83
mod stripper;
94
pub(crate) use stripper::*;
105

11-
mod calculate_doc_coverage;
12-
mod check_doc_cfg;
13-
mod check_doc_test_visibility;
6+
pub(crate) mod calculate_doc_coverage;
7+
pub(crate) mod check_doc_cfg;
8+
pub(crate) mod check_doc_test_visibility;
149
pub(crate) mod collect_intra_doc_links;
15-
mod collect_trait_impls;
16-
mod lint;
17-
mod propagate_doc_cfg;
18-
mod propagate_stability;
19-
mod strip_aliased_non_local;
20-
mod strip_hidden;
21-
mod strip_priv_imports;
22-
mod strip_private;
23-
24-
/// A single pass over the cleaned documentation.
25-
///
26-
/// Runs in the compiler context, so it has access to types and traits and the like.
27-
#[derive(Copy, Clone)]
28-
pub(crate) struct Pass {
29-
pub(crate) name: &'static str,
30-
pub(crate) run: Option<fn(clean::Crate, &mut DocContext<'_>) -> clean::Crate>,
31-
}
32-
33-
/// In a list of passes, a pass that may or may not need to be run depending on options.
34-
#[derive(Copy, Clone)]
35-
pub(crate) struct ConditionalPass {
36-
pub(crate) pass: Pass,
37-
pub(crate) condition: Condition,
38-
}
39-
40-
/// How to decide whether to run a conditional pass.
41-
#[derive(Copy, Clone)]
42-
pub(crate) enum Condition {
43-
Always,
44-
/// When `--document-private-items` is passed.
45-
WhenDocumentPrivate,
46-
/// When `--document-private-items` is not passed.
47-
WhenNotDocumentPrivate,
48-
/// When `--document-hidden-items` is not passed.
49-
WhenNotDocumentHidden,
50-
}
51-
52-
/// The list of passes run by default.
53-
const DEFAULT_PASSES: &[ConditionalPass] = &[
54-
ConditionalPass::always(collect_trait_impls::COLLECT_TRAIT_IMPLS),
55-
ConditionalPass::always(check_doc_test_visibility::CHECK_DOC_TEST_VISIBILITY),
56-
ConditionalPass::always(check_doc_cfg::CHECK_DOC_CFG),
57-
ConditionalPass::always(strip_aliased_non_local::STRIP_ALIASED_NON_LOCAL),
58-
ConditionalPass::always(propagate_doc_cfg::PROPAGATE_DOC_CFG),
59-
ConditionalPass::new(strip_hidden::STRIP_HIDDEN, WhenNotDocumentHidden),
60-
ConditionalPass::new(strip_private::STRIP_PRIVATE, WhenNotDocumentPrivate),
61-
ConditionalPass::new(strip_priv_imports::STRIP_PRIV_IMPORTS, WhenDocumentPrivate),
62-
ConditionalPass::always(collect_intra_doc_links::COLLECT_INTRA_DOC_LINKS),
63-
ConditionalPass::always(propagate_stability::PROPAGATE_STABILITY),
64-
ConditionalPass::always(lint::RUN_LINTS),
65-
];
66-
67-
/// The list of default passes run when `--doc-coverage` is passed to rustdoc.
68-
const COVERAGE_PASSES: &[ConditionalPass] = &[
69-
ConditionalPass::new(strip_hidden::STRIP_HIDDEN, WhenNotDocumentHidden),
70-
ConditionalPass::new(strip_private::STRIP_PRIVATE, WhenNotDocumentPrivate),
71-
ConditionalPass::always(calculate_doc_coverage::CALCULATE_DOC_COVERAGE),
72-
];
73-
74-
impl ConditionalPass {
75-
pub(crate) const fn always(pass: Pass) -> Self {
76-
Self::new(pass, Always)
77-
}
78-
79-
pub(crate) const fn new(pass: Pass, condition: Condition) -> Self {
80-
ConditionalPass { pass, condition }
81-
}
82-
}
83-
84-
/// Returns the given default set of passes.
85-
pub(crate) fn defaults(show_coverage: bool) -> &'static [ConditionalPass] {
86-
if show_coverage { COVERAGE_PASSES } else { DEFAULT_PASSES }
87-
}
10+
pub(crate) mod collect_trait_impls;
11+
pub(crate) mod lint;
12+
pub(crate) mod propagate_doc_cfg;
13+
pub(crate) mod propagate_stability;
14+
pub(crate) mod strip_aliased_non_local;
15+
pub(crate) mod strip_hidden;
16+
pub(crate) mod strip_priv_imports;
17+
pub(crate) mod strip_private;
18+
19+
pub(crate) macro track($tcx:expr, $name:ident($( $args:tt )*)) {{
20+
tracing::debug!("running pass `{}`", stringify!($name));
21+
$tcx.sess.time(stringify!($name), || self::$name::$name($( $args )*))
22+
}}

src/librustdoc/passes/propagate_doc_cfg.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ use crate::clean::inline::{load_attrs, merge_attrs};
99
use crate::clean::{CfgInfo, Crate, Item, ItemKind};
1010
use crate::core::DocContext;
1111
use crate::fold::DocFolder;
12-
use crate::passes::Pass;
13-
14-
pub(crate) const PROPAGATE_DOC_CFG: Pass =
15-
Pass { name: "propagate-doc-cfg", run: Some(propagate_doc_cfg) };
1612

1713
pub(crate) fn propagate_doc_cfg(cr: Crate, cx: &mut DocContext<'_>) -> Crate {
1814
if cx.tcx.features().doc_cfg() {

0 commit comments

Comments
 (0)