Skip to content

Commit dca8dde

Browse files
authored
Rollup merge of #91846 - camelid:doctest-cleanup, r=GuillaumeGomez
rustdoc: Reduce number of arguments for `run_test` a bit - rustdoc: Coalesce some `run_test` args as one `LangString` arg - Rename `TestOptions` to `GlobalTestOptions` - doctest: Rename `options` to `rustdoc_options`
2 parents e354f0c + 9afa190 commit dca8dde

File tree

3 files changed

+64
-69
lines changed

3 files changed

+64
-69
lines changed

Diff for: src/librustdoc/doctest.rs

+45-50
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ use std::sync::atomic::{AtomicUsize, Ordering};
2929
use std::sync::{Arc, Mutex};
3030

3131
use crate::clean::{types::AttributesExt, Attributes};
32-
use crate::config::Options;
32+
use crate::config::Options as RustdocOptions;
3333
use crate::html::markdown::{self, ErrorCodes, Ignore, LangString};
3434
use crate::lint::init_lints;
3535
use crate::passes::span_of_attrs;
3636

37+
/// Options that apply to all doctests in a crate or Markdown file (for `rustdoc foo.md`).
3738
#[derive(Clone, Default)]
38-
crate struct TestOptions {
39+
crate struct GlobalTestOptions {
3940
/// Whether to disable the default `extern crate my_crate;` when creating doctests.
4041
crate no_crate_inject: bool,
4142
/// Additional crate-level attributes to add to doctests.
4243
crate attrs: Vec<String>,
4344
}
4445

45-
crate fn run(options: Options) -> Result<(), ErrorReported> {
46+
crate fn run(options: RustdocOptions) -> Result<(), ErrorReported> {
4647
let input = config::Input::File(options.input.clone());
4748

4849
let invalid_codeblock_attributes_name = crate::lint::INVALID_CODEBLOCK_ATTRIBUTES.name;
@@ -214,10 +215,10 @@ crate fn run_tests(mut test_args: Vec<String>, nocapture: bool, tests: Vec<test:
214215
}
215216

216217
// Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.
217-
fn scrape_test_config(attrs: &[ast::Attribute]) -> TestOptions {
218+
fn scrape_test_config(attrs: &[ast::Attribute]) -> GlobalTestOptions {
218219
use rustc_ast_pretty::pprust;
219220

220-
let mut opts = TestOptions { no_crate_inject: false, attrs: Vec::new() };
221+
let mut opts = GlobalTestOptions { no_crate_inject: false, attrs: Vec::new() };
221222

222223
let test_attrs: Vec<_> = attrs
223224
.iter()
@@ -292,66 +293,63 @@ fn run_test(
292293
test: &str,
293294
crate_name: &str,
294295
line: usize,
295-
options: Options,
296-
should_panic: bool,
296+
rustdoc_options: RustdocOptions,
297+
mut lang_string: LangString,
297298
no_run: bool,
298-
as_test_harness: bool,
299299
runtool: Option<String>,
300300
runtool_args: Vec<String>,
301301
target: TargetTriple,
302-
compile_fail: bool,
303-
mut error_codes: Vec<String>,
304-
opts: &TestOptions,
302+
opts: &GlobalTestOptions,
305303
edition: Edition,
306304
outdir: DirState,
307305
path: PathBuf,
308306
test_id: &str,
309307
report_unused_externs: impl Fn(UnusedExterns),
310308
) -> Result<(), TestFailure> {
311309
let (test, line_offset, supports_color) =
312-
make_test(test, Some(crate_name), as_test_harness, opts, edition, Some(test_id));
310+
make_test(test, Some(crate_name), lang_string.test_harness, opts, edition, Some(test_id));
313311

314312
let output_file = outdir.path().join("rust_out");
315313

316-
let rustc_binary = options
314+
let rustc_binary = rustdoc_options
317315
.test_builder
318316
.as_deref()
319317
.unwrap_or_else(|| rustc_interface::util::rustc_path().expect("found rustc"));
320318
let mut compiler = Command::new(&rustc_binary);
321319
compiler.arg("--crate-type").arg("bin");
322-
for cfg in &options.cfgs {
320+
for cfg in &rustdoc_options.cfgs {
323321
compiler.arg("--cfg").arg(&cfg);
324322
}
325-
if let Some(sysroot) = options.maybe_sysroot {
323+
if let Some(sysroot) = rustdoc_options.maybe_sysroot {
326324
compiler.arg("--sysroot").arg(sysroot);
327325
}
328326
compiler.arg("--edition").arg(&edition.to_string());
329327
compiler.env("UNSTABLE_RUSTDOC_TEST_PATH", path);
330328
compiler.env("UNSTABLE_RUSTDOC_TEST_LINE", format!("{}", line as isize - line_offset as isize));
331329
compiler.arg("-o").arg(&output_file);
332-
if as_test_harness {
330+
if lang_string.test_harness {
333331
compiler.arg("--test");
334332
}
335-
if options.json_unused_externs && !compile_fail {
333+
if rustdoc_options.json_unused_externs && !lang_string.compile_fail {
336334
compiler.arg("--error-format=json");
337335
compiler.arg("--json").arg("unused-externs");
338336
compiler.arg("-Z").arg("unstable-options");
339337
compiler.arg("-W").arg("unused_crate_dependencies");
340338
}
341-
for lib_str in &options.lib_strs {
339+
for lib_str in &rustdoc_options.lib_strs {
342340
compiler.arg("-L").arg(&lib_str);
343341
}
344-
for extern_str in &options.extern_strs {
342+
for extern_str in &rustdoc_options.extern_strs {
345343
compiler.arg("--extern").arg(&extern_str);
346344
}
347345
compiler.arg("-Ccodegen-units=1");
348-
for codegen_options_str in &options.codegen_options_strs {
346+
for codegen_options_str in &rustdoc_options.codegen_options_strs {
349347
compiler.arg("-C").arg(&codegen_options_str);
350348
}
351-
for debugging_option_str in &options.debugging_opts_strs {
349+
for debugging_option_str in &rustdoc_options.debugging_opts_strs {
352350
compiler.arg("-Z").arg(&debugging_option_str);
353351
}
354-
if no_run && !compile_fail && options.persist_doctests.is_none() {
352+
if no_run && !lang_string.compile_fail && rustdoc_options.persist_doctests.is_none() {
355353
compiler.arg("--emit=metadata");
356354
}
357355
compiler.arg("--target").arg(match target {
@@ -360,7 +358,7 @@ fn run_test(
360358
path.to_str().expect("target path must be valid unicode").to_string()
361359
}
362360
});
363-
if let ErrorOutputType::HumanReadable(kind) = options.error_format {
361+
if let ErrorOutputType::HumanReadable(kind) = rustdoc_options.error_format {
364362
let (short, color_config) = kind.unzip();
365363

366364
if short {
@@ -418,20 +416,20 @@ fn run_test(
418416

419417
let out = out_lines.join("\n");
420418
let _bomb = Bomb(&out);
421-
match (output.status.success(), compile_fail) {
419+
match (output.status.success(), lang_string.compile_fail) {
422420
(true, true) => {
423421
return Err(TestFailure::UnexpectedCompilePass);
424422
}
425423
(true, false) => {}
426424
(false, true) => {
427-
if !error_codes.is_empty() {
425+
if !lang_string.error_codes.is_empty() {
428426
// We used to check if the output contained "error[{}]: " but since we added the
429427
// colored output, we can't anymore because of the color escape characters before
430428
// the ":".
431-
error_codes.retain(|err| !out.contains(&format!("error[{}]", err)));
429+
lang_string.error_codes.retain(|err| !out.contains(&format!("error[{}]", err)));
432430

433-
if !error_codes.is_empty() {
434-
return Err(TestFailure::MissingErrorCodes(error_codes));
431+
if !lang_string.error_codes.is_empty() {
432+
return Err(TestFailure::MissingErrorCodes(lang_string.error_codes));
435433
}
436434
}
437435
}
@@ -454,11 +452,11 @@ fn run_test(
454452
} else {
455453
cmd = Command::new(output_file);
456454
}
457-
if let Some(run_directory) = options.test_run_directory {
455+
if let Some(run_directory) = rustdoc_options.test_run_directory {
458456
cmd.current_dir(run_directory);
459457
}
460458

461-
let result = if options.nocapture {
459+
let result = if rustdoc_options.nocapture {
462460
cmd.status().map(|status| process::Output {
463461
status,
464462
stdout: Vec::new(),
@@ -470,9 +468,9 @@ fn run_test(
470468
match result {
471469
Err(e) => return Err(TestFailure::ExecutionError(e)),
472470
Ok(out) => {
473-
if should_panic && out.status.success() {
471+
if lang_string.should_panic && out.status.success() {
474472
return Err(TestFailure::UnexpectedRunPass);
475-
} else if !should_panic && !out.status.success() {
473+
} else if !lang_string.should_panic && !out.status.success() {
476474
return Err(TestFailure::ExecutionFailure(out));
477475
}
478476
}
@@ -487,7 +485,7 @@ crate fn make_test(
487485
s: &str,
488486
crate_name: Option<&str>,
489487
dont_insert_main: bool,
490-
opts: &TestOptions,
488+
opts: &GlobalTestOptions,
491489
edition: Edition,
492490
test_id: Option<&str>,
493491
) -> (String, usize, bool) {
@@ -804,11 +802,11 @@ crate struct Collector {
804802
// the `names` vector of that test will be `["Title", "Subtitle"]`.
805803
names: Vec<String>,
806804

807-
options: Options,
805+
rustdoc_options: RustdocOptions,
808806
use_headers: bool,
809807
enable_per_target_ignores: bool,
810808
crate_name: Symbol,
811-
opts: TestOptions,
809+
opts: GlobalTestOptions,
812810
position: Span,
813811
source_map: Option<Lrc<SourceMap>>,
814812
filename: Option<PathBuf>,
@@ -820,17 +818,17 @@ crate struct Collector {
820818
impl Collector {
821819
crate fn new(
822820
crate_name: Symbol,
823-
options: Options,
821+
rustdoc_options: RustdocOptions,
824822
use_headers: bool,
825-
opts: TestOptions,
823+
opts: GlobalTestOptions,
826824
source_map: Option<Lrc<SourceMap>>,
827825
filename: Option<PathBuf>,
828826
enable_per_target_ignores: bool,
829827
) -> Collector {
830828
Collector {
831829
tests: Vec::new(),
832830
names: Vec::new(),
833-
options,
831+
rustdoc_options,
834832
use_headers,
835833
enable_per_target_ignores,
836834
crate_name,
@@ -884,14 +882,14 @@ impl Tester for Collector {
884882
let name = self.generate_name(line, &filename);
885883
let crate_name = self.crate_name.to_string();
886884
let opts = self.opts.clone();
887-
let edition = config.edition.unwrap_or(self.options.edition);
888-
let options = self.options.clone();
889-
let runtool = self.options.runtool.clone();
890-
let runtool_args = self.options.runtool_args.clone();
891-
let target = self.options.target.clone();
885+
let edition = config.edition.unwrap_or(self.rustdoc_options.edition);
886+
let rustdoc_options = self.rustdoc_options.clone();
887+
let runtool = self.rustdoc_options.runtool.clone();
888+
let runtool_args = self.rustdoc_options.runtool_args.clone();
889+
let target = self.rustdoc_options.target.clone();
892890
let target_str = target.to_string();
893891
let unused_externs = self.unused_extern_reports.clone();
894-
let no_run = config.no_run || options.no_run;
892+
let no_run = config.no_run || rustdoc_options.no_run;
895893
if !config.compile_fail {
896894
self.compiling_test_count.fetch_add(1, Ordering::SeqCst);
897895
}
@@ -925,7 +923,7 @@ impl Tester for Collector {
925923
self.visited_tests.entry((file.clone(), line)).and_modify(|v| *v += 1).or_insert(0)
926924
},
927925
);
928-
let outdir = if let Some(mut path) = options.persist_doctests.clone() {
926+
let outdir = if let Some(mut path) = rustdoc_options.persist_doctests.clone() {
929927
path.push(&test_id);
930928

931929
std::fs::create_dir_all(&path)
@@ -965,15 +963,12 @@ impl Tester for Collector {
965963
&test,
966964
&crate_name,
967965
line,
968-
options,
969-
config.should_panic,
966+
rustdoc_options,
967+
config,
970968
no_run,
971-
config.test_harness,
972969
runtool,
973970
runtool_args,
974971
target,
975-
config.compile_fail,
976-
config.error_codes,
977972
&opts,
978973
edition,
979974
outdir,

0 commit comments

Comments
 (0)