Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/librustdoc/calculate_doc_coverage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! Calculates information used for the --show-coverage flag.
//! Calculates information used for the `--show-coverage` flag.
//!
//! More specifically, it counts the number of items with documentation, ones with
//! "examples" (i.e., non-ignored Rust code blocks) and various totals.

use std::collections::BTreeMap;
use std::fs::{File, create_dir_all};
Expand All @@ -17,7 +20,7 @@ use crate::core::DocContext;
use crate::docfs::PathError;
use crate::error::Error;
use crate::html::markdown::{ErrorCodes, find_testable_code};
use crate::passes::{Tests, should_have_doc_example};
use crate::passes::check_doc_test_visibility::{Tests, should_have_doc_example};
use crate::visit::DocVisitor;
use crate::{clean, try_err};

Expand Down
34 changes: 0 additions & 34 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use crate::externalfiles::ExternalHtml;
use crate::html::markdown::IdMap;
use crate::html::render::StylePath;
use crate::html::static_files;
use crate::passes::{self, Condition};
use crate::scrape_examples::{AllCallLocations, ScrapeExamplesOptions};
use crate::{html, opts, theme};

Expand Down Expand Up @@ -428,40 +427,7 @@ impl Options {
// check for deprecated options
check_deprecated_options(matches, dcx);

if matches.opt_strs("passes") == ["list"] {
println!("Available passes for running rustdoc:");
for pass in passes::PASSES {
println!("{:>20} - {}", pass.name, pass.description);
}
println!("\nDefault passes for rustdoc:");
for p in passes::DEFAULT_PASSES {
print!("{:>20}", p.pass.name);
println_condition(p.condition);
}

if nightly_options::match_is_nightly_build(matches) {
println!("\nPasses run with `--show-coverage`:");
for p in passes::COVERAGE_PASSES {
print!("{:>20}", p.pass.name);
println_condition(p.condition);
}
}

fn println_condition(condition: Condition) {
use Condition::*;
match condition {
Always => println!(),
WhenDocumentPrivate => println!(" (when --document-private-items)"),
WhenNotDocumentPrivate => println!(" (when not --document-private-items)"),
WhenNotDocumentHidden => println!(" (when not --document-hidden-items)"),
}
}

return None;
}

let should_test = matches.opt_present("test");

let show_coverage = matches.opt_present("show-coverage");
let output_format_s = matches.opt_str("output-format");
let output_format = match output_format_s.as_deref() {
Expand Down
35 changes: 4 additions & 31 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ pub(crate) use rustc_session::config::{Options, UnstableOptions};
use rustc_span::source_map;
use rustc_span::symbol::sym;
use rustc_structures::CrateType;
use tracing::{debug, info};
use tracing::debug;

use crate::clean::inline::build_trait;
use crate::clean::{self, ItemId};
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
use crate::formats::cache::Cache;
use crate::html::macro_expansion::{ExpandedCode, source_macro_expansion};
use crate::passes;
use crate::passes::Condition::*;
use crate::passes::collect_intra_doc_links::LinkCollector;

pub(crate) struct DocContext<'tcx> {
pub(crate) tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -428,31 +426,8 @@ pub(crate) fn run_global_ctxt(
);
}

info!("Executing passes");

let mut visited = FxHashMap::default();
let mut ambiguous = FxIndexMap::default();

for p in passes::defaults(show_coverage) {
let run = match p.condition {
Always => true,
WhenDocumentPrivate => ctxt.document_private(),
WhenNotDocumentPrivate => !ctxt.document_private(),
WhenNotDocumentHidden => !ctxt.document_hidden(),
};
if run {
debug!("running pass {}", p.pass.name);
if let Some(run_fn) = p.pass.run {
krate = tcx.sess.time(p.pass.name, || run_fn(krate, &mut ctxt));
} else {
let (k, LinkCollector { visited_links, ambiguous_links, .. }) =
passes::collect_intra_doc_links::collect_intra_doc_links(krate, &mut ctxt);
krate = k;
visited = visited_links;
ambiguous = ambiguous_links;
}
}
}
let store;
(krate, store) = passes::run(krate, &mut ctxt, show_coverage);

if show_coverage
&& let Err(error) = crate::calculate_doc_coverage::run(&krate, &mut ctxt, &render_options)
Expand All @@ -466,9 +441,7 @@ pub(crate) fn run_global_ctxt(
krate =
tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate, &render_options));

let mut collector =
LinkCollector { cx: &mut ctxt, visited_links: visited, ambiguous_links: ambiguous };
collector.resolve_ambiguities();
passes::finalize(&mut ctxt, store);

tcx.dcx().abort_if_errors();

Expand Down
12 changes: 2 additions & 10 deletions src/librustdoc/passes/check_doc_test_visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,19 @@ use rustc_macros::Diagnostic;
use rustc_middle::lint::LintLevelSource;
use tracing::debug;

use super::Pass;
use crate::clean;
use crate::clean::utils::inherits_doc_hidden;
use crate::clean::*;
use crate::clean::{self, *};
use crate::core::DocContext;
use crate::html::markdown::{
CodeLineMapping, ErrorCodes, Ignore, LangString, MdRelLine, find_testable_code,
};
use crate::visit::DocVisitor;

pub(crate) const CHECK_DOC_TEST_VISIBILITY: Pass = Pass {
name: "check_doc_test_visibility",
run: Some(check_doc_test_visibility),
description: "run various visibility-related lints on doctests",
};

struct DocTestVisibilityLinter<'a, 'tcx> {
cx: &'a mut DocContext<'tcx>,
}

pub(crate) fn check_doc_test_visibility(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
pub(super) fn check_doc_test_visibility(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
let mut coll = DocTestVisibilityLinter { cx };
coll.visit_crate(&krate);
krate
Expand Down
52 changes: 27 additions & 25 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module implements [RFC 1946]: Intra-rustdoc-links
//! Resolves intra-doc links ([RFC 1946]).
//!
//! [RFC 1946]: https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md
//! [RFC 1946]: https://rust-lang.github.io/rfcs/1946-intra-rustdoc-links.html

use std::borrow::Cow;
use std::fmt::Display;
Expand Down Expand Up @@ -36,23 +36,19 @@ use crate::clean::{self, Crate, Item, ItemId, ItemLink, PrimitiveType, reexport_
use crate::core::DocContext;
use crate::html::markdown::{MarkdownLink, MarkdownLinkRange, markdown_links};
use crate::lint::{BROKEN_INTRA_DOC_LINKS, PRIVATE_INTRA_DOC_LINKS};
use crate::passes::Pass;
use crate::visit::DocVisitor;

pub(crate) const COLLECT_INTRA_DOC_LINKS: Pass =
Pass { name: "collect-intra-doc-links", run: None, description: "resolves intra-doc links" };

pub(crate) fn collect_intra_doc_links<'a, 'tcx>(
pub(super) fn collect_intra_doc_links(
krate: Crate,
cx: &'a mut DocContext<'tcx>,
) -> (Crate, LinkCollector<'a, 'tcx>) {
let mut collector = LinkCollector {
cx,
visited_links: FxHashMap::default(),
ambiguous_links: FxIndexMap::default(),
};
cx: &mut DocContext<'_>,
) -> (Crate, LinkCollection) {
let mut collector = LinkCollector { cx, links: LinkCollection::default() };
collector.visit_crate(&krate);
(krate, collector)
(krate, collector.links)
}

pub(super) fn resolve_ambiguous_links(links: LinkCollection, cx: &mut DocContext<'_>) {
LinkCollector { cx, links }.resolve_ambiguities();
}

fn filter_assoc_items_by_name_and_namespace(
Expand Down Expand Up @@ -252,11 +248,16 @@ impl OwnedDiagnosticInfo {
}
}

pub(crate) struct LinkCollector<'a, 'tcx> {
pub(crate) cx: &'a mut DocContext<'tcx>,
struct LinkCollector<'a, 'tcx> {
cx: &'a mut DocContext<'tcx>,
links: LinkCollection,
}

#[derive(Default)]
pub(super) struct LinkCollection {
/// Cache the resolved links so we can avoid resolving (and emitting errors for) the same link.
/// The link will be `None` if it could not be resolved (i.e. the error was cached).
pub(crate) visited_links: FxHashMap<ResolutionInfo, Option<(Res, Option<UrlFragment>)>>,
visited: FxHashMap<ResolutionInfo, Option<(Res, Option<UrlFragment>)>>,
/// According to `rustc_resolve`, these links are ambiguous.
///
/// However, we cannot link to an item that has been stripped from the documentation. If all
Expand All @@ -267,7 +268,7 @@ pub(crate) struct LinkCollector<'a, 'tcx> {
/// We could get correct results by simply delaying everything. This would have fewer happy
/// codepaths, but we want to distinguish different kinds of error conditions, and this is easy
/// to do by resolving links as soon as possible.
pub(crate) ambiguous_links: FxIndexMap<(ItemId, String), Vec<AmbiguousLinks>>,
ambiguous: FxIndexMap<(ItemId, String), Vec<AmbiguousLinks>>,
}

pub(crate) struct AmbiguousLinks {
Expand Down Expand Up @@ -1216,7 +1217,8 @@ impl LinkCollector<'_, '_> {
resolved,
};

self.ambiguous_links
self.links
.ambiguous
.entry((item.item_id, path_str.to_string()))
.or_default()
.push(links);
Expand Down Expand Up @@ -1272,8 +1274,8 @@ impl LinkCollector<'_, '_> {
|| !did.is_local()
}

pub(crate) fn resolve_ambiguities(&mut self) {
let mut ambiguous_links = mem::take(&mut self.ambiguous_links);
fn resolve_ambiguities(&mut self) {
let mut ambiguous_links = mem::take(&mut self.links.ambiguous);
for ((item_id, path_str), info_items) in ambiguous_links.iter_mut() {
for info in info_items {
info.resolved.retain(|(res, _)| match res {
Expand Down Expand Up @@ -1523,7 +1525,7 @@ impl LinkCollector<'_, '_> {
// which we want in some cases but not in others.
cache_errors: bool,
) -> Option<Vec<(Res, Option<UrlFragment>)>> {
if let Some(res) = self.visited_links.get(&key)
if let Some(res) = self.links.visited.get(&key)
&& (res.is_some() || cache_errors)
{
return res.clone().map(|r| vec![r]);
Expand Down Expand Up @@ -1570,9 +1572,9 @@ impl LinkCollector<'_, '_> {
out.push((res, fragment));
}
if let [r] = out.as_slice() {
self.visited_links.insert(key, Some(r.clone()));
self.links.visited.insert(key, Some(r.clone()));
} else if cache_errors {
self.visited_links.insert(key, None);
self.links.visited.insert(key, None);
}
Some(out)
}
Expand Down
16 changes: 5 additions & 11 deletions src/librustdoc/passes/collect_trait_impls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Collects trait impls for each item in the crate. For example, if a crate
//! defines a struct that implements a trait, this pass will note that the
//! struct implements that trait.
//! Collects trait impls for each item in the crate.
//!
//! For example, if a crate defines a struct that implements a trait,
//! this pass will note that the struct implements that trait.

use rustc_data_structures::fx::FxHashSet;
use rustc_errors::FatalError;
Expand All @@ -11,19 +12,12 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::kw;
use tracing::debug;

use super::Pass;
use crate::clean::*;
use crate::core::DocContext;
use crate::formats::cache::Cache;
use crate::visit::DocVisitor;

pub(crate) const COLLECT_TRAIT_IMPLS: Pass = Pass {
name: "collect-trait-impls",
run: Some(collect_trait_impls),
description: "retrieves trait impls for items in the crate",
};

pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate {
pub(super) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate {
let tcx = cx.tcx;
// We need to check if there are errors before running this pass because it would crash when
// we try to get auto and blanket implementations.
Expand Down
9 changes: 2 additions & 7 deletions src/librustdoc/passes/lint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Runs several rustdoc lints, consolidating them into a single pass for
//! efficiency and simplicity.
//! Runs several rustdoc lints, consolidating them into a single pass for efficiency and simplicity.

mod bare_urls;
mod check_code_block_syntax;
Expand All @@ -8,19 +7,15 @@ mod html_tags;
mod redundant_explicit_links;
mod unescaped_backticks;

use super::Pass;
use crate::clean::*;
use crate::core::DocContext;
use crate::visit::DocVisitor;

pub(crate) const RUN_LINTS: Pass =
Pass { name: "run-lints", run: Some(run_lints), description: "runs some of rustdoc's lints" };

struct Linter<'a, 'tcx> {
cx: &'a mut DocContext<'tcx>,
}

pub(crate) fn run_lints(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
pub(super) fn lint(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
Linter { cx }.visit_crate(&krate);
krate
}
Expand Down
Loading
Loading