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.
This commit is contained in:
Nicholas Nethercote 2022-10-17 10:51:40 +11:00
parent 38988e62bc
commit ca2561a07b
5 changed files with 48 additions and 47 deletions

View File

@ -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<String>,
/// 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<ScrapeExamplesOptions>,
/// 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<EmitType>,
/// 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<String>,
) -> Result<Options, i32> {
) -> 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.

View File

@ -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(),

View File

@ -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,

View File

@ -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)

View File

@ -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);