Skip to content

Commit 4ba60ab

Browse files
authored
Auto merge of rust-lang#34186 - GuillaumeGomez:err-code-check, r=alexcrichton
Implementation of rust-lang#34168 r? @brson cc @alexcrichton cc @steveklabnik cc @jonathandturner I only updated `librustc_privacy/diagnostics.rs`, and I already found a case where the code doesn't throw the expected error code (E0448). Fixes rust-lang#34168.
2 parents 5522e67 + ddfaf10 commit 4ba60ab

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed

src/librustc_privacy/diagnostics.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ E0445: r##"
1616
A private trait was used on a public type parameter bound. Erroneous code
1717
examples:
1818
19-
```compile_fail
19+
```compile_fail,E0445
2020
#![deny(private_in_public)]
2121
2222
trait Foo {
@@ -46,7 +46,7 @@ pub fn foo<T: Foo> (t: T) {} // ok!
4646
E0446: r##"
4747
A private type was used in a public type signature. Erroneous code example:
4848
49-
```compile_fail
49+
```compile_fail,E0446
5050
#![deny(private_in_public)]
5151
5252
mod Foo {
@@ -100,7 +100,7 @@ pub enum Foo {
100100
Since the enum is already public, adding `pub` on one its elements is
101101
unnecessary. Example:
102102
103-
```compile_fail
103+
```compile_fail,
104104
enum Foo {
105105
pub Bar, // not ok!
106106
}
@@ -119,7 +119,7 @@ E0450: r##"
119119
A tuple constructor was invoked while some of its fields are private. Erroneous
120120
code example:
121121
122-
```compile_fail
122+
```compile_fail,E0450
123123
mod Bar {
124124
pub struct Foo(isize);
125125
}
@@ -157,7 +157,7 @@ let f = bar::Foo::new(1);
157157
E0451: r##"
158158
A struct constructor with private fields was invoked. Erroneous code example:
159159
160-
```compile_fail
160+
```compile_fail,E0451
161161
mod Bar {
162162
pub struct Foo {
163163
pub a: isize,

src/librustdoc/html/markdown.rs

+37-18
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,30 +592,34 @@ 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

591607
// marker | should_panic| no_run| ignore| rust | test_harness| compile_fail
592-
t("", false, false, false, true, false, false);
593-
t("rust", false, false, false, true, false, false);
594-
t("sh", false, false, false, false, false, false);
595-
t("ignore", false, false, true, true, false, false);
596-
t("should_panic", true, false, false, true, false, false);
597-
t("no_run", false, true, false, true, false, false);
598-
t("test_harness", false, false, false, true, true, false);
599-
t("compile_fail", false, true, false, true, false, true);
600-
t("{.no_run .example}", false, true, false, true, false, false);
601-
t("{.sh .should_panic}", true, false, false, true, false, false);
602-
t("{.example .rust}", false, false, false, true, false, false);
603-
t("{.test_harness .rust}", false, false, false, true, true, false);
608+
// | error_codes
609+
t("", false, false, false, true, false, false, Vec::new());
610+
t("rust", false, false, false, true, false, false, Vec::new());
611+
t("sh", false, false, false, false, false, false, Vec::new());
612+
t("ignore", false, false, true, true, false, false, Vec::new());
613+
t("should_panic", true, false, false, true, false, false, Vec::new());
614+
t("no_run", false, true, false, true, false, false, Vec::new());
615+
t("test_harness", false, false, false, true, true, false, Vec::new());
616+
t("compile_fail", false, true, false, true, false, true, Vec::new());
617+
t("E0450", false, false, false, true, false, false,
618+
vec!("E0450".to_owned()));
619+
t("{.no_run .example}", false, true, false, true, false, false, Vec::new());
620+
t("{.sh .should_panic}", true, false, false, true, false, false, Vec::new());
621+
t("{.example .rust}", false, false, false, true, false, false, Vec::new());
622+
t("{.test_harness .rust}", false, false, false, true, true, false, Vec::new());
604623
}
605624

606625
#[test]

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.retain(|err| !out.contains(err));
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)