From 23bbe2bce72c4674ef46507a2db1e4e5e55ff3ff Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 25 May 2022 16:08:41 +0000 Subject: [PATCH] Reproduce #2156 --- ui_test/.gitignore | 1 + ui_test/src/comments.rs | 11 +++++-- ui_test/src/lib.rs | 10 +++--- ui_test/tests/check_annotations.rs | 49 ++++++++++++++++++++++++++++++ ui_test/tests/comment_parser.rs | 22 ++++++++++++++ 5 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 ui_test/.gitignore create mode 100644 ui_test/tests/check_annotations.rs create mode 100644 ui_test/tests/comment_parser.rs diff --git a/ui_test/.gitignore b/ui_test/.gitignore new file mode 100644 index 000000000000..03314f77b5aa --- /dev/null +++ b/ui_test/.gitignore @@ -0,0 +1 @@ +Cargo.lock diff --git a/ui_test/src/comments.rs b/ui_test/src/comments.rs index 14566d2feccf..193cda68b9aa 100644 --- a/ui_test/src/comments.rs +++ b/ui_test/src/comments.rs @@ -5,7 +5,7 @@ use regex::Regex; /// This crate supports various magic comments that get parsed as file-specific /// configuration values. This struct parses them all in one go and then they /// get processed by their respective use sites. -#[derive(Default)] +#[derive(Default, Debug)] pub struct Comments { /// List of revision names to execute. Can only be speicified once pub revisions: Option>, @@ -26,6 +26,7 @@ pub struct Comments { pub error_matches: Vec, } +#[derive(Debug)] pub struct ErrorMatch { pub matched: String, pub revision: Option, @@ -33,9 +34,13 @@ pub struct ErrorMatch { } impl Comments { - pub fn parse(path: &Path) -> Self { - let mut this = Self::default(); + pub fn parse_file(path: &Path) -> Self { let content = std::fs::read_to_string(path).unwrap(); + Self::parse(path, &content) + } + + pub fn parse(path: &Path, content: &str) -> Self { + let mut this = Self::default(); let error_pattern_regex = Regex::new(r"//(\[(?P[^\]]+)\])?~[|^]*\s*(ERROR|HELP|WARN)?:?(?P.*)") .unwrap(); diff --git a/ui_test/src/lib.rs b/ui_test/src/lib.rs index b779d6844a97..4f7e55fdce8d 100644 --- a/ui_test/src/lib.rs +++ b/ui_test/src/lib.rs @@ -9,7 +9,7 @@ use comments::ErrorMatch; use crossbeam::queue::SegQueue; use regex::Regex; -use crate::comments::Comments; +pub use crate::comments::Comments; mod comments; @@ -73,7 +73,7 @@ pub fn run_tests(config: Config) { if !path.extension().map(|ext| ext == "rs").unwrap_or(false) { continue; } - let comments = Comments::parse(&path); + let comments = Comments::parse_file(&path); // Ignore file if only/ignore rules do (not) apply if ignore_file(&comments, &target) { ignored.fetch_add(1, Ordering::Relaxed); @@ -171,7 +171,7 @@ pub fn run_tests(config: Config) { } #[derive(Debug)] -enum Error { +pub enum Error { /// Got an invalid exit status for the given mode. ExitStatus(Mode, ExitStatus), PatternNotFound { @@ -191,7 +191,7 @@ enum Error { }, } -type Errors = Vec; +pub type Errors = Vec; fn run_test( path: &Path, @@ -249,7 +249,7 @@ fn run_test( (miri, errors) } -fn check_annotations( +pub fn check_annotations( unnormalized_stderr: &[u8], errors: &mut Errors, config: &Config, diff --git a/ui_test/tests/check_annotations.rs b/ui_test/tests/check_annotations.rs new file mode 100644 index 000000000000..4735fe1fa049 --- /dev/null +++ b/ui_test/tests/check_annotations.rs @@ -0,0 +1,49 @@ +use std::path::{Path, PathBuf}; + +use ui_test::{check_annotations, Comments, Config, Error, Mode, OutputConflictHandling}; + +fn config() -> Config { + Config { + args: vec![], + target: None, + stderr_filters: vec![], + stdout_filters: vec![], + root_dir: PathBuf::from("."), + mode: Mode::Fail, + program: PathBuf::from("cake"), + output_conflict_handling: OutputConflictHandling::Error, + } +} + +#[test] +fn issue_2156() { + let s = r" +use std::mem; + +fn main() { + let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR encountered a dangling reference (address $HEX is unallocated) +} + "; + let comments = Comments::parse(Path::new(""), s); + let mut errors = vec![]; + let config = config(); + let unnormalized_stderr = r" +error: Undefined Behavior: type validation failed: encountered a dangling reference (address 0x10 is unallocated) + --> tests/compile-fail/validity/dangling_ref1.rs:6:29 + | +LL | let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR encountered a dangling reference (address $HEX is unallocated) + | ^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (address 0x10 is unallocated) + | + = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior + = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information + + = note: inside `main` at tests/compile-fail/validity/dangling_ref1.rs:6:29 +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace +error: aborting due to previous error + "; + check_annotations(unnormalized_stderr.as_bytes(), &mut errors, &config, "", &comments); + match &errors[..] { + [Error::PatternNotFound { .. }] => {} + _ => panic!("{:#?}", errors), + } +} diff --git a/ui_test/tests/comment_parser.rs b/ui_test/tests/comment_parser.rs new file mode 100644 index 000000000000..ee1382a6c788 --- /dev/null +++ b/ui_test/tests/comment_parser.rs @@ -0,0 +1,22 @@ +use std::path::Path; + +use ui_test::Comments; + +#[test] +fn issue_2156() { + let s = r" +use std::mem; + +fn main() { + let _x: &i32 = unsafe { mem::transmute(16usize) }; //~ ERROR encountered a dangling reference (address $HEX is unallocated) +} + "; + let comments = Comments::parse(Path::new(""), s); + println!("{:#?}", comments); + assert_eq!(comments.error_matches[0].definition_line, 4); + assert_eq!(comments.error_matches[0].revision, None); + assert_eq!( + comments.error_matches[0].matched, + "encountered a dangling reference (address $HEX is unallocated)" + ); +}