From ca2561a07b01841ceef6790d78e12aaa1d2e3aec Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 17 Oct 2022 10:51:40 +1100 Subject: [PATCH] Avoid cloning `RenderOptions`. By moving `RenderOptions` out of `Option`, because the two structs' uses are almost entirely separate. The only complication is that `unstable_features` is needed in both structs, but it's a tiny `Copy` type so its duplication seems fine. --- src/librustdoc/config.rs | 80 ++++++++++++++------------- src/librustdoc/doctest.rs | 2 +- src/librustdoc/html/render/context.rs | 3 +- src/librustdoc/lib.rs | 8 +-- src/librustdoc/markdown.rs | 2 +- 5 files changed, 48 insertions(+), 47 deletions(-) diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 932533db05c..67ea39fb965 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -142,8 +142,6 @@ pub(crate) struct Options { // Options that alter generated documentation pages /// Crate version to note on the sidebar of generated docs. pub(crate) crate_version: Option, - /// Collected options specific to outputting final pages. - pub(crate) render_options: RenderOptions, /// The format that we output when rendering. /// /// Currently used only for the `--show-coverage` option. @@ -159,6 +157,10 @@ pub(crate) struct Options { /// Configuration for scraping examples from the current crate. If this option is Some(..) then /// the compiler will scrape examples and not generate documentation. pub(crate) scrape_examples_options: Option, + + /// Note: this field is duplicated in `RenderOptions` because it's useful + /// to have it in both places. + pub(crate) unstable_features: rustc_feature::UnstableFeatures, } impl fmt::Debug for Options { @@ -194,7 +196,6 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { .field("persist_doctests", &self.persist_doctests) .field("show_coverage", &self.show_coverage) .field("crate_version", &self.crate_version) - .field("render_options", &self.render_options) .field("runtool", &self.runtool) .field("runtool_args", &self.runtool_args) .field("enable-per-target-ignores", &self.enable_per_target_ignores) @@ -202,6 +203,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { .field("no_run", &self.no_run) .field("nocapture", &self.nocapture) .field("scrape_examples_options", &self.scrape_examples_options) + .field("unstable_features", &self.unstable_features) .finish() } } @@ -267,6 +269,8 @@ pub(crate) struct RenderOptions { pub(crate) generate_redirect_map: bool, /// Show the memory layout of types in the docs. pub(crate) show_type_layout: bool, + /// 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. @@ -316,7 +320,7 @@ impl Options { pub(crate) fn from_matches( matches: &getopts::Matches, args: Vec, - ) -> Result { + ) -> Result<(Options, RenderOptions), i32> { let args = &args[1..]; // Check for unstable options. nightly_options::check_nightly_options(matches, &opts()); @@ -710,7 +714,9 @@ fn println_condition(condition: Condition) { let with_examples = matches.opt_strs("with-examples"); let call_locations = crate::scrape_examples::load_call_locations(with_examples, &diag)?; - Ok(Options { + let unstable_features = + rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref()); + let options = Options { input, proc_macro_crate, error_format, @@ -744,42 +750,42 @@ fn println_condition(condition: Condition) { run_check, no_run, nocapture, - render_options: RenderOptions { - output, - external_html, - id_map, - playground_url, - module_sorting, - themes, - extension_css, - extern_html_root_urls, - extern_html_root_takes_precedence, - default_settings, - resource_suffix, - enable_minification, - enable_index_page, - index_page, - static_root_path, - markdown_no_toc, - markdown_css, - markdown_playground_url, - document_private, - document_hidden, - generate_redirect_map, - show_type_layout, - unstable_features: rustc_feature::UnstableFeatures::from_environment( - crate_name.as_deref(), - ), - emit, - generate_link_to_definition, - call_locations, - no_emit_shared: false, - }, crate_name, output_format, json_unused_externs, scrape_examples_options, - }) + unstable_features, + }; + let render_options = RenderOptions { + output, + external_html, + id_map, + playground_url, + module_sorting, + themes, + extension_css, + extern_html_root_urls, + extern_html_root_takes_precedence, + default_settings, + resource_suffix, + enable_minification, + enable_index_page, + index_page, + static_root_path, + markdown_no_toc, + markdown_css, + markdown_playground_url, + document_private, + document_hidden, + generate_redirect_map, + show_type_layout, + unstable_features, + emit, + generate_link_to_definition, + call_locations, + no_emit_shared: false, + }; + Ok((options, render_options)) } /// Returns `true` if the file given as `self.input` is a Markdown file. diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 58f02e3da55..cb216970c7c 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -79,7 +79,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> { lint_cap: Some(options.lint_cap.unwrap_or(lint::Forbid)), cg: options.codegen_options.clone(), externs: options.externs.clone(), - unstable_features: options.render_options.unstable_features, + unstable_features: options.unstable_features, actually_rustdoc: true, edition: options.edition, target_triple: options.target.clone(), diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index e303dd8bdaf..5733d1f9c79 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -430,7 +430,6 @@ fn init( extension_css, resource_suffix, static_root_path, - unstable_features, generate_redirect_map, show_type_layout, generate_link_to_definition, @@ -511,7 +510,7 @@ fn init( resource_suffix, static_root_path, fs: DocFS::new(sender), - codes: ErrorCodes::from(unstable_features.is_nightly_build()), + codes: ErrorCodes::from(options.unstable_features.is_nightly_build()), playground, all: RefCell::new(AllTypes::new()), errors: receiver, diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index e78b0c9199c..793061a9f7a 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -720,7 +720,7 @@ fn main_args(at_args: &[String]) -> MainResult { // Note that we discard any distinction between different non-zero exit // codes from `from_matches` here. - let options = match config::Options::from_matches(&matches, args) { + let (options, render_options) = match config::Options::from_matches(&matches, args) { Ok(opts) => opts, Err(code) => { return if code == 0 { @@ -743,7 +743,6 @@ fn main_args(at_args: &[String]) -> MainResult { (true, false) => return doctest::run(options), (false, true) => { let input = options.input.clone(); - let render_options = options.render_options.clone(); let edition = options.edition; let config = core::create_config(options); @@ -775,11 +774,8 @@ fn main_args(at_args: &[String]) -> MainResult { let crate_version = options.crate_version.clone(); let output_format = options.output_format; - // FIXME: fix this clone (especially render_options) let externs = options.externs.clone(); - let render_options = options.render_options.clone(); let scrape_examples_options = options.scrape_examples_options.clone(); - let document_private = options.render_options.document_private; let config = core::create_config(options); @@ -815,7 +811,7 @@ fn main_args(at_args: &[String]) -> MainResult { sess, krate, externs, - document_private, + render_options.document_private, ) }); (resolver.clone(), resolver_caches) diff --git a/src/librustdoc/markdown.rs b/src/librustdoc/markdown.rs index eb64ac455dc..044e051440c 100644 --- a/src/librustdoc/markdown.rs +++ b/src/librustdoc/markdown.rs @@ -143,7 +143,7 @@ pub(crate) fn test(options: Options) -> Result<(), String> { options.enable_per_target_ignores, ); collector.set_position(DUMMY_SP); - let codes = ErrorCodes::from(options.render_options.unstable_features.is_nightly_build()); + let codes = ErrorCodes::from(options.unstable_features.is_nightly_build()); find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores, None);