Skip to content

Commit 279b4d2

Browse files
camelidGuillaumeGomez
authored andcommitted
Merge RustDoctest and MdDoctest into one type
1 parent b7dd401 commit 279b4d2

File tree

3 files changed

+42
-77
lines changed

3 files changed

+42
-77
lines changed

src/librustdoc/doctest.rs

Lines changed: 35 additions & 54 deletions
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
});
@@ -976,9 +975,12 @@ impl IndividualTestOptions {
976975
}
977976

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

984986
pub(crate) trait DoctestVisitor {
@@ -1030,27 +1032,18 @@ impl CreateRunnableDoctests {
10301032
}
10311033

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

1053-
let path = match &filename {
1046+
let path = match &test.filename {
10541047
FileName::Real(path) => {
10551048
if let Some(local_path) = path.local_path() {
10561049
local_path.to_path_buf()
@@ -1063,7 +1056,8 @@ impl CreateRunnableDoctests {
10631056
};
10641057

10651058
// For example `module/file.rs` would become `module_file_rs`
1066-
let file = filename
1059+
let file = test
1060+
.filename
10671061
.prefer_local()
10681062
.to_string_lossy()
10691063
.chars()
@@ -1072,22 +1066,25 @@ impl CreateRunnableDoctests {
10721066
let test_id = format!(
10731067
"{file}_{line}_{number}",
10741068
file = file,
1075-
line = line,
1069+
line = test.line,
10761070
number = {
10771071
// Increases the current test number, if this file already
10781072
// exists or it creates a new entry with a test number of 0.
1079-
self.visited_tests.entry((file.clone(), line)).and_modify(|v| *v += 1).or_insert(0)
1073+
self.visited_tests
1074+
.entry((file.clone(), test.line))
1075+
.and_modify(|v| *v += 1)
1076+
.or_insert(0)
10801077
},
10811078
);
10821079

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

1086-
debug!("creating test {name}: {text}");
1083+
debug!("creating test {name}: {}", test.text);
10871084
self.tests.push(test::TestDescAndFn {
10881085
desc: test::TestDesc {
10891086
name: test::DynTestName(name),
1090-
ignore: match langstr.ignore {
1087+
ignore: match test.langstr.ignore {
10911088
Ignore::All => true,
10921089
Ignore::None => false,
10931090
Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
@@ -1100,22 +1097,20 @@ impl CreateRunnableDoctests {
11001097
end_col: 0,
11011098
// compiler failures are test failures
11021099
should_panic: test::ShouldPanic::No,
1103-
compile_fail: langstr.compile_fail,
1100+
compile_fail: test.langstr.compile_fail,
11041101
no_run,
11051102
test_type: test::TestType::DocTest,
11061103
},
11071104
testfn: test::DynTestFn(Box::new(move || {
11081105
doctest_run_fn(
11091106
RunnableDoctest {
11101107
crate_name,
1111-
line,
11121108
rustdoc_test_options,
1113-
langstr,
11141109
no_run,
11151110
opts,
11161111
edition,
11171112
path,
1118-
text,
1113+
scraped_test: test,
11191114
},
11201115
unused_externs,
11211116
)
@@ -1127,45 +1122,31 @@ impl CreateRunnableDoctests {
11271122
/// A doctest that is ready to run.
11281123
struct RunnableDoctest {
11291124
crate_name: String,
1130-
line: usize,
11311125
rustdoc_test_options: IndividualTestOptions,
1132-
langstr: LangString,
11331126
no_run: bool,
11341127
opts: GlobalTestOptions,
11351128
edition: Edition,
11361129
path: PathBuf,
1137-
text: String,
1130+
scraped_test: ScrapedDoctest,
11381131
}
11391132

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

src/librustdoc/doctest/markdown.rs

Lines changed: 3 additions & 11 deletions
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(),
@@ -127,7 +119,7 @@ pub(crate) fn test(options: Options) -> Result<(), String> {
127119
opts,
128120
file_path,
129121
);
130-
md_collector.tests.into_iter().for_each(|t| collector.add_test(ScrapedDoctest::Markdown(t)));
122+
md_collector.tests.into_iter().for_each(|t| collector.add_test(t));
131123
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
132124
Ok(())
133125
}

src/librustdoc/doctest/rust.rs

Lines changed: 4 additions & 12 deletions
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)