Skip to content

Commit ae7173e

Browse files
committed
Auto merge of #147832 - aDotInTheVoid:rustdoc-format-options, r=GuillaumeGomez
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
2 parents 25d319a + 6f1cecf commit ae7173e

File tree

12 files changed

+51
-46
lines changed

12 files changed

+51
-46
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
}
@@ -1588,9 +1588,9 @@ fn first_non_private<'tcx>(
15881588
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res {
15891589
continue;
15901590
}
1591-
if (cx.render_options.document_hidden ||
1591+
if (cx.document_hidden() ||
15921592
!cx.tcx.is_doc_hidden(use_def_id)) &&
1593-
// We never check for "cx.render_options.document_private"
1593+
// We never check for "cx.document_private()"
15941594
// because if a re-export is not fully public, it's never
15951595
// documented.
15961596
cx.tcx.local_visibility(local_use_def_id).is_public()
@@ -2634,7 +2634,7 @@ fn get_all_import_attributes<'hir>(
26342634
attrs = import_attrs.iter().map(|attr| (Cow::Borrowed(attr), Some(def_id))).collect();
26352635
first = false;
26362636
// We don't add attributes of an intermediate re-export if it has `#[doc(hidden)]`.
2637-
} else if cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id) {
2637+
} else if cx.document_hidden() || !cx.tcx.is_doc_hidden(def_id) {
26382638
add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id));
26392639
}
26402640
}
@@ -3075,8 +3075,7 @@ fn clean_use_statement_inner<'tcx>(
30753075
// #[doc(no_inline)] attribute is present.
30763076
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
30773077
let mut denied = cx.is_json_output()
3078-
|| !(visibility.is_public()
3079-
|| (cx.render_options.document_private && is_visible_from_parent_mod))
3078+
|| !(visibility.is_public() || (cx.document_private() && is_visible_from_parent_mod))
30803079
|| pub_underscore
30813080
|| attrs.iter().any(|a| {
30823081
a.has_name(sym::doc)

src/librustdoc/core.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_span::symbol::sym;
2929
use tracing::{debug, info};
3030

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

144152
/// Creates a new `DiagCtxt` that can be used to emit warnings and errors.
@@ -379,7 +387,6 @@ pub(crate) fn run_global_ctxt(
379387
cache: Cache::new(render_options.document_private, render_options.document_hidden),
380388
inlined: FxHashSet::default(),
381389
output_format,
382-
render_options,
383390
show_coverage,
384391
};
385392

@@ -424,9 +431,9 @@ pub(crate) fn run_global_ctxt(
424431
for p in passes::defaults(show_coverage) {
425432
let run = match p.condition {
426433
Always => true,
427-
WhenDocumentPrivate => ctxt.render_options.document_private,
428-
WhenNotDocumentPrivate => !ctxt.render_options.document_private,
429-
WhenNotDocumentHidden => !ctxt.render_options.document_hidden,
434+
WhenDocumentPrivate => ctxt.document_private(),
435+
WhenNotDocumentPrivate => !ctxt.document_private(),
436+
WhenNotDocumentHidden => !ctxt.document_hidden(),
430437
};
431438
if run {
432439
debug!("running pass {}", p.pass.name);
@@ -444,15 +451,16 @@ pub(crate) fn run_global_ctxt(
444451

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

447-
krate = tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate));
454+
krate =
455+
tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate, &render_options));
448456

449457
let mut collector =
450458
LinkCollector { cx: &mut ctxt, visited_links: visited, ambiguous_links: ambiguous };
451459
collector.resolve_ambiguities();
452460

453461
tcx.dcx().abort_if_errors();
454462

455-
(krate, ctxt.render_options, ctxt.cache, expanded_macros)
463+
(krate, render_options, ctxt.cache, expanded_macros)
456464
}
457465

458466
/// 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)
@@ -2399,7 +2399,7 @@ fn privacy_error(cx: &DocContext<'_>, diag_info: &DiagnosticInfo<'_>, path_str:
23992399
diag.span_label(sp, "this item is private");
24002400
}
24012401

2402-
let note_msg = if cx.render_options.document_private {
2402+
let note_msg = if cx.document_private() {
24032403
"this link resolves only because you passed `--document-private-items`, but will break without"
24042404
} else {
24052405
"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)