Skip to content

Commit 3782251

Browse files
EtomicBombethan
authored and
ethan
committed
librustdoc::config: removed Input from Options
The `librustdoc::config::Options` struct no longer includes `rustc_session::config::Input`. This is so that Input can be optional. In rfc#3662, the crate input is not required if `--merge=finalize`. Replacing Input with Option<Input> was decided against. In most places that Input is needed, it should be statically known to not be optional (means fewer unwraps). We just want to have an Input-free Options in librustdoc::main_args, where we can run the write shared procedure.
1 parent ec867f0 commit 3782251

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

src/librustdoc/config.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ impl TryFrom<&str> for OutputFormat {
5757
#[derive(Clone)]
5858
pub(crate) struct Options {
5959
// Basic options / Options passed directly to rustc
60-
/// The crate root or Markdown file to load.
61-
pub(crate) input: Input,
6260
/// The name of the crate being documented.
6361
pub(crate) crate_name: Option<String>,
6462
/// Whether or not this is a bin crate
@@ -179,7 +177,6 @@ impl fmt::Debug for Options {
179177
}
180178

181179
f.debug_struct("Options")
182-
.field("input", &self.input.source_name())
183180
.field("crate_name", &self.crate_name)
184181
.field("bin_crate", &self.bin_crate)
185182
.field("proc_macro_crate", &self.proc_macro_crate)
@@ -348,7 +345,7 @@ impl Options {
348345
early_dcx: &mut EarlyDiagCtxt,
349346
matches: &getopts::Matches,
350347
args: Vec<String>,
351-
) -> Option<(Options, RenderOptions)> {
348+
) -> Option<(Input, Options, RenderOptions)> {
352349
// Check for unstable options.
353350
nightly_options::check_nightly_options(early_dcx, matches, &opts());
354351

@@ -751,7 +748,6 @@ impl Options {
751748
let unstable_features =
752749
rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
753750
let options = Options {
754-
input,
755751
bin_crate,
756752
proc_macro_crate,
757753
error_format,
@@ -824,15 +820,13 @@ impl Options {
824820
html_no_source,
825821
output_to_stdout,
826822
};
827-
Some((options, render_options))
823+
Some((input, options, render_options))
828824
}
825+
}
829826

830-
/// Returns `true` if the file given as `self.input` is a Markdown file.
831-
pub(crate) fn markdown_input(&self) -> Option<&Path> {
832-
self.input
833-
.opt_path()
834-
.filter(|p| matches!(p.extension(), Some(e) if e == "md" || e == "markdown"))
835-
}
827+
/// Returns `true` if the file given as `self.input` is a Markdown file.
828+
pub(crate) fn markdown_input(input: &Input) -> Option<&Path> {
829+
input.opt_path().filter(|p| matches!(p.extension(), Some(e) if e == "md" || e == "markdown"))
836830
}
837831

838832
fn parse_remap_path_prefix(

src/librustdoc/core.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_interface::interface;
2020
use rustc_lint::{late_lint_mod, MissingDoc};
2121
use rustc_middle::hir::nested_filter;
2222
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
23-
use rustc_session::config::{self, CrateType, ErrorOutputType, ResolveDocLinks};
23+
use rustc_session::config::{self, CrateType, ErrorOutputType, Input, ResolveDocLinks};
2424
pub(crate) use rustc_session::config::{Options, UnstableOptions};
2525
use rustc_session::{lint, Session};
2626
use rustc_span::symbol::sym;
@@ -177,8 +177,8 @@ pub(crate) fn new_dcx(
177177

178178
/// Parse, resolve, and typecheck the given crate.
179179
pub(crate) fn create_config(
180+
input: Input,
180181
RustdocOptions {
181-
input,
182182
crate_name,
183183
proc_macro_crate,
184184
error_format,

src/librustdoc/doctest.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_errors::{ColorConfig, DiagCtxtHandle, ErrorGuaranteed, FatalError};
1919
use rustc_hir::def_id::LOCAL_CRATE;
2020
use rustc_hir::CRATE_HIR_ID;
2121
use rustc_interface::interface;
22-
use rustc_session::config::{self, CrateType, ErrorOutputType};
22+
use rustc_session::config::{self, CrateType, ErrorOutputType, Input};
2323
use rustc_session::lint;
2424
use rustc_span::edition::Edition;
2525
use rustc_span::symbol::sym;
@@ -88,7 +88,11 @@ fn get_doctest_dir() -> io::Result<TempDir> {
8888
TempFileBuilder::new().prefix("rustdoctest").tempdir()
8989
}
9090

91-
pub(crate) fn run(dcx: DiagCtxtHandle<'_>, options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
91+
pub(crate) fn run(
92+
dcx: DiagCtxtHandle<'_>,
93+
input: Input,
94+
options: RustdocOptions,
95+
) -> Result<(), ErrorGuaranteed> {
9296
let invalid_codeblock_attributes_name = crate::lint::INVALID_CODEBLOCK_ATTRIBUTES.name;
9397

9498
// See core::create_config for what's going on here.
@@ -135,7 +139,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, options: RustdocOptions) -> Result<()
135139
opts: sessopts,
136140
crate_cfg: cfgs,
137141
crate_check_cfg: options.check_cfgs.clone(),
138-
input: options.input.clone(),
142+
input: input.clone(),
139143
output_file: None,
140144
output_dir: None,
141145
file_loader: None,

src/librustdoc/doctest/markdown.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::fs::read_to_string;
44
use std::sync::{Arc, Mutex};
55

6+
use rustc_session::config::Input;
67
use rustc_span::FileName;
78
use tempfile::tempdir;
89

@@ -69,17 +70,16 @@ impl DocTestVisitor for MdCollector {
6970
}
7071

7172
/// Runs any tests/code examples in the markdown file `options.input`.
72-
pub(crate) fn test(options: Options) -> Result<(), String> {
73-
use rustc_session::config::Input;
74-
let input_str = match &options.input {
73+
pub(crate) fn test(input: &Input, options: Options) -> Result<(), String> {
74+
let input_str = match input {
7575
Input::File(path) => {
7676
read_to_string(path).map_err(|err| format!("{}: {err}", path.display()))?
7777
}
7878
Input::Str { name: _, input } => input.clone(),
7979
};
8080

8181
// Obviously not a real crate name, but close enough for purposes of doctests.
82-
let crate_name = options.input.filestem().to_string();
82+
let crate_name = input.filestem().to_string();
8383
let temp_dir =
8484
tempdir().map_err(|error| format!("failed to create temporary directory: {error:?}"))?;
8585
let args_file = temp_dir.path().join("rustdoc-cfgs");
@@ -96,8 +96,7 @@ pub(crate) fn test(options: Options) -> Result<(), String> {
9696
let mut md_collector = MdCollector {
9797
tests: vec![],
9898
cur_path: vec![],
99-
filename: options
100-
.input
99+
filename: input
101100
.opt_path()
102101
.map(ToOwned::to_owned)
103102
.map(FileName::from)

src/librustdoc/lib.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -727,30 +727,32 @@ fn main_args(
727727

728728
// Note that we discard any distinction between different non-zero exit
729729
// codes from `from_matches` here.
730-
let (options, render_options) = match config::Options::from_matches(early_dcx, &matches, args) {
731-
Some(opts) => opts,
732-
None => return Ok(()),
733-
};
730+
let (input, options, render_options) =
731+
match config::Options::from_matches(early_dcx, &matches, args) {
732+
Some(opts) => opts,
733+
None => return Ok(()),
734+
};
734735

735736
let dcx =
736737
core::new_dcx(options.error_format, None, options.diagnostic_width, &options.unstable_opts);
737738
let dcx = dcx.handle();
738739

739-
match (options.should_test, options.markdown_input()) {
740-
(true, Some(_)) => return wrap_return(dcx, doctest::test_markdown(options)),
741-
(true, None) => return doctest::run(dcx, options),
742-
(false, Some(input)) => {
743-
let input = input.to_owned();
740+
match (options.should_test, config::markdown_input(&input)) {
741+
(true, Some(_)) => return wrap_return(dcx, doctest::test_markdown(&input, options)),
742+
(true, None) => return doctest::run(dcx, input, options),
743+
(false, Some(md_input)) => {
744+
let md_input = md_input.to_owned();
744745
let edition = options.edition;
745-
let config = core::create_config(options, &render_options, using_internal_features);
746+
let config =
747+
core::create_config(input, options, &render_options, using_internal_features);
746748

747749
// `markdown::render` can invoke `doctest::make_test`, which
748750
// requires session globals and a thread pool, so we use
749751
// `run_compiler`.
750752
return wrap_return(
751753
dcx,
752754
interface::run_compiler(config, |_compiler| {
753-
markdown::render(&input, render_options, edition)
755+
markdown::render(&md_input, render_options, edition)
754756
}),
755757
);
756758
}
@@ -775,7 +777,7 @@ fn main_args(
775777
let scrape_examples_options = options.scrape_examples_options.clone();
776778
let bin_crate = options.bin_crate;
777779

778-
let config = core::create_config(options, &render_options, using_internal_features);
780+
let config = core::create_config(input, options, &render_options, using_internal_features);
779781

780782
interface::run_compiler(config, |compiler| {
781783
let sess = &compiler.sess;

0 commit comments

Comments
 (0)