提交 4ba60aba 编写于 作者: B bors 提交者: GitHub

Auto merge of #34186 - GuillaumeGomez:err-code-check, r=alexcrichton

Implementation of #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 #34168.
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
A private trait was used on a public type parameter bound. Erroneous code A private trait was used on a public type parameter bound. Erroneous code
examples: examples:
```compile_fail ```compile_fail,E0445
#![deny(private_in_public)] #![deny(private_in_public)]
trait Foo { trait Foo {
...@@ -46,7 +46,7 @@ pub fn foo<T: Foo> (t: T) {} // ok! ...@@ -46,7 +46,7 @@ pub fn foo<T: Foo> (t: T) {} // ok!
E0446: r##" E0446: r##"
A private type was used in a public type signature. Erroneous code example: A private type was used in a public type signature. Erroneous code example:
```compile_fail ```compile_fail,E0446
#![deny(private_in_public)] #![deny(private_in_public)]
mod Foo { mod Foo {
...@@ -100,7 +100,7 @@ pub enum Foo { ...@@ -100,7 +100,7 @@ pub enum Foo {
Since the enum is already public, adding `pub` on one its elements is Since the enum is already public, adding `pub` on one its elements is
unnecessary. Example: unnecessary. Example:
```compile_fail ```compile_fail,
enum Foo { enum Foo {
pub Bar, // not ok! pub Bar, // not ok!
} }
...@@ -119,7 +119,7 @@ pub enum Foo { ...@@ -119,7 +119,7 @@ pub enum Foo {
A tuple constructor was invoked while some of its fields are private. Erroneous A tuple constructor was invoked while some of its fields are private. Erroneous
code example: code example:
```compile_fail ```compile_fail,E0450
mod Bar { mod Bar {
pub struct Foo(isize); pub struct Foo(isize);
} }
...@@ -157,7 +157,7 @@ pub fn new(x: isize) -> Foo { ...@@ -157,7 +157,7 @@ pub fn new(x: isize) -> Foo {
E0451: r##" E0451: r##"
A struct constructor with private fields was invoked. Erroneous code example: A struct constructor with private fields was invoked. Erroneous code example:
```compile_fail ```compile_fail,E0451
mod Bar { mod Bar {
pub struct Foo { pub struct Foo {
pub a: isize, pub a: isize,
......
...@@ -408,7 +408,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) { ...@@ -408,7 +408,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
tests.add_test(text.to_owned(), tests.add_test(text.to_owned(),
block_info.should_panic, block_info.no_run, block_info.should_panic, block_info.no_run,
block_info.ignore, block_info.test_harness, block_info.ignore, block_info.test_harness,
block_info.compile_fail); block_info.compile_fail, block_info.error_codes);
} }
} }
...@@ -454,6 +454,7 @@ struct LangString { ...@@ -454,6 +454,7 @@ struct LangString {
rust: bool, rust: bool,
test_harness: bool, test_harness: bool,
compile_fail: bool, compile_fail: bool,
error_codes: Vec<String>,
} }
impl LangString { impl LangString {
...@@ -465,6 +466,7 @@ fn all_false() -> LangString { ...@@ -465,6 +466,7 @@ fn all_false() -> LangString {
rust: true, // NB This used to be `notrust = false` rust: true, // NB This used to be `notrust = false`
test_harness: false, test_harness: false,
compile_fail: false, compile_fail: false,
error_codes: Vec::new(),
} }
} }
...@@ -472,9 +474,14 @@ fn parse(string: &str) -> LangString { ...@@ -472,9 +474,14 @@ fn parse(string: &str) -> LangString {
let mut seen_rust_tags = false; let mut seen_rust_tags = false;
let mut seen_other_tags = false; let mut seen_other_tags = false;
let mut data = LangString::all_false(); let mut data = LangString::all_false();
let allow_compile_fail = match get_unstable_features_setting() { let mut allow_compile_fail = false;
UnstableFeatures::Allow | UnstableFeatures::Cheat=> true, let mut allow_error_code_check = false;
_ => false, match get_unstable_features_setting() {
UnstableFeatures::Allow | UnstableFeatures::Cheat => {
allow_compile_fail = true;
allow_error_code_check = true;
}
_ => {},
}; };
let tokens = string.split(|c: char| let tokens = string.split(|c: char|
...@@ -493,7 +500,15 @@ fn parse(string: &str) -> LangString { ...@@ -493,7 +500,15 @@ fn parse(string: &str) -> LangString {
data.compile_fail = true; data.compile_fail = true;
seen_rust_tags = true; seen_rust_tags = true;
data.no_run = true; data.no_run = true;
}, }
x if allow_error_code_check && x.starts_with("E") && x.len() == 5 => {
if let Ok(_) = x[1..].parse::<u32>() {
data.error_codes.push(x.to_owned());
seen_rust_tags = true;
} else {
seen_other_tags = true;
}
}
_ => { seen_other_tags = true } _ => { seen_other_tags = true }
} }
} }
...@@ -577,7 +592,7 @@ mod tests { ...@@ -577,7 +592,7 @@ mod tests {
fn test_lang_string_parse() { fn test_lang_string_parse() {
fn t(s: &str, fn t(s: &str,
should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool, should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool,
compile_fail: bool) { compile_fail: bool, error_codes: Vec<String>) {
assert_eq!(LangString::parse(s), LangString { assert_eq!(LangString::parse(s), LangString {
should_panic: should_panic, should_panic: should_panic,
no_run: no_run, no_run: no_run,
...@@ -585,22 +600,26 @@ fn t(s: &str, ...@@ -585,22 +600,26 @@ fn t(s: &str,
rust: rust, rust: rust,
test_harness: test_harness, test_harness: test_harness,
compile_fail: compile_fail, compile_fail: compile_fail,
error_codes: error_codes,
}) })
} }
// marker | should_panic| no_run| ignore| rust | test_harness| compile_fail // marker | should_panic| no_run| ignore| rust | test_harness| compile_fail
t("", false, false, false, true, false, false); // | error_codes
t("rust", false, false, false, true, false, false); t("", false, false, false, true, false, false, Vec::new());
t("sh", false, false, false, false, false, false); t("rust", false, false, false, true, false, false, Vec::new());
t("ignore", false, false, true, true, false, false); t("sh", false, false, false, false, false, false, Vec::new());
t("should_panic", true, false, false, true, false, false); t("ignore", false, false, true, true, false, false, Vec::new());
t("no_run", false, true, false, true, false, false); t("should_panic", true, false, false, true, false, false, Vec::new());
t("test_harness", false, false, false, true, true, false); t("no_run", false, true, false, true, false, false, Vec::new());
t("compile_fail", false, true, false, true, false, true); t("test_harness", false, false, false, true, true, false, Vec::new());
t("{.no_run .example}", false, true, false, true, false, false); t("compile_fail", false, true, false, true, false, true, Vec::new());
t("{.sh .should_panic}", true, false, false, true, false, false); t("E0450", false, false, false, true, false, false,
t("{.example .rust}", false, false, false, true, false, false); vec!("E0450".to_owned()));
t("{.test_harness .rust}", false, false, false, true, true, false); t("{.no_run .example}", false, true, false, true, false, false, Vec::new());
t("{.sh .should_panic}", true, false, false, true, false, false, Vec::new());
t("{.example .rust}", false, false, false, true, false, false, Vec::new());
t("{.test_harness .rust}", false, false, false, true, true, false, Vec::new());
} }
#[test] #[test]
......
...@@ -176,7 +176,7 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions { ...@@ -176,7 +176,7 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths, fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
externs: core::Externs, externs: core::Externs,
should_panic: bool, no_run: bool, as_test_harness: bool, should_panic: bool, no_run: bool, as_test_harness: bool,
compile_fail: bool, opts: &TestOptions) { compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions) {
// the test harness wants its own `main` & top level functions, so // the test harness wants its own `main` & top level functions, so
// never wrap the test in `fn main() { ... }` // never wrap the test in `fn main() { ... }`
let test = maketest(test, Some(cratename), as_test_harness, opts); let test = maketest(test, Some(cratename), as_test_harness, opts);
...@@ -232,7 +232,7 @@ fn drop(&mut self) { ...@@ -232,7 +232,7 @@ fn drop(&mut self) {
None, None,
codemap.clone()); codemap.clone());
let old = io::set_panic(box Sink(data.clone())); let old = io::set_panic(box Sink(data.clone()));
let _bomb = Bomb(data, old.unwrap_or(box io::stdout())); let _bomb = Bomb(data.clone(), old.unwrap_or(box io::stdout()));
// Compile the code // Compile the code
let diagnostic_handler = errors::Handler::with_emitter(true, false, box emitter); let diagnostic_handler = errors::Handler::with_emitter(true, false, box emitter);
...@@ -273,13 +273,28 @@ fn drop(&mut self) { ...@@ -273,13 +273,28 @@ fn drop(&mut self) {
} else if count == 0 && compile_fail == true { } else if count == 0 && compile_fail == true {
panic!("test compiled while it wasn't supposed to") panic!("test compiled while it wasn't supposed to")
} }
if count > 0 && error_codes.len() > 0 {
let out = String::from_utf8(data.lock().unwrap().to_vec()).unwrap();
error_codes.retain(|err| !out.contains(err));
}
} }
Ok(()) if compile_fail => panic!("test compiled while it wasn't supposed to"), Ok(()) if compile_fail => panic!("test compiled while it wasn't supposed to"),
_ => {} _ => {}
} }
} }
Err(_) if compile_fail == false => panic!("couldn't compile the test"), Err(_) => {
_ => {} if compile_fail == false {
panic!("couldn't compile the test");
}
if error_codes.len() > 0 {
let out = String::from_utf8(data.lock().unwrap().to_vec()).unwrap();
error_codes.retain(|err| !out.contains(err));
}
}
}
if error_codes.len() > 0 {
panic!("Some expected error codes were not found: {:?}", error_codes);
} }
if no_run { return } if no_run { return }
...@@ -411,7 +426,7 @@ pub fn new(cratename: String, cfgs: Vec<String>, libs: SearchPaths, externs: cor ...@@ -411,7 +426,7 @@ pub fn new(cratename: String, cfgs: Vec<String>, libs: SearchPaths, externs: cor
pub fn add_test(&mut self, test: String, pub fn add_test(&mut self, test: String,
should_panic: bool, no_run: bool, should_ignore: bool, should_panic: bool, no_run: bool, should_ignore: bool,
as_test_harness: bool, compile_fail: bool) { as_test_harness: bool, compile_fail: bool, error_codes: Vec<String>) {
let name = if self.use_headers { let name = if self.use_headers {
let s = self.current_header.as_ref().map(|s| &**s).unwrap_or(""); let s = self.current_header.as_ref().map(|s| &**s).unwrap_or("");
format!("{}_{}", s, self.cnt) format!("{}_{}", s, self.cnt)
...@@ -442,6 +457,7 @@ pub fn add_test(&mut self, test: String, ...@@ -442,6 +457,7 @@ pub fn add_test(&mut self, test: String,
no_run, no_run,
as_test_harness, as_test_harness,
compile_fail, compile_fail,
error_codes,
&opts); &opts);
}) })
}); });
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册