Skip to content

Commit f39b8ba

Browse files
author
jefftt
committed
librustdoc: Make RenderOptions boolean fields into newtypes
1 parent be0ade2 commit f39b8ba

23 files changed

+133
-88
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.render_options.document_hidden.0;
501501
let (trait_items, generics) = match impl_item {
502502
Some(impl_) => (
503503
impl_

src/librustdoc/clean/mod.rs

Lines changed: 7 additions & 7 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.render_options.document_hidden.0 || !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.render_options.document_hidden.0 && 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.render_options.document_hidden.0 || !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.render_options.document_hidden.0 || !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,7 +1581,7 @@ 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.render_options.document_hidden.0 ||
15851585
!cx.tcx.is_doc_hidden(use_def_id)) &&
15861586
// We never check for "cx.render_options.document_private"
15871587
// because if a re-export is not fully public, it's never
@@ -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.render_options.document_hidden.0 || !cx.tcx.is_doc_hidden(def_id) {
26322632
add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id));
26332633
}
26342634
}
@@ -3066,7 +3066,7 @@ fn clean_use_statement_inner<'tcx>(
30663066
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
30673067
let mut denied = cx.is_json_output()
30683068
|| !(visibility.is_public()
3069-
|| (cx.render_options.document_private && is_visible_from_parent_mod))
3069+
|| (cx.render_options.document_private.0 && is_visible_from_parent_mod))
30703070
|| pub_underscore
30713071
|| attrs.iter().any(|a| {
30723072
a.has_name(sym::doc)

src/librustdoc/clean/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use crate::clean::cfg::Cfg;
3737
use crate::clean::clean_middle_path;
3838
use crate::clean::inline::{self, print_inlined_const};
3939
use crate::clean::utils::{is_literal_expr, print_evaluated_const};
40+
use crate::config::ExternHtmlRootTakesPrecedence;
4041
use crate::core::DocContext;
4142
use crate::formats::cache::Cache;
4243
use crate::formats::item_type::ItemType;
@@ -159,7 +160,7 @@ impl ExternalCrate {
159160
pub(crate) fn location(
160161
&self,
161162
extern_url: Option<&str>,
162-
extern_url_takes_precedence: bool,
163+
extern_url_takes_precedence: ExternHtmlRootTakesPrecedence,
163164
dst: &std::path::Path,
164165
tcx: TyCtxt<'_>,
165166
) -> ExternalLocation {
@@ -181,7 +182,7 @@ impl ExternalCrate {
181182
return Local;
182183
}
183184

184-
if extern_url_takes_precedence && let Some(url) = extern_url {
185+
if extern_url_takes_precedence.0 && let Some(url) = extern_url {
185186
return to_remote(url);
186187
}
187188

src/librustdoc/config.rs

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,15 @@ pub(crate) struct RenderOptions {
249249
/// A map of crate names to the URL to use instead of querying the crate's `html_root_url`.
250250
pub(crate) extern_html_root_urls: BTreeMap<String, String>,
251251
/// Whether to give precedence to `html_root_url` or `--extern-html-root-url`.
252-
pub(crate) extern_html_root_takes_precedence: bool,
252+
pub(crate) extern_html_root_takes_precedence: ExternHtmlRootTakesPrecedence,
253253
/// A map of the default settings (values are as for DOM storage API). Keys should lack the
254254
/// `rustdoc-` prefix.
255255
pub(crate) default_settings: FxIndexMap<String, String>,
256256
/// If present, suffix added to CSS/JavaScript files when referencing them in generated pages.
257257
pub(crate) resource_suffix: String,
258258
/// Whether to create an index page in the root of the output directory. If this is true but
259259
/// `enable_index_page` is None, generate a static listing of crates instead.
260-
pub(crate) enable_index_page: bool,
260+
pub(crate) enable_index_page: EnableIndexPage,
261261
/// A file to use as the index page at the root of the output directory. Overrides
262262
/// `enable_index_page` to be true if set.
263263
pub(crate) index_page: Option<PathBuf>,
@@ -268,47 +268,86 @@ pub(crate) struct RenderOptions {
268268
// Options specific to reading standalone Markdown files
269269
/// Whether to generate a table of contents on the output file when reading a standalone
270270
/// Markdown file.
271-
pub(crate) markdown_no_toc: bool,
271+
pub(crate) markdown_no_toc: MarkdownNoToc,
272272
/// Additional CSS files to link in pages generated from standalone Markdown files.
273273
pub(crate) markdown_css: Vec<String>,
274274
/// If present, playground URL to use in the "Run" button added to code samples generated from
275275
/// standalone Markdown files. If not present, `playground_url` is used.
276276
pub(crate) markdown_playground_url: Option<String>,
277277
/// Document items that have lower than `pub` visibility.
278-
pub(crate) document_private: bool,
278+
pub(crate) document_private: DocumentPrivate,
279279
/// Document items that have `doc(hidden)`.
280-
pub(crate) document_hidden: bool,
280+
pub(crate) document_hidden: DocumentHidden,
281281
/// If `true`, generate a JSON file in the crate folder instead of HTML redirection files.
282-
pub(crate) generate_redirect_map: bool,
282+
pub(crate) generate_redirect_map: GenerateRedirectMap,
283283
/// Show the memory layout of types in the docs.
284-
pub(crate) show_type_layout: bool,
284+
pub(crate) show_type_layout: ShowTypeLayout,
285285
/// Note: this field is duplicated in `Options` because it's useful to have
286286
/// it in both places.
287287
pub(crate) unstable_features: rustc_feature::UnstableFeatures,
288288
pub(crate) emit: Vec<EmitType>,
289289
/// If `true`, HTML source pages will generate links for items to their definition.
290-
pub(crate) generate_link_to_definition: bool,
290+
pub(crate) generate_link_to_definition: GenerateLinkToDefinition,
291291
/// Set of function-call locations to include as examples
292292
pub(crate) call_locations: AllCallLocations,
293293
/// If `true`, Context::init will not emit shared files.
294-
pub(crate) no_emit_shared: bool,
294+
pub(crate) no_emit_shared: NoEmitShared,
295295
/// If `true`, HTML source code pages won't be generated.
296-
pub(crate) html_no_source: bool,
296+
pub(crate) html_no_source: HtmlNoSource,
297297
/// This field is only used for the JSON output. If it's set to true, no file will be created
298298
/// and content will be displayed in stdout directly.
299-
pub(crate) output_to_stdout: bool,
299+
pub(crate) output_to_stdout: OutputToStdout,
300300
/// Whether we should read or write rendered cross-crate info in the doc root.
301301
pub(crate) should_merge: ShouldMerge,
302302
/// Path to crate-info for external crates.
303303
pub(crate) include_parts_dir: Vec<PathToParts>,
304304
/// Where to write crate-info
305305
pub(crate) parts_out_dir: Option<PathToParts>,
306306
/// disable minification of CSS/JS
307-
pub(crate) disable_minification: bool,
307+
pub(crate) disable_minification: DisableMinification,
308308
/// If `true`, HTML source pages will generate the possibility to expand macros.
309-
pub(crate) generate_macro_expansion: bool,
309+
pub(crate) generate_macro_expansion: GenerateMacroExpansion,
310310
}
311311

312+
#[derive(Clone, Copy, Debug, Default)]
313+
pub(crate) struct ExternHtmlRootTakesPrecedence(pub(crate) bool);
314+
315+
#[derive(Clone, Copy, Debug, Default)]
316+
pub(crate) struct EnableIndexPage(pub(crate) bool);
317+
318+
#[derive(Clone, Copy, Debug, Default)]
319+
pub(crate) struct MarkdownNoToc(pub(crate) bool);
320+
321+
#[derive(Clone, Copy, Debug, Default)]
322+
pub(crate) struct DocumentPrivate(pub(crate) bool);
323+
324+
#[derive(Clone, Copy, Debug, Default)]
325+
pub(crate) struct DocumentHidden(pub(crate) bool);
326+
327+
#[derive(Clone, Copy, Debug, Default)]
328+
pub(crate) struct GenerateRedirectMap(pub(crate) bool);
329+
330+
#[derive(Clone, Copy, Debug, Default)]
331+
pub(crate) struct ShowTypeLayout(pub(crate) bool);
332+
333+
#[derive(Clone, Copy, Debug, Default)]
334+
pub(crate) struct GenerateLinkToDefinition(pub(crate) bool);
335+
336+
#[derive(Clone, Copy, Debug, Default)]
337+
pub(crate) struct NoEmitShared(pub(crate) bool);
338+
339+
#[derive(Clone, Copy, Debug, Default)]
340+
pub(crate) struct HtmlNoSource(pub(crate) bool);
341+
342+
#[derive(Clone, Copy, Debug, Default)]
343+
pub(crate) struct OutputToStdout(pub(crate) bool);
344+
345+
#[derive(Clone, Copy, Debug, Default)]
346+
pub(crate) struct DisableMinification(pub(crate) bool);
347+
348+
#[derive(Clone, Copy, Debug, Default)]
349+
pub(crate) struct GenerateMacroExpansion(pub(crate) bool);
350+
312351
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
313352
pub(crate) enum ModuleSorting {
314353
DeclarationOrder,
@@ -633,15 +672,15 @@ impl Options {
633672
dcx.fatal("the `--test` flag must be passed to enable `--no-run`");
634673
}
635674

636-
let mut output_to_stdout = false;
675+
let mut output_to_stdout = OutputToStdout(false);
637676
let test_builder_wrappers =
638677
matches.opt_strs("test-builder-wrapper").iter().map(PathBuf::from).collect();
639678
let output = match (matches.opt_str("out-dir"), matches.opt_str("output")) {
640679
(Some(_), Some(_)) => {
641680
dcx.fatal("cannot use both 'out-dir' and 'output' at once");
642681
}
643682
(Some(out_dir), None) | (None, Some(out_dir)) => {
644-
output_to_stdout = out_dir == "-";
683+
output_to_stdout.0 = out_dir == "-";
645684
PathBuf::from(out_dir)
646685
}
647686
(None, None) => PathBuf::from("doc"),
@@ -773,11 +812,11 @@ impl Options {
773812
ModuleSorting::Alphabetical
774813
};
775814
let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
776-
let markdown_no_toc = matches.opt_present("markdown-no-toc");
815+
let markdown_no_toc = MarkdownNoToc(matches.opt_present("markdown-no-toc"));
777816
let markdown_css = matches.opt_strs("markdown-css");
778817
let markdown_playground_url = matches.opt_str("markdown-playground-url");
779818
let crate_version = matches.opt_str("crate-version");
780-
let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
819+
let enable_index_page = EnableIndexPage(matches.opt_present("enable-index-page") || index_page.is_some());
781820
let static_root_path = matches.opt_str("static-root-path");
782821
let test_run_directory = matches.opt_str("test-run-directory").map(PathBuf::from);
783822
let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from);
@@ -788,30 +827,30 @@ impl Options {
788827
let extern_strs = matches.opt_strs("extern");
789828
let test_runtool = matches.opt_str("test-runtool");
790829
let test_runtool_args = matches.opt_strs("test-runtool-arg");
791-
let document_private = matches.opt_present("document-private-items");
792-
let document_hidden = matches.opt_present("document-hidden-items");
830+
let document_private = DocumentPrivate(matches.opt_present("document-private-items"));
831+
let document_hidden = DocumentHidden(matches.opt_present("document-hidden-items"));
793832
let run_check = matches.opt_present("check");
794-
let generate_redirect_map = matches.opt_present("generate-redirect-map");
795-
let show_type_layout = matches.opt_present("show-type-layout");
833+
let generate_redirect_map = GenerateRedirectMap(matches.opt_present("generate-redirect-map"));
834+
let show_type_layout = ShowTypeLayout(matches.opt_present("show-type-layout"));
796835
let nocapture = matches.opt_present("nocapture");
797-
let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
798-
let generate_macro_expansion = matches.opt_present("generate-macro-expansion");
836+
let generate_link_to_definition = GenerateLinkToDefinition(matches.opt_present("generate-link-to-definition"));
837+
let generate_macro_expansion = GenerateMacroExpansion(matches.opt_present("generate-macro-expansion"));
799838
let extern_html_root_takes_precedence =
800-
matches.opt_present("extern-html-root-takes-precedence");
801-
let html_no_source = matches.opt_present("html-no-source");
839+
ExternHtmlRootTakesPrecedence(matches.opt_present("extern-html-root-takes-precedence"));
840+
let html_no_source = HtmlNoSource(matches.opt_present("html-no-source"));
802841
let should_merge = match parse_merge(matches) {
803842
Ok(result) => result,
804843
Err(e) => dcx.fatal(format!("--merge option error: {e}")),
805844
};
806845

807-
if generate_link_to_definition && (show_coverage || output_format != OutputFormat::Html) {
846+
if generate_link_to_definition.0 && (show_coverage || output_format != OutputFormat::Html) {
808847
dcx.struct_warn(
809848
"`--generate-link-to-definition` option can only be used with HTML output format",
810849
)
811850
.with_note("`--generate-link-to-definition` option will be ignored")
812851
.emit();
813852
}
814-
if generate_macro_expansion && (show_coverage || output_format != OutputFormat::Html) {
853+
if generate_macro_expansion.0 && (show_coverage || output_format != OutputFormat::Html) {
815854
dcx.struct_warn(
816855
"`--generate-macro-expansion` option can only be used with HTML output format",
817856
)
@@ -828,7 +867,7 @@ impl Options {
828867
let unstable_features =
829868
rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
830869

831-
let disable_minification = matches.opt_present("disable-minification");
870+
let disable_minification = DisableMinification(matches.opt_present("disable-minification"));
832871

833872
let options = Options {
834873
bin_crate,
@@ -901,7 +940,7 @@ impl Options {
901940
generate_link_to_definition,
902941
generate_macro_expansion,
903942
call_locations,
904-
no_emit_shared: false,
943+
no_emit_shared: NoEmitShared(false),
905944
html_no_source,
906945
output_to_stdout,
907946
should_merge,

src/librustdoc/core.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use tracing::{debug, info};
2929

3030
use crate::clean::inline::build_trait;
3131
use crate::clean::{self, ItemId};
32-
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
32+
use crate::config::{DocumentPrivate, Options as RustdocOptions, OutputFormat, RenderOptions};
3333
use crate::formats::cache::Cache;
3434
use crate::html::macro_expansion::{ExpandedCode, source_macro_expansion};
3535
use crate::passes;
@@ -244,7 +244,7 @@ pub(crate) fn create_config(
244244

245245
let crate_types =
246246
if proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };
247-
let resolve_doc_links = if render_options.document_private {
247+
let resolve_doc_links = if render_options.document_private.0 {
248248
ResolveDocLinks::All
249249
} else {
250250
ResolveDocLinks::Exported
@@ -420,7 +420,7 @@ pub(crate) fn run_global_ctxt(
420420
// with the passes which we are supposed to run.
421421
for attr in krate.module.attrs.lists(sym::doc) {
422422
if attr.is_word() && attr.has_name(sym::document_private_items) {
423-
ctxt.render_options.document_private = true;
423+
ctxt.render_options.document_private = DocumentPrivate(true);
424424
}
425425
}
426426

@@ -432,9 +432,9 @@ pub(crate) fn run_global_ctxt(
432432
for p in passes::defaults(show_coverage) {
433433
let run = match p.condition {
434434
Always => true,
435-
WhenDocumentPrivate => ctxt.render_options.document_private,
436-
WhenNotDocumentPrivate => !ctxt.render_options.document_private,
437-
WhenNotDocumentHidden => !ctxt.render_options.document_hidden,
435+
WhenDocumentPrivate => ctxt.render_options.document_private.0,
436+
WhenNotDocumentPrivate => !ctxt.render_options.document_private.0,
437+
WhenNotDocumentHidden => !ctxt.render_options.document_hidden.0,
438438
};
439439
if run {
440440
debug!("running pass {}", p.pass.name);

src/librustdoc/formats/cache.rs

Lines changed: 4 additions & 3 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::{DocumentHidden, DocumentPrivate};
1314
use crate::core::DocContext;
1415
use crate::fold::DocFolder;
1516
use crate::formats::Impl;
@@ -88,10 +89,10 @@ pub(crate) struct Cache {
8889

8990
/// Whether to document private items.
9091
/// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
91-
pub(crate) document_private: bool,
92+
pub(crate) document_private: DocumentPrivate,
9293
/// Whether to document hidden items.
9394
/// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
94-
pub(crate) document_hidden: bool,
95+
pub(crate) document_hidden: DocumentHidden,
9596

9697
/// Crates marked with [`#[doc(masked)]`][doc_masked].
9798
///
@@ -142,7 +143,7 @@ struct CacheBuilder<'a, 'tcx> {
142143
}
143144

144145
impl Cache {
145-
pub(crate) fn new(document_private: bool, document_hidden: bool) -> Self {
146+
pub(crate) fn new(document_private: DocumentPrivate, document_hidden: DocumentHidden) -> Self {
146147
Cache { document_private, document_hidden, ..Cache::default() }
147148
}
148149

src/librustdoc/html/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ pub(crate) fn href_with_root_path(
563563
return Err(HrefError::Private);
564564
}
565565
} else if !cache.effective_visibilities.is_directly_public(tcx, did)
566-
&& !cache.document_private
566+
&& !cache.document_private.0
567567
&& !cache.primitive_locations.values().any(|&id| id == did)
568568
{
569569
return Err(HrefError::Private);

src/librustdoc/html/macro_expansion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub(crate) fn source_macro_expansion(
1414
source_map: &SourceMap,
1515
) -> FxHashMap<BytePos, Vec<ExpandedCode>> {
1616
if output_format == OutputFormat::Html
17-
&& !render_options.html_no_source
18-
&& render_options.generate_macro_expansion
17+
&& !render_options.html_no_source.0
18+
&& render_options.generate_macro_expansion.0
1919
{
2020
let mut expanded_visitor = ExpandedCodeVisitor { expanded_codes: Vec::new(), source_map };
2121
walk_crate(&mut expanded_visitor, krate);

0 commit comments

Comments
 (0)