From 89721a97666fb42b9f7cd0ab127597fb353538dd Mon Sep 17 00:00:00 2001 From: jefftt Date: Sun, 12 Oct 2025 18:46:32 -0400 Subject: [PATCH] librustdoc: Make RenderOptions boolean fields into newtypes --- src/librustdoc/clean/inline.rs | 2 +- src/librustdoc/clean/mod.rs | 14 +-- src/librustdoc/clean/types.rs | 7 +- src/librustdoc/config.rs | 101 +++++++++++++----- src/librustdoc/core.rs | 12 +-- src/librustdoc/formats/cache.rs | 7 +- src/librustdoc/html/format.rs | 2 +- src/librustdoc/html/macro_expansion.rs | 4 +- src/librustdoc/html/render/context.rs | 12 +-- src/librustdoc/html/render/mod.rs | 4 +- src/librustdoc/html/render/span_map.rs | 5 +- src/librustdoc/html/render/type_layout.rs | 2 +- src/librustdoc/html/render/write_shared.rs | 6 +- src/librustdoc/json/mod.rs | 4 +- src/librustdoc/lib.rs | 2 +- src/librustdoc/markdown.rs | 2 +- .../passes/check_doc_test_visibility.rs | 2 +- .../passes/collect_intra_doc_links.rs | 6 +- .../passes/lint/redundant_explicit_links.rs | 4 +- src/librustdoc/passes/stripper.rs | 13 +-- src/librustdoc/scrape_examples.rs | 3 +- src/librustdoc/visit_ast.rs | 8 +- src/librustdoc/visit_lib.rs | 5 +- 23 files changed, 139 insertions(+), 88 deletions(-) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 8beea0580de2d..69415b61d321c 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -497,7 +497,7 @@ pub(crate) fn build_impl( return; } - let document_hidden = cx.render_options.document_hidden; + let document_hidden = cx.render_options.document_hidden.0; let (trait_items, generics) = match impl_item { Some(impl_) => ( impl_ diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 4fd8d245089e7..ddb556de127ec 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -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.render_options.document_hidden.0 || !item.is_doc_hidden()) { inserted.insert((item.type_(), name)); } @@ -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.render_options.document_hidden.0 && 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. @@ -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.render_options.document_hidden.0 || !item.is_doc_hidden()) { inserted.insert((item.type_(), name)); } @@ -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.render_options.document_hidden.0 || !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; } @@ -1581,7 +1581,7 @@ fn first_non_private<'tcx>( if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res { continue; } - if (cx.render_options.document_hidden || + if (cx.render_options.document_hidden.0 || !cx.tcx.is_doc_hidden(use_def_id)) && // We never check for "cx.render_options.document_private" // because if a re-export is not fully public, it's never @@ -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.render_options.document_hidden.0 || !cx.tcx.is_doc_hidden(def_id) { add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id)); } } @@ -3066,7 +3066,7 @@ fn clean_use_statement_inner<'tcx>( // 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)) + || (cx.render_options.document_private.0 && is_visible_from_parent_mod)) || pub_underscore || attrs.iter().any(|a| { a.has_name(sym::doc) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index f3662a67bbea2..ff26d3ade9cc9 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -37,6 +37,7 @@ use crate::clean::cfg::Cfg; use crate::clean::clean_middle_path; use crate::clean::inline::{self, print_inlined_const}; use crate::clean::utils::{is_literal_expr, print_evaluated_const}; +use crate::config::ExternHtmlRootTakesPrecedence; use crate::core::DocContext; use crate::formats::cache::Cache; use crate::formats::item_type::ItemType; @@ -159,7 +160,7 @@ impl ExternalCrate { pub(crate) fn location( &self, extern_url: Option<&str>, - extern_url_takes_precedence: bool, + extern_url_takes_precedence: ExternHtmlRootTakesPrecedence, dst: &std::path::Path, tcx: TyCtxt<'_>, ) -> ExternalLocation { @@ -181,7 +182,9 @@ impl ExternalCrate { return Local; } - if extern_url_takes_precedence && let Some(url) = extern_url { + if extern_url_takes_precedence.0 + && let Some(url) = extern_url + { return to_remote(url); } diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index d7f6fa347becb..b10a1c54e3475 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -249,7 +249,7 @@ pub(crate) struct RenderOptions { /// A map of crate names to the URL to use instead of querying the crate's `html_root_url`. pub(crate) extern_html_root_urls: BTreeMap, /// Whether to give precedence to `html_root_url` or `--extern-html-root-url`. - pub(crate) extern_html_root_takes_precedence: bool, + pub(crate) extern_html_root_takes_precedence: ExternHtmlRootTakesPrecedence, /// A map of the default settings (values are as for DOM storage API). Keys should lack the /// `rustdoc-` prefix. pub(crate) default_settings: FxIndexMap, @@ -257,7 +257,7 @@ pub(crate) struct RenderOptions { pub(crate) resource_suffix: String, /// Whether to create an index page in the root of the output directory. If this is true but /// `enable_index_page` is None, generate a static listing of crates instead. - pub(crate) enable_index_page: bool, + pub(crate) enable_index_page: EnableIndexPage, /// A file to use as the index page at the root of the output directory. Overrides /// `enable_index_page` to be true if set. pub(crate) index_page: Option, @@ -268,35 +268,35 @@ pub(crate) struct RenderOptions { // Options specific to reading standalone Markdown files /// Whether to generate a table of contents on the output file when reading a standalone /// Markdown file. - pub(crate) markdown_no_toc: bool, + pub(crate) markdown_no_toc: MarkdownNoToc, /// Additional CSS files to link in pages generated from standalone Markdown files. pub(crate) markdown_css: Vec, /// If present, playground URL to use in the "Run" button added to code samples generated from /// standalone Markdown files. If not present, `playground_url` is used. pub(crate) markdown_playground_url: Option, /// Document items that have lower than `pub` visibility. - pub(crate) document_private: bool, + pub(crate) document_private: DocumentPrivate, /// Document items that have `doc(hidden)`. - pub(crate) document_hidden: bool, + pub(crate) document_hidden: DocumentHidden, /// If `true`, generate a JSON file in the crate folder instead of HTML redirection files. - pub(crate) generate_redirect_map: bool, + pub(crate) generate_redirect_map: GenerateRedirectMap, /// Show the memory layout of types in the docs. - pub(crate) show_type_layout: bool, + pub(crate) show_type_layout: ShowTypeLayout, /// Note: this field is duplicated in `Options` because it's useful to have /// it in both places. pub(crate) unstable_features: rustc_feature::UnstableFeatures, pub(crate) emit: Vec, /// If `true`, HTML source pages will generate links for items to their definition. - pub(crate) generate_link_to_definition: bool, + pub(crate) generate_link_to_definition: GenerateLinkToDefinition, /// Set of function-call locations to include as examples pub(crate) call_locations: AllCallLocations, /// If `true`, Context::init will not emit shared files. - pub(crate) no_emit_shared: bool, + pub(crate) no_emit_shared: NoEmitShared, /// If `true`, HTML source code pages won't be generated. - pub(crate) html_no_source: bool, + pub(crate) html_no_source: HtmlNoSource, /// This field is only used for the JSON output. If it's set to true, no file will be created /// and content will be displayed in stdout directly. - pub(crate) output_to_stdout: bool, + pub(crate) output_to_stdout: OutputToStdout, /// Whether we should read or write rendered cross-crate info in the doc root. pub(crate) should_merge: ShouldMerge, /// Path to crate-info for external crates. @@ -304,11 +304,50 @@ pub(crate) struct RenderOptions { /// Where to write crate-info pub(crate) parts_out_dir: Option, /// disable minification of CSS/JS - pub(crate) disable_minification: bool, + pub(crate) disable_minification: DisableMinification, /// If `true`, HTML source pages will generate the possibility to expand macros. - pub(crate) generate_macro_expansion: bool, + pub(crate) generate_macro_expansion: GenerateMacroExpansion, } +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct ExternHtmlRootTakesPrecedence(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct EnableIndexPage(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct MarkdownNoToc(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct DocumentPrivate(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct DocumentHidden(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct GenerateRedirectMap(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct ShowTypeLayout(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct GenerateLinkToDefinition(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct NoEmitShared(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct HtmlNoSource(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct OutputToStdout(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct DisableMinification(pub(crate) bool); + +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct GenerateMacroExpansion(pub(crate) bool); + #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub(crate) enum ModuleSorting { DeclarationOrder, @@ -633,7 +672,7 @@ impl Options { dcx.fatal("the `--test` flag must be passed to enable `--no-run`"); } - let mut output_to_stdout = false; + let mut output_to_stdout = OutputToStdout(false); let test_builder_wrappers = matches.opt_strs("test-builder-wrapper").iter().map(PathBuf::from).collect(); let output = match (matches.opt_str("out-dir"), matches.opt_str("output")) { @@ -641,7 +680,7 @@ impl Options { dcx.fatal("cannot use both 'out-dir' and 'output' at once"); } (Some(out_dir), None) | (None, Some(out_dir)) => { - output_to_stdout = out_dir == "-"; + output_to_stdout.0 = out_dir == "-"; PathBuf::from(out_dir) } (None, None) => PathBuf::from("doc"), @@ -773,11 +812,12 @@ impl Options { ModuleSorting::Alphabetical }; let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default(); - let markdown_no_toc = matches.opt_present("markdown-no-toc"); + let markdown_no_toc = MarkdownNoToc(matches.opt_present("markdown-no-toc")); let markdown_css = matches.opt_strs("markdown-css"); let markdown_playground_url = matches.opt_str("markdown-playground-url"); let crate_version = matches.opt_str("crate-version"); - let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some(); + let enable_index_page = + EnableIndexPage(matches.opt_present("enable-index-page") || index_page.is_some()); let static_root_path = matches.opt_str("static-root-path"); let test_run_directory = matches.opt_str("test-run-directory").map(PathBuf::from); let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from); @@ -788,30 +828,33 @@ impl Options { let extern_strs = matches.opt_strs("extern"); let test_runtool = matches.opt_str("test-runtool"); let test_runtool_args = matches.opt_strs("test-runtool-arg"); - let document_private = matches.opt_present("document-private-items"); - let document_hidden = matches.opt_present("document-hidden-items"); + let document_private = DocumentPrivate(matches.opt_present("document-private-items")); + let document_hidden = DocumentHidden(matches.opt_present("document-hidden-items")); let run_check = matches.opt_present("check"); - let generate_redirect_map = matches.opt_present("generate-redirect-map"); - let show_type_layout = matches.opt_present("show-type-layout"); + let generate_redirect_map = + GenerateRedirectMap(matches.opt_present("generate-redirect-map")); + let show_type_layout = ShowTypeLayout(matches.opt_present("show-type-layout")); let nocapture = matches.opt_present("nocapture"); - let generate_link_to_definition = matches.opt_present("generate-link-to-definition"); - let generate_macro_expansion = matches.opt_present("generate-macro-expansion"); + let generate_link_to_definition = + GenerateLinkToDefinition(matches.opt_present("generate-link-to-definition")); + let generate_macro_expansion = + GenerateMacroExpansion(matches.opt_present("generate-macro-expansion")); let extern_html_root_takes_precedence = - matches.opt_present("extern-html-root-takes-precedence"); - let html_no_source = matches.opt_present("html-no-source"); + ExternHtmlRootTakesPrecedence(matches.opt_present("extern-html-root-takes-precedence")); + let html_no_source = HtmlNoSource(matches.opt_present("html-no-source")); let should_merge = match parse_merge(matches) { Ok(result) => result, Err(e) => dcx.fatal(format!("--merge option error: {e}")), }; - if generate_link_to_definition && (show_coverage || output_format != OutputFormat::Html) { + if generate_link_to_definition.0 && (show_coverage || output_format != OutputFormat::Html) { dcx.struct_warn( "`--generate-link-to-definition` option can only be used with HTML output format", ) .with_note("`--generate-link-to-definition` option will be ignored") .emit(); } - if generate_macro_expansion && (show_coverage || output_format != OutputFormat::Html) { + if generate_macro_expansion.0 && (show_coverage || output_format != OutputFormat::Html) { dcx.struct_warn( "`--generate-macro-expansion` option can only be used with HTML output format", ) @@ -828,7 +871,7 @@ impl Options { let unstable_features = rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref()); - let disable_minification = matches.opt_present("disable-minification"); + let disable_minification = DisableMinification(matches.opt_present("disable-minification")); let options = Options { bin_crate, @@ -901,7 +944,7 @@ impl Options { generate_link_to_definition, generate_macro_expansion, call_locations, - no_emit_shared: false, + no_emit_shared: NoEmitShared(false), html_no_source, output_to_stdout, should_merge, diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 0d4e24538e0d5..a36d5e5b9f98e 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -29,7 +29,7 @@ use tracing::{debug, info}; use crate::clean::inline::build_trait; use crate::clean::{self, ItemId}; -use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions}; +use crate::config::{DocumentPrivate, Options as RustdocOptions, OutputFormat, RenderOptions}; use crate::formats::cache::Cache; use crate::html::macro_expansion::{ExpandedCode, source_macro_expansion}; use crate::passes; @@ -244,7 +244,7 @@ pub(crate) fn create_config( let crate_types = if proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] }; - let resolve_doc_links = if render_options.document_private { + let resolve_doc_links = if render_options.document_private.0 { ResolveDocLinks::All } else { ResolveDocLinks::Exported @@ -420,7 +420,7 @@ pub(crate) fn run_global_ctxt( // 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; + ctxt.render_options.document_private = DocumentPrivate(true); } } @@ -432,9 +432,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.render_options.document_private.0, + WhenNotDocumentPrivate => !ctxt.render_options.document_private.0, + WhenNotDocumentHidden => !ctxt.render_options.document_hidden.0, }; if run { debug!("running pass {}", p.pass.name); diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 5e5592269af60..2099983f99845 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -10,6 +10,7 @@ use tracing::debug; use crate::clean::types::ExternalLocation; use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType}; +use crate::config::{DocumentHidden, DocumentPrivate}; use crate::core::DocContext; use crate::fold::DocFolder; use crate::formats::Impl; @@ -88,10 +89,10 @@ pub(crate) struct Cache { /// Whether to document private items. /// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions. - pub(crate) document_private: bool, + pub(crate) document_private: DocumentPrivate, /// Whether to document hidden items. /// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions. - pub(crate) document_hidden: bool, + pub(crate) document_hidden: DocumentHidden, /// Crates marked with [`#[doc(masked)]`][doc_masked]. /// @@ -142,7 +143,7 @@ struct CacheBuilder<'a, 'tcx> { } impl Cache { - pub(crate) fn new(document_private: bool, document_hidden: bool) -> Self { + pub(crate) fn new(document_private: DocumentPrivate, document_hidden: DocumentHidden) -> Self { Cache { document_private, document_hidden, ..Cache::default() } } diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 856e637a4587b..8f06e53e76d18 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -563,7 +563,7 @@ pub(crate) fn href_with_root_path( return Err(HrefError::Private); } } else if !cache.effective_visibilities.is_directly_public(tcx, did) - && !cache.document_private + && !cache.document_private.0 && !cache.primitive_locations.values().any(|&id| id == did) { return Err(HrefError::Private); diff --git a/src/librustdoc/html/macro_expansion.rs b/src/librustdoc/html/macro_expansion.rs index 9098e92a5cd41..c829426cbd03b 100644 --- a/src/librustdoc/html/macro_expansion.rs +++ b/src/librustdoc/html/macro_expansion.rs @@ -14,8 +14,8 @@ pub(crate) fn source_macro_expansion( source_map: &SourceMap, ) -> FxHashMap> { if output_format == OutputFormat::Html - && !render_options.html_no_source - && render_options.generate_macro_expansion + && !render_options.html_no_source.0 + && render_options.generate_macro_expansion.0 { let mut expanded_visitor = ExpandedCodeVisitor { expanded_codes: Vec::new(), source_map }; walk_crate(&mut expanded_visitor, krate); diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 4c06d0da47013..67b6b798e3c18 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -21,7 +21,7 @@ use super::{AllTypes, LinkFromSrc, StylePath, collect_spans_and_sources, scrape_ use crate::clean::types::ExternalLocation; use crate::clean::utils::has_doc_flag; use crate::clean::{self, ExternalCrate}; -use crate::config::{ModuleSorting, RenderOptions, ShouldMerge}; +use crate::config::{ModuleSorting, RenderOptions, ShouldMerge, ShowTypeLayout}; use crate::docfs::{DocFS, PathError}; use crate::error::Error; use crate::formats::FormatRenderer; @@ -107,7 +107,7 @@ pub(crate) struct SharedContext<'tcx> { /// The local file sources we've emitted and their respective url-paths. pub(crate) local_sources: FxIndexMap, /// Show the memory layout of types in the docs. - pub(super) show_type_layout: bool, + pub(super) show_type_layout: ShowTypeLayout, /// The base-URL of the issue tracker for when an item has been tagged with /// an issue number. pub(super) issue_tracker_base_url: Option, @@ -516,7 +516,7 @@ impl<'tcx> Context<'tcx> { scrape_examples_extension: !call_locations.is_empty(), }; let mut issue_tracker_base_url = None; - let mut include_sources = !html_no_source; + let mut include_sources = !html_no_source.0; // Crawl the crate attributes looking for attributes which control how we're // going to emit HTML @@ -569,7 +569,7 @@ impl<'tcx> Context<'tcx> { playground, all: RefCell::new(AllTypes::new()), errors: receiver, - redirections: if generate_redirect_map { Some(Default::default()) } else { None }, + redirections: if generate_redirect_map.0 { Some(Default::default()) } else { None }, show_type_layout, span_correspondence_map: matches, cache, @@ -595,7 +595,7 @@ impl<'tcx> Context<'tcx> { sources::render(&mut cx, &krate)?; } - if !no_emit_shared { + if !no_emit_shared.0 { write_shared(&mut cx, &krate, &md_opts, tcx)?; } @@ -812,7 +812,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { { self.info.is_inside_inlined_module = true; } - } else if !self.cache().document_hidden && item.is_doc_hidden() { + } else if !self.cache().document_hidden.0 && item.is_doc_hidden() { // We're not inside an inlined module anymore since this one cannot be re-exported. self.info.is_inside_inlined_module = false; } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index c3b1e3eb6c087..c5706046a46fb 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2963,9 +2963,9 @@ fn repr_attribute<'tcx>( }; let repr = adt.repr(); - let is_visible = |def_id| cache.document_hidden || !tcx.is_doc_hidden(def_id); + let is_visible = |def_id| cache.document_hidden.0 || !tcx.is_doc_hidden(def_id); let is_public_field = |field: &ty::FieldDef| { - (cache.document_private || field.vis.is_public()) && is_visible(field.did) + (cache.document_private.0 || field.vis.is_public()) && is_visible(field.did) }; if repr.transparent() { diff --git a/src/librustdoc/html/render/span_map.rs b/src/librustdoc/html/render/span_map.rs index bc9417b1bb1de..c8c18be0b0872 100644 --- a/src/librustdoc/html/render/span_map.rs +++ b/src/librustdoc/html/render/span_map.rs @@ -11,6 +11,7 @@ use rustc_span::hygiene::MacroKind; use rustc_span::{BytePos, ExpnKind}; use crate::clean::{self, PrimitiveType, rustc_span}; +use crate::config::GenerateLinkToDefinition; use crate::html::sources; /// This is a stripped down version of [`rustc_span::Span`] that only contains the start and end byte positions of the span. @@ -80,12 +81,12 @@ pub(crate) fn collect_spans_and_sources( krate: &clean::Crate, src_root: &Path, include_sources: bool, - generate_link_to_definition: bool, + generate_link_to_definition: GenerateLinkToDefinition, ) -> (FxIndexMap, FxHashMap) { if include_sources { let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() }; - if generate_link_to_definition { + if generate_link_to_definition.0 { tcx.hir_walk_toplevel_module(&mut visitor); } let sources = sources::collect_local_sources(tcx, src_root, krate); diff --git a/src/librustdoc/html/render/type_layout.rs b/src/librustdoc/html/render/type_layout.rs index fb1f0271c2ad7..45ec334ee3761 100644 --- a/src/librustdoc/html/render/type_layout.rs +++ b/src/librustdoc/html/render/type_layout.rs @@ -27,7 +27,7 @@ struct TypeLayoutSize { pub(crate) fn document_type_layout(cx: &Context<'_>, ty_def_id: DefId) -> impl fmt::Display { fmt::from_fn(move |f| { - if !cx.shared.show_type_layout { + if !cx.shared.show_type_layout.0 { return Ok(()); } diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 3a1db805d01cf..b2ea881f02678 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -104,7 +104,7 @@ pub(crate) fn write_shared( cx.info.include_sources, )?; match &opt.index_page { - Some(index_page) if opt.enable_index_page => { + Some(index_page) if opt.enable_index_page.0 => { let mut md_opts = opt.clone(); md_opts.output = cx.dst.clone(); md_opts.external_html = cx.shared.layout.external_html.clone(); @@ -113,7 +113,7 @@ pub(crate) fn write_shared( &index_page ); } - None if opt.enable_index_page => { + None if opt.enable_index_page.0 => { write_rendered_cci::( || CratesIndexPart::blank(cx), &cx.dst, @@ -212,7 +212,7 @@ fn write_static_files( static_files::for_each(|f: &static_files::StaticFile| { let filename = static_dir.join(f.output_filename()); let contents: &[u8] = - if opt.disable_minification { f.src_bytes } else { f.minified_bytes }; + if opt.disable_minification.0 { f.src_bytes } else { f.minified_bytes }; fs::write(&filename, contents).map_err(|e| PathError::new(e, &filename)) })?; } diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index b724d7e866a05..f6462a55b9399 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -190,7 +190,7 @@ impl<'tcx> JsonRenderer<'tcx> { JsonRenderer { tcx, index: FxHashMap::default(), - out_dir: if options.output_to_stdout { None } else { Some(options.output) }, + out_dir: if options.output_to_stdout.0 { None } else { Some(options.output) }, cache: Rc::new(cache), imported_items, id_interner: Default::default(), @@ -323,7 +323,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { let output_crate = types::Crate { root: self.id_from_item_default(e.def_id().into()), crate_version: self.cache.crate_version.clone(), - includes_private: self.cache.document_private, + includes_private: self.cache.document_private.0, index, paths: self .cache diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index d7ffb25f8bd81..99af2333a2983 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -782,7 +782,7 @@ fn run_merge_finalize(opt: config::RenderOptions) -> Result<(), error::Error> { "config.rs only allows us to return InputMode::NoInputMergeFinalize if --merge=finalize" ); let crates = html::render::CrateInfo::read_many(&opt.include_parts_dir)?; - let include_sources = !opt.html_no_source; + let include_sources = !opt.html_no_source.0; html::render::write_not_crate_specific( &crates, &opt.output, diff --git a/src/librustdoc/markdown.rs b/src/librustdoc/markdown.rs index 4ca2c104888bb..ac573502eaa6c 100644 --- a/src/librustdoc/markdown.rs +++ b/src/librustdoc/markdown.rs @@ -79,7 +79,7 @@ pub(crate) fn render_and_write>( let error_codes = ErrorCodes::from(options.unstable_features.is_nightly_build()); let text = fmt::from_fn(|f| { - if !options.markdown_no_toc { + if !options.markdown_no_toc.0 { MarkdownWithToc { content: text, links: &[], diff --git a/src/librustdoc/passes/check_doc_test_visibility.rs b/src/librustdoc/passes/check_doc_test_visibility.rs index e0ea760cf3ba5..0d1613cfeaf43 100644 --- a/src/librustdoc/passes/check_doc_test_visibility.rs +++ b/src/librustdoc/passes/check_doc_test_visibility.rs @@ -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.render_options.document_hidden.0 && (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() { diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 79d74c3c4eb90..1b9cb4ba52c4a 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -1070,7 +1070,7 @@ fn preprocessed_markdown_links(s: &str) -> Vec { impl LinkCollector<'_, '_> { #[instrument(level = "debug", skip_all)] fn resolve_links(&mut self, item: &Item) { - if !self.cx.render_options.document_private + if !self.cx.render_options.document_private.0 && 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) @@ -1204,7 +1204,7 @@ impl LinkCollector<'_, '_> { let cache = &self.cx.cache; if !original_did.is_local() && !cache.effective_visibilities.is_directly_public(tcx, did) - && !cache.document_private + && !cache.document_private.0 && !cache.primitive_locations.values().any(|&id| id == did) { return false; @@ -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.render_options.document_private.0 { "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`" diff --git a/src/librustdoc/passes/lint/redundant_explicit_links.rs b/src/librustdoc/passes/lint/redundant_explicit_links.rs index e69cf87f95787..2f92317085f6c 100644 --- a/src/librustdoc/passes/lint/redundant_explicit_links.rs +++ b/src/librustdoc/passes/lint/redundant_explicit_links.rs @@ -46,12 +46,12 @@ fn check_redundant_explicit_link_for_did( return; }; - let is_hidden = !cx.render_options.document_hidden + let is_hidden = !cx.render_options.document_hidden.0 && (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 + let is_private = !cx.render_options.document_private.0 && !cx.cache.effective_visibilities.is_directly_public(cx.tcx, did); if is_private { return; diff --git a/src/librustdoc/passes/stripper.rs b/src/librustdoc/passes/stripper.rs index 99d22526f85b7..f7d317d45384d 100644 --- a/src/librustdoc/passes/stripper.rs +++ b/src/librustdoc/passes/stripper.rs @@ -8,6 +8,7 @@ use tracing::debug; use crate::clean::utils::inherits_doc_hidden; use crate::clean::{self, Item, ItemId, ItemIdSet}; +use crate::config::{DocumentHidden, DocumentPrivate}; use crate::fold::{DocFolder, strip_item}; use crate::formats::cache::Cache; use crate::visit_lib::RustdocEffectiveVisibilities; @@ -174,8 +175,8 @@ pub(crate) struct ImplStripper<'a, 'tcx> { pub(crate) retained: &'a ItemIdSet, pub(crate) cache: &'a Cache, pub(crate) is_json_output: bool, - pub(crate) document_private: bool, - pub(crate) document_hidden: bool, + pub(crate) document_private: DocumentPrivate, + pub(crate) document_hidden: DocumentHidden, } impl ImplStripper<'_, '_> { @@ -187,7 +188,7 @@ impl ImplStripper<'_, '_> { // If the "for" item is exported and the impl block isn't `#[doc(hidden)]`, then we // need to keep it. self.cache.effective_visibilities.is_exported(self.tcx, for_def_id) - && (self.document_hidden + && (self.document_hidden.0 || ((!item.is_doc_hidden() && for_def_id .as_local() @@ -211,7 +212,7 @@ impl DocFolder for ImplStripper<'_, '_> { // If the only items present are private ones and we're not rendering private items, // we don't document it. if !imp.items.is_empty() - && !self.document_private + && !self.document_private.0 && imp.items.iter().all(|i| { let item_id = i.item_id; item_id.is_local() @@ -263,7 +264,7 @@ impl DocFolder for ImplStripper<'_, '_> { pub(crate) struct ImportStripper<'tcx> { pub(crate) tcx: TyCtxt<'tcx>, pub(crate) is_json_output: bool, - pub(crate) document_hidden: bool, + pub(crate) document_hidden: DocumentHidden, } impl ImportStripper<'_> { @@ -281,7 +282,7 @@ impl DocFolder for ImportStripper<'_> { fn fold_item(&mut self, i: Item) -> Option { match &i.kind { clean::ImportItem(imp) - if !self.document_hidden && self.import_should_be_hidden(&i, imp) => + if !self.document_hidden.0 && self.import_should_be_hidden(&i, imp) => { None } diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 471e966e2c24b..abeb2a40ea521 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -18,6 +18,7 @@ use rustc_span::edition::Edition; use rustc_span::{BytePos, FileName, SourceFile}; use tracing::{debug, trace, warn}; +use crate::config::NoEmitShared; use crate::html::render::Context; use crate::{clean, config, formats}; @@ -274,7 +275,7 @@ pub(crate) fn run( ) { let inner = move || -> Result<(), String> { // Generates source files for examples - renderopts.no_emit_shared = true; + renderopts.no_emit_shared = NoEmitShared(true); let (cx, _) = Context::init(krate, renderopts, cache, tcx, Default::default()) .map_err(|e| e.to_string())?; diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index dc9889cec2193..4d86bde62e2fa 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -247,14 +247,14 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { 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) - || (document_hidden && hir_attr_lists(use_attrs, sym::doc).has_word(sym::hidden)); + || (document_hidden.0 && hir_attr_lists(use_attrs, sym::doc).has_word(sym::hidden)); if is_no_inline { return false; } let is_glob = renamed.is_none(); - let is_hidden = !document_hidden && tcx.is_doc_hidden(ori_res_did); + let is_hidden = !document_hidden.0 && tcx.is_doc_hidden(ori_res_did); let Some(res_did) = ori_res_did.as_local() else { // For cross-crate impl inlining we need to know whether items are // reachable in documentation -- a previously unreachable item can be @@ -281,7 +281,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let item = tcx.hir_node_by_def_id(res_did); if !please_inline { - let inherits_hidden = !document_hidden && inherits_doc_hidden(tcx, res_did, None); + let inherits_hidden = !document_hidden.0 && inherits_doc_hidden(tcx, res_did, None); // Only inline if requested or if the item would otherwise be stripped. if (!is_private && !inherits_hidden) || ( is_hidden && @@ -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.render_options.document_hidden.0 { return true; } let tcx = self.cx.tcx; diff --git a/src/librustdoc/visit_lib.rs b/src/librustdoc/visit_lib.rs index 369fc52860ee2..7a7beaaca550e 100644 --- a/src/librustdoc/visit_lib.rs +++ b/src/librustdoc/visit_lib.rs @@ -2,6 +2,7 @@ use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, DefIdSet}; use rustc_middle::ty::TyCtxt; +use crate::config::DocumentHidden; use crate::core::DocContext; // FIXME: this may not be exhaustive, but is sufficient for rustdocs current uses @@ -47,7 +48,7 @@ struct LibEmbargoVisitor<'a, 'tcx> { extern_public: &'a mut DefIdSet, // Keeps track of already visited modules, in case a module re-exports its parent visited_mods: DefIdSet, - document_hidden: bool, + document_hidden: DocumentHidden, } impl LibEmbargoVisitor<'_, '_> { @@ -66,7 +67,7 @@ impl LibEmbargoVisitor<'_, '_> { } fn visit_item(&mut self, def_id: DefId) { - if self.document_hidden || !self.tcx.is_doc_hidden(def_id) { + if self.document_hidden.0 || !self.tcx.is_doc_hidden(def_id) { self.extern_public.insert(def_id); if self.tcx.def_kind(def_id) == DefKind::Mod { self.visit_mod(def_id);