Skip to content

Commit 7854e6f

Browse files
committed
Merge RustDoctest and MdDoctest into one type
1 parent 63e4bee commit 7854e6f

File tree

3 files changed

+42
-77
lines changed

3 files changed

+42
-77
lines changed

src/librustdoc/doctest.rs

+35-54
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ use crate::config::Options as RustdocOptions;
3737
use crate::html::markdown::{ErrorCodes, Ignore, LangString};
3838
use crate::lint::init_lints;
3939

40-
use self::markdown::MdDoctest;
41-
use self::rust::{HirCollector, RustDoctest};
40+
use self::rust::HirCollector;
4241

4342
/// Options that apply to all doctests in a crate or Markdown file (for `rustdoc foo.md`).
4443
#[derive(Clone, Default)]
@@ -194,7 +193,7 @@ pub(crate) fn run(
194193
tcx,
195194
);
196195
let tests = hir_collector.collect_crate();
197-
tests.into_iter().for_each(|t| collector.add_test(ScrapedDoctest::Rust(t)));
196+
tests.into_iter().for_each(|t| collector.add_test(t));
198197

199198
collector
200199
});
@@ -977,9 +976,12 @@ impl IndividualTestOptions {
977976
}
978977

979978
/// A doctest scraped from the code, ready to be turned into a runnable test.
980-
enum ScrapedDoctest {
981-
Rust(RustDoctest),
982-
Markdown(MdDoctest),
979+
struct ScrapedDoctest {
980+
filename: FileName,
981+
line: usize,
982+
logical_path: Vec<String>,
983+
langstr: LangString,
984+
text: String,
983985
}
984986

985987
pub(crate) trait DoctestVisitor {
@@ -1031,27 +1033,18 @@ impl CreateRunnableDoctests {
10311033
}
10321034

10331035
fn add_test(&mut self, test: ScrapedDoctest) {
1034-
let (filename, line, logical_path, langstr, text) = match test {
1035-
ScrapedDoctest::Rust(RustDoctest { filename, line, logical_path, langstr, text }) => {
1036-
(filename, line, logical_path, langstr, text)
1037-
}
1038-
ScrapedDoctest::Markdown(MdDoctest { filename, line, logical_path, langstr, text }) => {
1039-
(filename, line, logical_path, langstr, text)
1040-
}
1041-
};
1042-
1043-
let name = self.generate_name(&filename, line, &logical_path);
1036+
let name = self.generate_name(&test.filename, test.line, &test.logical_path);
10441037
let crate_name = self.crate_name.clone();
10451038
let opts = self.opts.clone();
1046-
let edition = langstr.edition.unwrap_or(self.rustdoc_options.edition);
1039+
let edition = test.langstr.edition.unwrap_or(self.rustdoc_options.edition);
10471040
let target_str = self.rustdoc_options.target.to_string();
10481041
let unused_externs = self.unused_extern_reports.clone();
1049-
let no_run = langstr.no_run || self.rustdoc_options.no_run;
1050-
if !langstr.compile_fail {
1042+
let no_run = test.langstr.no_run || self.rustdoc_options.no_run;
1043+
if !test.langstr.compile_fail {
10511044
self.compiling_test_count.fetch_add(1, Ordering::SeqCst);
10521045
}
10531046

1054-
let path = match &filename {
1047+
let path = match &test.filename {
10551048
FileName::Real(path) => {
10561049
if let Some(local_path) = path.local_path() {
10571050
local_path.to_path_buf()
@@ -1064,7 +1057,8 @@ impl CreateRunnableDoctests {
10641057
};
10651058

10661059
// For example `module/file.rs` would become `module_file_rs`
1067-
let file = filename
1060+
let file = test
1061+
.filename
10681062
.prefer_local()
10691063
.to_string_lossy()
10701064
.chars()
@@ -1073,22 +1067,25 @@ impl CreateRunnableDoctests {
10731067
let test_id = format!(
10741068
"{file}_{line}_{number}",
10751069
file = file,
1076-
line = line,
1070+
line = test.line,
10771071
number = {
10781072
// Increases the current test number, if this file already
10791073
// exists or it creates a new entry with a test number of 0.
1080-
self.visited_tests.entry((file.clone(), line)).and_modify(|v| *v += 1).or_insert(0)
1074+
self.visited_tests
1075+
.entry((file.clone(), test.line))
1076+
.and_modify(|v| *v += 1)
1077+
.or_insert(0)
10811078
},
10821079
);
10831080

10841081
let rustdoc_test_options =
10851082
IndividualTestOptions::new(&self.rustdoc_options, &self.arg_file, test_id);
10861083

1087-
debug!("creating test {name}: {text}");
1084+
debug!("creating test {name}: {}", test.text);
10881085
self.tests.push(test::TestDescAndFn {
10891086
desc: test::TestDesc {
10901087
name: test::DynTestName(name),
1091-
ignore: match langstr.ignore {
1088+
ignore: match test.langstr.ignore {
10921089
Ignore::All => true,
10931090
Ignore::None => false,
10941091
Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
@@ -1101,22 +1098,20 @@ impl CreateRunnableDoctests {
11011098
end_col: 0,
11021099
// compiler failures are test failures
11031100
should_panic: test::ShouldPanic::No,
1104-
compile_fail: langstr.compile_fail,
1101+
compile_fail: test.langstr.compile_fail,
11051102
no_run,
11061103
test_type: test::TestType::DocTest,
11071104
},
11081105
testfn: test::DynTestFn(Box::new(move || {
11091106
doctest_run_fn(
11101107
RunnableDoctest {
11111108
crate_name,
1112-
line,
11131109
rustdoc_test_options,
1114-
langstr,
11151110
no_run,
11161111
opts,
11171112
edition,
11181113
path,
1119-
text,
1114+
scraped_test: test,
11201115
},
11211116
unused_externs,
11221117
)
@@ -1128,45 +1123,31 @@ impl CreateRunnableDoctests {
11281123
/// A doctest that is ready to run.
11291124
struct RunnableDoctest {
11301125
crate_name: String,
1131-
line: usize,
11321126
rustdoc_test_options: IndividualTestOptions,
1133-
langstr: LangString,
11341127
no_run: bool,
11351128
opts: GlobalTestOptions,
11361129
edition: Edition,
11371130
path: PathBuf,
1138-
text: String,
1131+
scraped_test: ScrapedDoctest,
11391132
}
11401133

11411134
fn doctest_run_fn(
1142-
test: RunnableDoctest,
1135+
runnable_test: RunnableDoctest,
11431136
unused_externs: Arc<Mutex<Vec<UnusedExterns>>>,
11441137
) -> Result<(), String> {
1145-
let RunnableDoctest {
1146-
crate_name,
1147-
line,
1148-
rustdoc_test_options,
1149-
langstr,
1150-
no_run,
1151-
opts,
1152-
edition,
1153-
path,
1154-
text,
1155-
} = test;
1156-
11571138
let report_unused_externs = |uext| {
11581139
unused_externs.lock().unwrap().push(uext);
11591140
};
11601141
let res = run_test(
1161-
&text,
1162-
&crate_name,
1163-
line,
1164-
rustdoc_test_options,
1165-
langstr,
1166-
no_run,
1167-
&opts,
1168-
edition,
1169-
path,
1142+
&runnable_test.scraped_test.text,
1143+
&runnable_test.crate_name,
1144+
runnable_test.scraped_test.line,
1145+
runnable_test.rustdoc_test_options,
1146+
runnable_test.scraped_test.langstr,
1147+
runnable_test.no_run,
1148+
&runnable_test.opts,
1149+
runnable_test.edition,
1150+
runnable_test.path,
11701151
report_unused_externs,
11711152
);
11721153

src/librustdoc/doctest/markdown.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,16 @@ use super::{
1111
use crate::config::Options;
1212
use crate::html::markdown::{find_testable_code, ErrorCodes, LangString};
1313

14-
pub(super) struct MdDoctest {
15-
pub(super) filename: FileName,
16-
pub(super) line: usize,
17-
pub(super) logical_path: Vec<String>,
18-
pub(super) langstr: LangString,
19-
pub(super) text: String,
20-
}
21-
2214
struct MdCollector {
23-
tests: Vec<MdDoctest>,
15+
tests: Vec<ScrapedDoctest>,
2416
cur_path: Vec<String>,
2517
filename: FileName,
2618
}
2719

2820
impl DoctestVisitor for MdCollector {
2921
fn visit_test(&mut self, test: String, config: LangString, line: usize) {
3022
let filename = self.filename.clone();
31-
self.tests.push(MdDoctest {
23+
self.tests.push(ScrapedDoctest {
3224
filename,
3325
line,
3426
logical_path: self.cur_path.clone(),
@@ -128,7 +120,7 @@ pub(crate) fn test(options: Options) -> Result<(), String> {
128120
opts,
129121
file_path,
130122
);
131-
md_collector.tests.into_iter().for_each(|t| collector.add_test(ScrapedDoctest::Markdown(t)));
123+
md_collector.tests.into_iter().for_each(|t| collector.add_test(t));
132124
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
133125
Ok(())
134126
}

src/librustdoc/doctest/rust.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,13 @@ use rustc_session::Session;
1313
use rustc_span::source_map::SourceMap;
1414
use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP};
1515

16-
use super::DoctestVisitor;
16+
use super::{DoctestVisitor, ScrapedDoctest};
1717
use crate::clean::{types::AttributesExt, Attributes};
1818
use crate::html::markdown::{self, ErrorCodes, LangString};
1919

20-
pub(super) struct RustDoctest {
21-
pub(super) filename: FileName,
22-
pub(super) line: usize,
23-
pub(super) logical_path: Vec<String>,
24-
pub(super) langstr: LangString,
25-
pub(super) text: String,
26-
}
27-
2820
struct RustCollector {
2921
source_map: Lrc<SourceMap>,
30-
tests: Vec<RustDoctest>,
22+
tests: Vec<ScrapedDoctest>,
3123
cur_path: Vec<String>,
3224
position: Span,
3325
}
@@ -48,7 +40,7 @@ impl RustCollector {
4840

4941
impl DoctestVisitor for RustCollector {
5042
fn visit_test(&mut self, test: String, config: LangString, line: usize) {
51-
self.tests.push(RustDoctest {
43+
self.tests.push(ScrapedDoctest {
5244
filename: self.get_filename(),
5345
line,
5446
logical_path: self.cur_path.clone(),
@@ -92,7 +84,7 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
9284
Self { sess, map, codes, enable_per_target_ignores, tcx, collector }
9385
}
9486

95-
pub fn collect_crate(mut self) -> Vec<RustDoctest> {
87+
pub fn collect_crate(mut self) -> Vec<ScrapedDoctest> {
9688
let tcx = self.tcx;
9789
self.visit_testable("".to_string(), CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID), |this| {
9890
tcx.hir().walk_toplevel_module(this)

0 commit comments

Comments
 (0)