Skip to content

Commit 366000d

Browse files
camelidGuillaumeGomez
authored andcommitted
Move some arguments to fields and reorganize fields
I moved some local arguments and options to either the local options struct or, if it made sense, the global options struct.
1 parent a429afa commit 366000d

File tree

3 files changed

+54
-58
lines changed

3 files changed

+54
-58
lines changed

src/librustdoc/doctest.rs

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@ use crate::lint::init_lints;
4040
use self::rust::HirCollector;
4141

4242
/// Options that apply to all doctests in a crate or Markdown file (for `rustdoc foo.md`).
43-
#[derive(Clone, Default)]
43+
#[derive(Clone)]
4444
pub(crate) struct GlobalTestOptions {
45+
/// Name of the crate (for regular `rustdoc`) or Markdown file (for `rustdoc foo.md`).
46+
pub(crate) crate_name: String,
4547
/// Whether to disable the default `extern crate my_crate;` when creating doctests.
4648
pub(crate) no_crate_inject: bool,
4749
/// Whether inserting extra indent spaces in code block,
4850
/// default is `false`, only `true` for generating code link of Rust playground
4951
pub(crate) insert_indent_space: bool,
5052
/// Additional crate-level attributes to add to doctests.
5153
pub(crate) attrs: Vec<String>,
54+
/// Path to file containing arguments for the invocation of rustc.
55+
pub(crate) args_file: PathBuf,
5256
}
5357

5458
pub(crate) fn generate_args_file(file_path: &Path, options: &RustdocOptions) -> Result<(), String> {
@@ -167,24 +171,19 @@ pub(crate) fn run(
167171
Ok(temp_dir) => temp_dir,
168172
Err(error) => return crate::wrap_return(dcx, Err(error)),
169173
};
170-
let file_path = temp_dir.path().join("rustdoc-cfgs");
171-
crate::wrap_return(dcx, generate_args_file(&file_path, &options))?;
174+
let args_path = temp_dir.path().join("rustdoc-cfgs");
175+
crate::wrap_return(dcx, generate_args_file(&args_path, &options))?;
172176

173177
let (tests, unused_extern_reports, compiling_test_count) =
174178
interface::run_compiler(config, |compiler| {
175179
compiler.enter(|queries| {
176180
let collector = queries.global_ctxt()?.enter(|tcx| {
181+
let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
177182
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
178-
179-
let opts = scrape_test_config(crate_attrs);
183+
let opts = scrape_test_config(crate_name, crate_attrs, args_path);
180184
let enable_per_target_ignores = options.enable_per_target_ignores;
181-
let mut collector = CreateRunnableDoctests::new(
182-
tcx.crate_name(LOCAL_CRATE).to_string(),
183-
options,
184-
opts,
185-
file_path,
186-
);
187185

186+
let mut collector = CreateRunnableDoctests::new(options, opts);
188187
let hir_collector = HirCollector::new(
189188
&compiler.sess,
190189
tcx.hir(),
@@ -264,11 +263,20 @@ pub(crate) fn run_tests(
264263
}
265264

266265
// Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.
267-
fn scrape_test_config(attrs: &[ast::Attribute]) -> GlobalTestOptions {
266+
fn scrape_test_config(
267+
crate_name: String,
268+
attrs: &[ast::Attribute],
269+
args_file: PathBuf,
270+
) -> GlobalTestOptions {
268271
use rustc_ast_pretty::pprust;
269272

270-
let mut opts =
271-
GlobalTestOptions { no_crate_inject: false, attrs: Vec::new(), insert_indent_space: false };
273+
let mut opts = GlobalTestOptions {
274+
crate_name,
275+
no_crate_inject: false,
276+
attrs: Vec::new(),
277+
insert_indent_space: false,
278+
args_file,
279+
};
272280

273281
let test_attrs: Vec<_> = attrs
274282
.iter()
@@ -363,20 +371,18 @@ fn wrapped_rustc_command(rustc_wrappers: &[PathBuf], rustc_binary: &Path) -> Com
363371

364372
fn run_test(
365373
test: &str,
366-
crate_name: &str,
367374
line: usize,
368375
rustdoc_options: &RustdocOptions,
369376
test_options: IndividualTestOptions,
370377
mut lang_string: LangString,
371378
no_run: bool,
372379
opts: &GlobalTestOptions,
373380
edition: Edition,
374-
path: PathBuf,
375381
report_unused_externs: impl Fn(UnusedExterns),
376382
) -> Result<(), TestFailure> {
377383
let (test, line_offset, supports_color) = make_test(
378384
test,
379-
Some(crate_name),
385+
Some(&opts.crate_name),
380386
lang_string.test_harness,
381387
opts,
382388
edition,
@@ -393,14 +399,14 @@ fn run_test(
393399
.unwrap_or_else(|| rustc_interface::util::rustc_path().expect("found rustc"));
394400
let mut compiler = wrapped_rustc_command(&rustdoc_options.test_builder_wrappers, rustc_binary);
395401

396-
compiler.arg(&format!("@{}", test_options.arg_file.display()));
402+
compiler.arg(&format!("@{}", opts.args_file.display()));
397403

398404
if let Some(sysroot) = &rustdoc_options.maybe_sysroot {
399405
compiler.arg(format!("--sysroot={}", sysroot.display()));
400406
}
401407

402408
compiler.arg("--edition").arg(&edition.to_string());
403-
compiler.env("UNSTABLE_RUSTDOC_TEST_PATH", path);
409+
compiler.env("UNSTABLE_RUSTDOC_TEST_PATH", &test_options.path);
404410
compiler.env("UNSTABLE_RUSTDOC_TEST_LINE", format!("{}", line as isize - line_offset as isize));
405411
compiler.arg("-o").arg(&output_file);
406412
if lang_string.test_harness {
@@ -926,13 +932,13 @@ fn partition_source(s: &str, edition: Edition) -> (String, String, String) {
926932
}
927933

928934
pub(crate) struct IndividualTestOptions {
929-
arg_file: PathBuf,
930935
outdir: DirState,
931936
test_id: String,
937+
path: PathBuf,
932938
}
933939

934940
impl IndividualTestOptions {
935-
fn new(options: &RustdocOptions, arg_file: &Path, test_id: String) -> Self {
941+
fn new(options: &RustdocOptions, test_id: String, test_path: PathBuf) -> Self {
936942
let outdir = if let Some(ref path) = options.persist_doctests {
937943
let mut path = path.clone();
938944
path.push(&test_id);
@@ -947,7 +953,7 @@ impl IndividualTestOptions {
947953
DirState::Temp(get_doctest_dir().expect("rustdoc needs a tempdir"))
948954
};
949955

950-
Self { arg_file: arg_file.into(), outdir, test_id }
956+
Self { outdir, test_id, path: test_path }
951957
}
952958
}
953959

@@ -979,30 +985,24 @@ pub(crate) struct CreateRunnableDoctests {
979985
pub(crate) tests: Vec<test::TestDescAndFn>,
980986

981987
rustdoc_options: Arc<RustdocOptions>,
982-
crate_name: String,
983988
opts: GlobalTestOptions,
984989
visited_tests: FxHashMap<(String, usize), usize>,
985990
unused_extern_reports: Arc<Mutex<Vec<UnusedExterns>>>,
986991
compiling_test_count: AtomicUsize,
987-
arg_file: PathBuf,
988992
}
989993

990994
impl CreateRunnableDoctests {
991995
pub(crate) fn new(
992-
crate_name: String,
993996
rustdoc_options: RustdocOptions,
994997
opts: GlobalTestOptions,
995-
arg_file: PathBuf,
996998
) -> CreateRunnableDoctests {
997999
CreateRunnableDoctests {
9981000
tests: Vec::new(),
9991001
rustdoc_options: Arc::new(rustdoc_options),
1000-
crate_name,
10011002
opts,
10021003
visited_tests: FxHashMap::default(),
10031004
unused_extern_reports: Default::default(),
10041005
compiling_test_count: AtomicUsize::new(0),
1005-
arg_file,
10061006
}
10071007
}
10081008

@@ -1017,7 +1017,6 @@ impl CreateRunnableDoctests {
10171017

10181018
fn add_test(&mut self, test: ScrapedDoctest) {
10191019
let name = self.generate_name(&test.filename, test.line, &test.logical_path);
1020-
let crate_name = self.crate_name.clone();
10211020
let opts = self.opts.clone();
10221021
let target_str = self.rustdoc_options.target.to_string();
10231022
let unused_externs = self.unused_extern_reports.clone();
@@ -1060,8 +1059,7 @@ impl CreateRunnableDoctests {
10601059
);
10611060

10621061
let rustdoc_options = self.rustdoc_options.clone();
1063-
let rustdoc_test_options =
1064-
IndividualTestOptions::new(&self.rustdoc_options, &self.arg_file, test_id);
1062+
let rustdoc_test_options = IndividualTestOptions::new(&self.rustdoc_options, test_id, path);
10651063

10661064
debug!("creating test {name}: {}", test.text);
10671065
self.tests.push(test::TestDescAndFn {
@@ -1085,25 +1083,15 @@ impl CreateRunnableDoctests {
10851083
test_type: test::TestType::DocTest,
10861084
},
10871085
testfn: test::DynTestFn(Box::new(move || {
1088-
doctest_run_fn(
1089-
crate_name,
1090-
rustdoc_test_options,
1091-
opts,
1092-
path,
1093-
test,
1094-
rustdoc_options,
1095-
unused_externs,
1096-
)
1086+
doctest_run_fn(rustdoc_test_options, opts, test, rustdoc_options, unused_externs)
10971087
})),
10981088
});
10991089
}
11001090
}
11011091

11021092
fn doctest_run_fn(
1103-
crate_name: String,
11041093
test_opts: IndividualTestOptions,
11051094
global_opts: GlobalTestOptions,
1106-
path: PathBuf,
11071095
scraped_test: ScrapedDoctest,
11081096
rustdoc_options: Arc<RustdocOptions>,
11091097
unused_externs: Arc<Mutex<Vec<UnusedExterns>>>,
@@ -1115,15 +1103,13 @@ fn doctest_run_fn(
11151103
let edition = scraped_test.edition(&rustdoc_options);
11161104
let res = run_test(
11171105
&scraped_test.text,
1118-
&crate_name,
11191106
scraped_test.line,
11201107
&rustdoc_options,
11211108
test_opts,
11221109
scraped_test.langstr,
11231110
no_run,
11241111
&global_opts,
11251112
edition,
1126-
path,
11271113
report_unused_externs,
11281114
);
11291115

src/librustdoc/doctest/markdown.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl DoctestVisitor for MdCollector {
7373
}
7474
}
7575

76-
/// Runs any tests/code examples in the markdown file `input`.
76+
/// Runs any tests/code examples in the markdown file `options.input`.
7777
pub(crate) fn test(options: Options) -> Result<(), String> {
7878
use rustc_session::config::Input;
7979
let input_str = match &options.input {
@@ -83,13 +83,20 @@ pub(crate) fn test(options: Options) -> Result<(), String> {
8383
Input::Str { name: _, input } => input.clone(),
8484
};
8585

86-
let mut opts = GlobalTestOptions::default();
87-
opts.no_crate_inject = true;
88-
86+
// Obviously not a real crate name, but close enough for purposes of doctests.
87+
let crate_name = options.input.filestem().to_string();
8988
let temp_dir =
9089
tempdir().map_err(|error| format!("failed to create temporary directory: {error:?}"))?;
91-
let file_path = temp_dir.path().join("rustdoc-cfgs");
92-
generate_args_file(&file_path, &options)?;
90+
let args_file = temp_dir.path().join("rustdoc-cfgs");
91+
generate_args_file(&args_file, &options)?;
92+
93+
let opts = GlobalTestOptions {
94+
crate_name,
95+
no_crate_inject: true,
96+
insert_indent_space: false,
97+
attrs: vec![],
98+
args_file,
99+
};
93100

94101
let mut md_collector = MdCollector {
95102
tests: vec![],
@@ -111,12 +118,7 @@ pub(crate) fn test(options: Options) -> Result<(), String> {
111118
None,
112119
);
113120

114-
let mut collector = CreateRunnableDoctests::new(
115-
options.input.filestem().to_string(),
116-
options.clone(),
117-
opts,
118-
file_path,
119-
);
121+
let mut collector = CreateRunnableDoctests::new(options.clone(), opts);
120122
md_collector.tests.into_iter().for_each(|t| collector.add_test(t));
121123
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
122124
Ok(())

src/librustdoc/html/markdown.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use std::collections::VecDeque;
3939
use std::fmt::Write;
4040
use std::iter::Peekable;
4141
use std::ops::{ControlFlow, Range};
42+
use std::path::PathBuf;
4243
use std::str::{self, CharIndices};
4344
use std::sync::OnceLock;
4445

@@ -287,8 +288,15 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
287288
.collect::<String>();
288289
let krate = krate.as_ref().map(|s| s.as_str());
289290

290-
let mut opts: GlobalTestOptions = Default::default();
291-
opts.insert_indent_space = true;
291+
// FIXME: separate out the code to make a code block into runnable code
292+
// from the complicated doctest logic
293+
let opts = GlobalTestOptions {
294+
crate_name: krate.map(String::from).unwrap_or_default(),
295+
no_crate_inject: false,
296+
insert_indent_space: true,
297+
attrs: vec![],
298+
args_file: PathBuf::new(),
299+
};
292300
let (test, _, _) = doctest::make_test(&test, krate, false, &opts, edition, None);
293301
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };
294302

0 commit comments

Comments
 (0)