Skip to content

Commit

Permalink
Avoid cloning RenderOptions.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nnethercote committed Oct 18, 2022
1 parent 38988e6 commit ca2561a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 47 deletions.
80 changes: 43 additions & 37 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -194,14 +196,14 @@ impl fmt::Debug for Options {
.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)
.field("run_check", &self.run_check)
.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()
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -710,7 +714,9 @@ impl Options {
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,
Expand Down Expand Up @@ -744,42 +750,42 @@ impl Options {
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.
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
extension_css,
resource_suffix,
static_root_path,
unstable_features,
generate_redirect_map,
show_type_layout,
generate_link_to_definition,
Expand Down Expand Up @@ -511,7 +510,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
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,
Expand Down
8 changes: 2 additions & 6 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -815,7 +811,7 @@ fn main_args(at_args: &[String]) -> MainResult {
sess,
krate,
externs,
document_private,
render_options.document_private,
)
});
(resolver.clone(), resolver_caches)
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit ca2561a

Please sign in to comment.