Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ pub(crate) fn build_impl(
return;
}

let document_hidden = cx.render_options.document_hidden;
let document_hidden = cx.document_hidden();
let (trait_items, generics) = match impl_item {
Some(impl_) => (
impl_
Expand Down
17 changes: 8 additions & 9 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
items.extend(doc.foreigns.iter().map(|(item, renamed, import_id)| {
let item = clean_maybe_renamed_foreign_item(cx, item, *renamed, *import_id);
if let Some(name) = item.name
&& (cx.render_options.document_hidden || !item.is_doc_hidden())
&& (cx.document_hidden() || !item.is_doc_hidden())
{
inserted.insert((item.type_(), name));
}
Expand All @@ -82,7 +82,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
return None;
}
let item = clean_doc_module(x, cx);
if !cx.render_options.document_hidden && item.is_doc_hidden() {
if !cx.document_hidden() && item.is_doc_hidden() {
// Hidden modules are stripped at a later stage.
// If a hidden module has the same name as a visible one, we want
// to keep both of them around.
Expand All @@ -104,7 +104,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
let v = clean_maybe_renamed_item(cx, item, *renamed, import_ids);
for item in &v {
if let Some(name) = item.name
&& (cx.render_options.document_hidden || !item.is_doc_hidden())
&& (cx.document_hidden() || !item.is_doc_hidden())
{
inserted.insert((item.type_(), name));
}
Expand Down Expand Up @@ -203,7 +203,7 @@ fn generate_item_with_correct_attrs(
.get_word_attr(sym::inline)
.is_some()
|| (is_glob_import(cx.tcx, import_id)
&& (cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id)));
&& (cx.document_hidden() || !cx.tcx.is_doc_hidden(def_id)));
attrs.extend(get_all_import_attributes(cx, import_id, def_id, is_inline));
is_inline = is_inline || import_is_inline;
}
Expand Down Expand Up @@ -1581,9 +1581,9 @@ fn first_non_private<'tcx>(
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res {
continue;
}
if (cx.render_options.document_hidden ||
if (cx.document_hidden() ||
!cx.tcx.is_doc_hidden(use_def_id)) &&
// We never check for "cx.render_options.document_private"
// We never check for "cx.document_private()"
// because if a re-export is not fully public, it's never
// documented.
cx.tcx.local_visibility(local_use_def_id).is_public()
Expand Down Expand Up @@ -2628,7 +2628,7 @@ fn get_all_import_attributes<'hir>(
attrs = import_attrs.iter().map(|attr| (Cow::Borrowed(attr), Some(def_id))).collect();
first = false;
// We don't add attributes of an intermediate re-export if it has `#[doc(hidden)]`.
} else if cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id) {
} else if cx.document_hidden() || !cx.tcx.is_doc_hidden(def_id) {
add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id));
}
}
Expand Down Expand Up @@ -3065,8 +3065,7 @@ fn clean_use_statement_inner<'tcx>(
// #[doc(no_inline)] attribute is present.
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
let mut denied = cx.is_json_output()
|| !(visibility.is_public()
|| (cx.render_options.document_private && is_visible_from_parent_mod))
|| !(visibility.is_public() || (cx.document_private() && is_visible_from_parent_mod))
|| pub_underscore
|| attrs.iter().any(|a| {
a.has_name(sym::doc)
Expand Down
44 changes: 26 additions & 18 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rustc_span::symbol::sym;
use tracing::{debug, info};

use crate::clean::inline::build_trait;
use crate::clean::{self, ItemId};
use crate::clean::{self, ItemId, NestedAttributesExt};
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
use crate::formats::cache::Cache;
use crate::html::macro_expansion::{ExpandedCode, source_macro_expansion};
Expand Down Expand Up @@ -61,8 +61,6 @@ pub(crate) struct DocContext<'tcx> {
// FIXME(eddyb) make this a `ty::TraitRef<'tcx>` set.
pub(crate) generated_synthetics: FxHashSet<(Ty<'tcx>, DefId)>,
pub(crate) auto_traits: Vec<DefId>,
/// The options given to rustdoc that could be relevant to a pass.
pub(crate) render_options: RenderOptions,
/// This same cache is used throughout rustdoc, including in [`crate::html::render`].
pub(crate) cache: Cache,
/// Used by [`clean::inline`] to tell if an item has already been inlined.
Expand Down Expand Up @@ -138,6 +136,16 @@ impl<'tcx> DocContext<'tcx> {
pub(crate) fn is_json_output(&self) -> bool {
self.output_format.is_json() && !self.show_coverage
}

/// If `--document-private-items` was passed to rustdoc.
pub(crate) fn document_private(&self) -> bool {
self.cache.document_private
}

/// If `--document-hidden-items` was passed to rustdoc.
pub(crate) fn document_hidden(&self) -> bool {
self.cache.document_hidden
}
}

/// Creates a new `DiagCtxt` that can be used to emit warnings and errors.
Expand Down Expand Up @@ -333,7 +341,7 @@ pub(crate) fn create_config(
pub(crate) fn run_global_ctxt(
tcx: TyCtxt<'_>,
show_coverage: bool,
render_options: RenderOptions,
mut render_options: RenderOptions,
output_format: OutputFormat,
) -> (clean::Crate, RenderOptions, Cache, FxHashMap<rustc_span::BytePos, Vec<ExpandedCode>>) {
// Certain queries assume that some checks were run elsewhere
Expand Down Expand Up @@ -366,6 +374,14 @@ pub(crate) fn run_global_ctxt(
let auto_traits =
tcx.visible_traits().filter(|&trait_def_id| tcx.trait_is_auto(trait_def_id)).collect();

// Remnence of processing crate attributes for passes to run.
// To be removed in https://github.com/rust-lang/rust/pull/146495.
if clean::hir_attr_lists(tcx.get_all_attrs(rustc_hir::def_id::CRATE_DEF_ID), sym::doc)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CC @fmease. I'd rather this didn't need to wait for the FCP in #146495, so I've moved the code here. As noted, it has no tests 👻

.has_word(sym::document_private_items)
{
render_options.document_private = true;
}

let mut ctxt = DocContext {
tcx,
param_env: ParamEnv::empty(),
Expand All @@ -379,7 +395,6 @@ pub(crate) fn run_global_ctxt(
cache: Cache::new(render_options.document_private, render_options.document_hidden),
inlined: FxHashSet::default(),
output_format,
render_options,
show_coverage,
};

Expand Down Expand Up @@ -416,14 +431,6 @@ pub(crate) fn run_global_ctxt(
);
}

// Process all of the crate attributes, extracting plugin metadata along
// with the passes which we are supposed to run.
for attr in krate.module.attrs.lists(sym::doc) {
if attr.is_word() && attr.has_name(sym::document_private_items) {
ctxt.render_options.document_private = true;
}
}

info!("Executing passes");

let mut visited = FxHashMap::default();
Expand All @@ -432,9 +439,9 @@ pub(crate) fn run_global_ctxt(
for p in passes::defaults(show_coverage) {
let run = match p.condition {
Always => true,
WhenDocumentPrivate => ctxt.render_options.document_private,
WhenNotDocumentPrivate => !ctxt.render_options.document_private,
WhenNotDocumentHidden => !ctxt.render_options.document_hidden,
WhenDocumentPrivate => ctxt.document_private(),
WhenNotDocumentPrivate => !ctxt.document_private(),
WhenNotDocumentHidden => !ctxt.document_hidden(),
};
if run {
debug!("running pass {}", p.pass.name);
Expand All @@ -452,15 +459,16 @@ pub(crate) fn run_global_ctxt(

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

krate = tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate));
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();

tcx.dcx().abort_if_errors();

(krate, ctxt.render_options, ctxt.cache, expanded_macros)
(krate, render_options, ctxt.cache, expanded_macros)
}

/// Due to <https://github.com/rust-lang/rust/pull/73566>,
Expand Down
8 changes: 6 additions & 2 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tracing::debug;

use crate::clean::types::ExternalLocation;
use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
use crate::config::RenderOptions;
use crate::core::DocContext;
use crate::fold::DocFolder;
use crate::formats::Impl;
Expand Down Expand Up @@ -156,15 +157,18 @@ impl Cache {

/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
/// in `krate` due to the data being moved into the `Cache`.
pub(crate) fn populate(cx: &mut DocContext<'_>, mut krate: clean::Crate) -> clean::Crate {
pub(crate) fn populate(
cx: &mut DocContext<'_>,
mut krate: clean::Crate,
render_options: &RenderOptions,
) -> clean::Crate {
let tcx = cx.tcx;

// Crawl the crate to build various caches used for the output
debug!(?cx.cache.crate_version);
assert!(cx.external_traits.is_empty());
cx.cache.traits = mem::take(&mut krate.external_traits);

let render_options = &cx.render_options;
let extern_url_takes_precedence = render_options.extern_html_root_takes_precedence;
let dst = &render_options.output;

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/check_doc_test_visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
return false;
}

if (!cx.render_options.document_hidden
if (!cx.document_hidden()
&& (cx.tcx.is_doc_hidden(def_id.to_def_id()) || inherits_doc_hidden(cx.tcx, def_id, None)))
|| cx.tcx.def_span(def_id.to_def_id()).in_derive_expansion()
{
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ fn preprocessed_markdown_links(s: &str) -> Vec<PreprocessedMarkdownLink> {
impl LinkCollector<'_, '_> {
#[instrument(level = "debug", skip_all)]
fn resolve_links(&mut self, item: &Item) {
if !self.cx.render_options.document_private
if !self.cx.document_private()
&& let Some(def_id) = item.item_id.as_def_id()
&& let Some(def_id) = def_id.as_local()
&& !self.cx.tcx.effective_visibilities(()).is_exported(def_id)
Expand Down Expand Up @@ -2400,7 +2400,7 @@ fn privacy_error(cx: &DocContext<'_>, diag_info: &DiagnosticInfo<'_>, path_str:
diag.span_label(sp, "this item is private");
}

let note_msg = if cx.render_options.document_private {
let note_msg = if cx.document_private() {
"this link resolves only because you passed `--document-private-items`, but will break without"
} else {
"this link will resolve properly if you pass `--document-private-items`"
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/passes/lint/redundant_explicit_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ fn check_redundant_explicit_link_for_did(
return;
};

let is_hidden = !cx.render_options.document_hidden
let is_hidden = !cx.document_hidden()
&& (item.is_doc_hidden() || inherits_doc_hidden(cx.tcx, local_item_id, None));
if is_hidden {
return;
}
let is_private = !cx.render_options.document_private
&& !cx.cache.effective_visibilities.is_directly_public(cx.tcx, did);
let is_private =
!cx.document_private() && !cx.cache.effective_visibilities.is_directly_public(cx.tcx, did);
if is_private {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes/strip_hidden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clea
retained: &retained,
cache: &cx.cache,
is_json_output,
document_private: cx.render_options.document_private,
document_hidden: cx.render_options.document_hidden,
document_private: cx.document_private(),
document_hidden: cx.document_hidden(),
};
stripper.fold_crate(krate)
}
Expand Down
8 changes: 2 additions & 6 deletions src/librustdoc/passes/strip_priv_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ pub(crate) const STRIP_PRIV_IMPORTS: Pass = Pass {

pub(crate) fn strip_priv_imports(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
let is_json_output = cx.is_json_output();
ImportStripper {
tcx: cx.tcx,
is_json_output,
document_hidden: cx.render_options.document_hidden,
}
.fold_crate(krate)
ImportStripper { tcx: cx.tcx, is_json_output, document_hidden: cx.document_hidden() }
.fold_crate(krate)
}
13 changes: 5 additions & 8 deletions src/librustdoc/passes/strip_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) ->
is_json_output,
tcx: cx.tcx,
};
krate = ImportStripper {
tcx: cx.tcx,
is_json_output,
document_hidden: cx.render_options.document_hidden,
}
.fold_crate(stripper.fold_crate(krate));
krate =
ImportStripper { tcx: cx.tcx, is_json_output, document_hidden: cx.document_hidden() }
.fold_crate(stripper.fold_crate(krate));
}

// strip all impls referencing private items
Expand All @@ -43,8 +40,8 @@ pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) ->
retained: &retained,
cache: &cx.cache,
is_json_output,
document_private: cx.render_options.document_private,
document_hidden: cx.render_options.document_hidden,
document_private: cx.document_private(),
document_hidden: cx.document_hidden(),
};
stripper.fold_crate(krate)
}
4 changes: 2 additions & 2 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
return false;
};

let document_hidden = self.cx.render_options.document_hidden;
let document_hidden = self.cx.document_hidden();
let use_attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(def_id));
// Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
let is_no_inline = hir_attr_lists(use_attrs, sym::doc).has_word(sym::no_inline)
Expand Down Expand Up @@ -351,7 +351,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
import_def_id: LocalDefId,
target_def_id: LocalDefId,
) -> bool {
if self.cx.render_options.document_hidden {
if self.cx.document_hidden() {
return true;
}
let tcx = self.cx.tcx;
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/visit_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ impl RustdocEffectiveVisibilities {

pub(crate) fn lib_embargo_visit_item(cx: &mut DocContext<'_>, def_id: DefId) {
assert!(!def_id.is_local());
let document_hidden = cx.document_hidden();
LibEmbargoVisitor {
tcx: cx.tcx,
extern_public: &mut cx.cache.effective_visibilities.extern_public,
visited_mods: Default::default(),
document_hidden: cx.render_options.document_hidden,
document_hidden,
}
.visit_item(def_id)
}
Expand Down
Loading