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.render_options.document_hidden.0;
let (trait_items, generics) = match impl_item {
Some(impl_) => (
impl_
Expand Down
14 changes: 7 additions & 7 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.render_options.document_hidden.0 || !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.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.
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.render_options.document_hidden.0 || !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.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;
}
Expand Down Expand Up @@ -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
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.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));
}
}
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
101 changes: 72 additions & 29 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@ 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<String, String>,
/// 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<String, String>,
/// If present, suffix added to CSS/JavaScript files when referencing them in generated pages.
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<PathBuf>,
Expand All @@ -268,47 +268,86 @@ 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<String>,
/// 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<String>,
/// 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<EmitType>,
/// 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.
pub(crate) include_parts_dir: Vec<PathToParts>,
/// Where to write crate-info
pub(crate) parts_out_dir: Option<PathToParts>,
/// 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,
}

Copy link
Member

@fmease fmease Oct 13, 2025

Choose a reason for hiding this comment

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

Could you please turn all of them into enum { Yes, No }, that's what's more conventional in this repo (and also what Esteban suggested back then (see the PR linked from the issue)).

#[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,
Expand Down Expand Up @@ -633,15 +672,15 @@ 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")) {
(Some(_), Some(_)) => {
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"),
Expand Down Expand Up @@ -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);
Expand All @@ -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",
)
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 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::{DocumentHidden, DocumentPrivate};
use crate::core::DocContext;
use crate::fold::DocFolder;
use crate::formats::Impl;
Expand Down Expand Up @@ -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].
///
Expand Down Expand Up @@ -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() }
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading