Skip to content

Commit abb2d75

Browse files
committed
rustdoc: Don't pass RenderOptions to DocContext
`RenderOptions` is full of HTML specific fields. The only ones that `DocContext` needs are `document_private` and `document_hidden`, which are accessable via `Cache` anyway. Part of a larger campeign against `RenderOption`: https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/Using.20.60RenderOptions.60.20in.20less.20places.2E/with/545705812
1 parent f464759 commit abb2d75

File tree

12 files changed

+60
-55
lines changed

12 files changed

+60
-55
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ pub(crate) fn build_impl(
497497
return;
498498
}
499499

500-
let document_hidden = cx.render_options.document_hidden;
500+
let document_hidden = cx.document_hidden();
501501
let (trait_items, generics) = match impl_item {
502502
Some(impl_) => (
503503
impl_

src/librustdoc/clean/mod.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
7171
items.extend(doc.foreigns.iter().map(|(item, renamed, import_id)| {
7272
let item = clean_maybe_renamed_foreign_item(cx, item, *renamed, *import_id);
7373
if let Some(name) = item.name
74-
&& (cx.render_options.document_hidden || !item.is_doc_hidden())
74+
&& (cx.document_hidden() || !item.is_doc_hidden())
7575
{
7676
inserted.insert((item.type_(), name));
7777
}
@@ -82,7 +82,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
8282
return None;
8383
}
8484
let item = clean_doc_module(x, cx);
85-
if !cx.render_options.document_hidden && item.is_doc_hidden() {
85+
if !cx.document_hidden() && item.is_doc_hidden() {
8686
// Hidden modules are stripped at a later stage.
8787
// If a hidden module has the same name as a visible one, we want
8888
// to keep both of them around.
@@ -104,7 +104,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
104104
let v = clean_maybe_renamed_item(cx, item, *renamed, import_ids);
105105
for item in &v {
106106
if let Some(name) = item.name
107-
&& (cx.render_options.document_hidden || !item.is_doc_hidden())
107+
&& (cx.document_hidden() || !item.is_doc_hidden())
108108
{
109109
inserted.insert((item.type_(), name));
110110
}
@@ -203,7 +203,7 @@ fn generate_item_with_correct_attrs(
203203
.get_word_attr(sym::inline)
204204
.is_some()
205205
|| (is_glob_import(cx.tcx, import_id)
206-
&& (cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id)));
206+
&& (cx.document_hidden() || !cx.tcx.is_doc_hidden(def_id)));
207207
attrs.extend(get_all_import_attributes(cx, import_id, def_id, is_inline));
208208
is_inline = is_inline || import_is_inline;
209209
}
@@ -1581,9 +1581,9 @@ fn first_non_private<'tcx>(
15811581
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res {
15821582
continue;
15831583
}
1584-
if (cx.render_options.document_hidden ||
1584+
if (cx.document_hidden() ||
15851585
!cx.tcx.is_doc_hidden(use_def_id)) &&
1586-
// We never check for "cx.render_options.document_private"
1586+
// We never check for "cx.document_private()"
15871587
// because if a re-export is not fully public, it's never
15881588
// documented.
15891589
cx.tcx.local_visibility(local_use_def_id).is_public()
@@ -2628,7 +2628,7 @@ fn get_all_import_attributes<'hir>(
26282628
attrs = import_attrs.iter().map(|attr| (Cow::Borrowed(attr), Some(def_id))).collect();
26292629
first = false;
26302630
// We don't add attributes of an intermediate re-export if it has `#[doc(hidden)]`.
2631-
} else if cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id) {
2631+
} else if cx.document_hidden() || !cx.tcx.is_doc_hidden(def_id) {
26322632
add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id));
26332633
}
26342634
}
@@ -3065,8 +3065,7 @@ fn clean_use_statement_inner<'tcx>(
30653065
// #[doc(no_inline)] attribute is present.
30663066
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
30673067
let mut denied = cx.is_json_output()
3068-
|| !(visibility.is_public()
3069-
|| (cx.render_options.document_private && is_visible_from_parent_mod))
3068+
|| !(visibility.is_public() || (cx.document_private() && is_visible_from_parent_mod))
30703069
|| pub_underscore
30713070
|| attrs.iter().any(|a| {
30723071
a.has_name(sym::doc)

src/librustdoc/core.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_span::symbol::sym;
2828
use tracing::{debug, info};
2929

3030
use crate::clean::inline::build_trait;
31-
use crate::clean::{self, ItemId};
31+
use crate::clean::{self, ItemId, NestedAttributesExt};
3232
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
3333
use crate::formats::cache::Cache;
3434
use crate::html::macro_expansion::{ExpandedCode, source_macro_expansion};
@@ -61,8 +61,6 @@ pub(crate) struct DocContext<'tcx> {
6161
// FIXME(eddyb) make this a `ty::TraitRef<'tcx>` set.
6262
pub(crate) generated_synthetics: FxHashSet<(Ty<'tcx>, DefId)>,
6363
pub(crate) auto_traits: Vec<DefId>,
64-
/// The options given to rustdoc that could be relevant to a pass.
65-
pub(crate) render_options: RenderOptions,
6664
/// This same cache is used throughout rustdoc, including in [`crate::html::render`].
6765
pub(crate) cache: Cache,
6866
/// Used by [`clean::inline`] to tell if an item has already been inlined.
@@ -138,6 +136,16 @@ impl<'tcx> DocContext<'tcx> {
138136
pub(crate) fn is_json_output(&self) -> bool {
139137
self.output_format.is_json() && !self.show_coverage
140138
}
139+
140+
/// If `--document-private-items` was passed to rustdoc.
141+
pub(crate) fn document_private(&self) -> bool {
142+
self.cache.document_private
143+
}
144+
145+
/// If `--document-hidden-items` was passed to rustdoc.
146+
pub(crate) fn document_hidden(&self) -> bool {
147+
self.cache.document_hidden
148+
}
141149
}
142150

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

377+
// Remnence of processing crate attributes for passes to run.
378+
// To be removed in https://github.com/rust-lang/rust/pull/146495.
379+
if clean::hir_attr_lists(tcx.get_all_attrs(rustc_hir::def_id::CRATE_DEF_ID), sym::doc)
380+
.has_word(sym::document_private_items)
381+
{
382+
render_options.document_private = true;
383+
}
384+
369385
let mut ctxt = DocContext {
370386
tcx,
371387
param_env: ParamEnv::empty(),
@@ -379,7 +395,6 @@ pub(crate) fn run_global_ctxt(
379395
cache: Cache::new(render_options.document_private, render_options.document_hidden),
380396
inlined: FxHashSet::default(),
381397
output_format,
382-
render_options,
383398
show_coverage,
384399
};
385400

@@ -416,14 +431,6 @@ pub(crate) fn run_global_ctxt(
416431
);
417432
}
418433

419-
// Process all of the crate attributes, extracting plugin metadata along
420-
// with the passes which we are supposed to run.
421-
for attr in krate.module.attrs.lists(sym::doc) {
422-
if attr.is_word() && attr.has_name(sym::document_private_items) {
423-
ctxt.render_options.document_private = true;
424-
}
425-
}
426-
427434
info!("Executing passes");
428435

429436
let mut visited = FxHashMap::default();
@@ -432,9 +439,9 @@ pub(crate) fn run_global_ctxt(
432439
for p in passes::defaults(show_coverage) {
433440
let run = match p.condition {
434441
Always => true,
435-
WhenDocumentPrivate => ctxt.render_options.document_private,
436-
WhenNotDocumentPrivate => !ctxt.render_options.document_private,
437-
WhenNotDocumentHidden => !ctxt.render_options.document_hidden,
442+
WhenDocumentPrivate => ctxt.document_private(),
443+
WhenNotDocumentPrivate => !ctxt.document_private(),
444+
WhenNotDocumentHidden => !ctxt.document_hidden(),
438445
};
439446
if run {
440447
debug!("running pass {}", p.pass.name);
@@ -452,15 +459,16 @@ pub(crate) fn run_global_ctxt(
452459

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

455-
krate = tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate));
462+
krate =
463+
tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate, &render_options));
456464

457465
let mut collector =
458466
LinkCollector { cx: &mut ctxt, visited_links: visited, ambiguous_links: ambiguous };
459467
collector.resolve_ambiguities();
460468

461469
tcx.dcx().abort_if_errors();
462470

463-
(krate, ctxt.render_options, ctxt.cache, expanded_macros)
471+
(krate, render_options, ctxt.cache, expanded_macros)
464472
}
465473

466474
/// Due to <https://github.com/rust-lang/rust/pull/73566>,

src/librustdoc/formats/cache.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use tracing::debug;
1010

1111
use crate::clean::types::ExternalLocation;
1212
use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
13+
use crate::config::RenderOptions;
1314
use crate::core::DocContext;
1415
use crate::fold::DocFolder;
1516
use crate::formats::Impl;
@@ -156,15 +157,18 @@ impl Cache {
156157

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

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

167-
let render_options = &cx.render_options;
168172
let extern_url_takes_precedence = render_options.extern_html_root_takes_precedence;
169173
let dst = &render_options.output;
170174

src/librustdoc/passes/check_doc_test_visibility.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
102102
return false;
103103
}
104104

105-
if (!cx.render_options.document_hidden
105+
if (!cx.document_hidden()
106106
&& (cx.tcx.is_doc_hidden(def_id.to_def_id()) || inherits_doc_hidden(cx.tcx, def_id, None)))
107107
|| cx.tcx.def_span(def_id.to_def_id()).in_derive_expansion()
108108
{

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ fn preprocessed_markdown_links(s: &str) -> Vec<PreprocessedMarkdownLink> {
10701070
impl LinkCollector<'_, '_> {
10711071
#[instrument(level = "debug", skip_all)]
10721072
fn resolve_links(&mut self, item: &Item) {
1073-
if !self.cx.render_options.document_private
1073+
if !self.cx.document_private()
10741074
&& let Some(def_id) = item.item_id.as_def_id()
10751075
&& let Some(def_id) = def_id.as_local()
10761076
&& !self.cx.tcx.effective_visibilities(()).is_exported(def_id)
@@ -2400,7 +2400,7 @@ fn privacy_error(cx: &DocContext<'_>, diag_info: &DiagnosticInfo<'_>, path_str:
24002400
diag.span_label(sp, "this item is private");
24012401
}
24022402

2403-
let note_msg = if cx.render_options.document_private {
2403+
let note_msg = if cx.document_private() {
24042404
"this link resolves only because you passed `--document-private-items`, but will break without"
24052405
} else {
24062406
"this link will resolve properly if you pass `--document-private-items`"

src/librustdoc/passes/lint/redundant_explicit_links.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ fn check_redundant_explicit_link_for_did(
4646
return;
4747
};
4848

49-
let is_hidden = !cx.render_options.document_hidden
49+
let is_hidden = !cx.document_hidden()
5050
&& (item.is_doc_hidden() || inherits_doc_hidden(cx.tcx, local_item_id, None));
5151
if is_hidden {
5252
return;
5353
}
54-
let is_private = !cx.render_options.document_private
55-
&& !cx.cache.effective_visibilities.is_directly_public(cx.tcx, did);
54+
let is_private =
55+
!cx.document_private() && !cx.cache.effective_visibilities.is_directly_public(cx.tcx, did);
5656
if is_private {
5757
return;
5858
}

src/librustdoc/passes/strip_hidden.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clea
4343
retained: &retained,
4444
cache: &cx.cache,
4545
is_json_output,
46-
document_private: cx.render_options.document_private,
47-
document_hidden: cx.render_options.document_hidden,
46+
document_private: cx.document_private(),
47+
document_hidden: cx.document_hidden(),
4848
};
4949
stripper.fold_crate(krate)
5050
}

src/librustdoc/passes/strip_priv_imports.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ pub(crate) const STRIP_PRIV_IMPORTS: Pass = Pass {
1414

1515
pub(crate) fn strip_priv_imports(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
1616
let is_json_output = cx.is_json_output();
17-
ImportStripper {
18-
tcx: cx.tcx,
19-
is_json_output,
20-
document_hidden: cx.render_options.document_hidden,
21-
}
22-
.fold_crate(krate)
17+
ImportStripper { tcx: cx.tcx, is_json_output, document_hidden: cx.document_hidden() }
18+
.fold_crate(krate)
2319
}

src/librustdoc/passes/strip_private.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) ->
2929
is_json_output,
3030
tcx: cx.tcx,
3131
};
32-
krate = ImportStripper {
33-
tcx: cx.tcx,
34-
is_json_output,
35-
document_hidden: cx.render_options.document_hidden,
36-
}
37-
.fold_crate(stripper.fold_crate(krate));
32+
krate =
33+
ImportStripper { tcx: cx.tcx, is_json_output, document_hidden: cx.document_hidden() }
34+
.fold_crate(stripper.fold_crate(krate));
3835
}
3936

4037
// strip all impls referencing private items
@@ -43,8 +40,8 @@ pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) ->
4340
retained: &retained,
4441
cache: &cx.cache,
4542
is_json_output,
46-
document_private: cx.render_options.document_private,
47-
document_hidden: cx.render_options.document_hidden,
43+
document_private: cx.document_private(),
44+
document_hidden: cx.document_hidden(),
4845
};
4946
stripper.fold_crate(krate)
5047
}

0 commit comments

Comments
 (0)