Skip to content

Commit 7746d7c

Browse files
Add error codes block code flag
1 parent 0740a93 commit 7746d7c

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

src/librustdoc/html/markdown.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
408408
tests.add_test(text.to_owned(),
409409
block_info.should_panic, block_info.no_run,
410410
block_info.ignore, block_info.test_harness,
411-
block_info.compile_fail);
411+
block_info.compile_fail, block_info.error_codes);
412412
}
413413
}
414414

@@ -454,6 +454,7 @@ struct LangString {
454454
rust: bool,
455455
test_harness: bool,
456456
compile_fail: bool,
457+
error_codes: Vec<String>,
457458
}
458459

459460
impl LangString {
@@ -465,16 +466,22 @@ impl LangString {
465466
rust: true, // NB This used to be `notrust = false`
466467
test_harness: false,
467468
compile_fail: false,
469+
error_codes: Vec::new(),
468470
}
469471
}
470472

471473
fn parse(string: &str) -> LangString {
472474
let mut seen_rust_tags = false;
473475
let mut seen_other_tags = false;
474476
let mut data = LangString::all_false();
475-
let allow_compile_fail = match get_unstable_features_setting() {
476-
UnstableFeatures::Allow | UnstableFeatures::Cheat=> true,
477-
_ => false,
477+
let mut allow_compile_fail = false;
478+
let mut allow_error_code_check = false;
479+
match get_unstable_features_setting() {
480+
UnstableFeatures::Allow | UnstableFeatures::Cheat => {
481+
allow_compile_fail = true;
482+
allow_error_code_check = true;
483+
}
484+
_ => {},
478485
};
479486

480487
let tokens = string.split(|c: char|
@@ -493,7 +500,15 @@ impl LangString {
493500
data.compile_fail = true;
494501
seen_rust_tags = true;
495502
data.no_run = true;
496-
},
503+
}
504+
x if allow_error_code_check && x.starts_with("E") && x.len() == 5 => {
505+
if let Ok(_) = x[1..].parse::<u32>() {
506+
data.error_codes.push(x.to_owned());
507+
seen_rust_tags = true;
508+
} else {
509+
seen_other_tags = true;
510+
}
511+
}
497512
_ => { seen_other_tags = true }
498513
}
499514
}
@@ -577,14 +592,15 @@ mod tests {
577592
fn test_lang_string_parse() {
578593
fn t(s: &str,
579594
should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool,
580-
compile_fail: bool) {
595+
compile_fail: bool, error_codes: Vec<String>) {
581596
assert_eq!(LangString::parse(s), LangString {
582597
should_panic: should_panic,
583598
no_run: no_run,
584599
ignore: ignore,
585600
rust: rust,
586601
test_harness: test_harness,
587602
compile_fail: compile_fail,
603+
error_codes: error_codes,
588604
})
589605
}
590606

src/librustdoc/test.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
176176
fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
177177
externs: core::Externs,
178178
should_panic: bool, no_run: bool, as_test_harness: bool,
179-
compile_fail: bool, opts: &TestOptions) {
179+
compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions) {
180180
// the test harness wants its own `main` & top level functions, so
181181
// never wrap the test in `fn main() { ... }`
182182
let test = maketest(test, Some(cratename), as_test_harness, opts);
@@ -232,7 +232,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
232232
None,
233233
codemap.clone());
234234
let old = io::set_panic(box Sink(data.clone()));
235-
let _bomb = Bomb(data, old.unwrap_or(box io::stdout()));
235+
let _bomb = Bomb(data.clone(), old.unwrap_or(box io::stdout()));
236236

237237
// Compile the code
238238
let diagnostic_handler = errors::Handler::with_emitter(true, false, box emitter);
@@ -273,13 +273,28 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
273273
} else if count == 0 && compile_fail == true {
274274
panic!("test compiled while it wasn't supposed to")
275275
}
276+
if count > 0 && error_codes.len() > 0 {
277+
let out = String::from_utf8(data.lock().unwrap().to_vec()).unwrap();
278+
error_codes = error_codes.into_iter().filter(|err| !out.contains(err)).collect();
279+
}
276280
}
277281
Ok(()) if compile_fail => panic!("test compiled while it wasn't supposed to"),
278282
_ => {}
279283
}
280284
}
281-
Err(_) if compile_fail == false => panic!("couldn't compile the test"),
282-
_ => {}
285+
Err(_) => {
286+
if compile_fail == false {
287+
panic!("couldn't compile the test");
288+
}
289+
if error_codes.len() > 0 {
290+
let out = String::from_utf8(data.lock().unwrap().to_vec()).unwrap();
291+
error_codes.retain(|err| !out.contains(err));
292+
}
293+
}
294+
}
295+
296+
if error_codes.len() > 0 {
297+
panic!("Some expected error codes were not found: {:?}", error_codes);
283298
}
284299

285300
if no_run { return }
@@ -411,7 +426,7 @@ impl Collector {
411426

412427
pub fn add_test(&mut self, test: String,
413428
should_panic: bool, no_run: bool, should_ignore: bool,
414-
as_test_harness: bool, compile_fail: bool) {
429+
as_test_harness: bool, compile_fail: bool, error_codes: Vec<String>) {
415430
let name = if self.use_headers {
416431
let s = self.current_header.as_ref().map(|s| &**s).unwrap_or("");
417432
format!("{}_{}", s, self.cnt)
@@ -442,6 +457,7 @@ impl Collector {
442457
no_run,
443458
as_test_harness,
444459
compile_fail,
460+
error_codes,
445461
&opts);
446462
})
447463
});

0 commit comments

Comments
 (0)