Skip to content

Commit f9bd6b4

Browse files
committed
auto merge of #14283 : Ryman/rust/commented_compile_fail_error, r=kballard
Edit: This now only covers refactoring to regex.
2 parents 84320a4 + 66d8c3c commit f9bd6b4

File tree

4 files changed

+31
-59
lines changed

4 files changed

+31
-59
lines changed

src/compiletest/common.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -91,6 +91,9 @@ pub struct Config {
9191
// Only run tests that match this filter
9292
pub filter: Option<Regex>,
9393

94+
// Precompiled regex for finding expected errors in cfail
95+
pub cfail_regex: Regex,
96+
9497
// Write out a parseable log of tests that were run
9598
pub logfile: Option<Path>,
9699

@@ -144,5 +147,4 @@ pub struct Config {
144147

145148
// Explain what's going on
146149
pub verbose: bool
147-
148150
}

src/compiletest/compiletest.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -33,6 +33,7 @@ use getopts::{optopt, optflag, reqopt};
3333
use common::Config;
3434
use common::{Pretty, DebugInfoGdb, Codegen};
3535
use util::logv;
36+
use regex::Regex;
3637

3738
pub mod procsrv;
3839
pub mod util;
@@ -147,6 +148,7 @@ pub fn parse_config(args: Vec<StrBuf> ) -> Config {
147148
.as_slice()).expect("invalid mode"),
148149
run_ignored: matches.opt_present("ignored"),
149150
filter: filter,
151+
cfail_regex: Regex::new(errors::EXPECTED_PATTERN).unwrap(),
150152
logfile: matches.opt_str("logfile").map(|s| Path::new(s)),
151153
save_metrics: matches.opt_str("save-metrics").map(|s| Path::new(s)),
152154
ratchet_metrics:

src/compiletest/errors.rs

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -9,68 +9,36 @@
99
// except according to those terms.
1010

1111
use std::io::{BufferedReader, File};
12+
use regex::Regex;
1213

1314
pub struct ExpectedError {
1415
pub line: uint,
1516
pub kind: StrBuf,
1617
pub msg: StrBuf,
1718
}
1819

19-
// Load any test directives embedded in the file
20-
pub fn load_errors(testfile: &Path) -> Vec<ExpectedError> {
20+
pub static EXPECTED_PATTERN : &'static str = r"//~(?P<adjusts>\^*)\s*(?P<kind>\S*)\s*(?P<msg>.*)";
2121

22-
let mut error_patterns = Vec::new();
22+
// Load any test directives embedded in the file
23+
pub fn load_errors(re: &Regex, testfile: &Path) -> Vec<ExpectedError> {
2324
let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
24-
let mut line_num = 1u;
25-
for ln in rdr.lines() {
26-
error_patterns.push_all_move(parse_expected(line_num,
27-
ln.unwrap().to_strbuf()));
28-
line_num += 1u;
29-
}
30-
return error_patterns;
31-
}
32-
33-
fn parse_expected(line_num: uint, line: StrBuf) -> Vec<ExpectedError> {
34-
let line = line.as_slice().trim().to_strbuf();
35-
let error_tag = "//~".to_strbuf();
36-
let mut idx;
37-
match line.as_slice().find_str(error_tag.as_slice()) {
38-
None => return Vec::new(),
39-
Some(nn) => { idx = (nn as uint) + error_tag.len(); }
40-
}
41-
42-
// "//~^^^ kind msg" denotes a message expected
43-
// three lines above current line:
44-
let mut adjust_line = 0u;
45-
let len = line.len();
46-
while idx < len && line.as_slice()[idx] == ('^' as u8) {
47-
adjust_line += 1u;
48-
idx += 1u;
49-
}
5025

51-
// Extract kind:
52-
while idx < len && line.as_slice()[idx] == (' ' as u8) {
53-
idx += 1u;
54-
}
55-
let start_kind = idx;
56-
while idx < len && line.as_slice()[idx] != (' ' as u8) {
57-
idx += 1u;
58-
}
59-
60-
let kind = line.as_slice().slice(start_kind, idx);
61-
let kind = kind.to_ascii().to_lower().into_str().to_strbuf();
62-
63-
// Extract msg:
64-
while idx < len && line.as_slice()[idx] == (' ' as u8) {
65-
idx += 1u;
66-
}
67-
let msg = line.as_slice().slice(idx, len).to_strbuf();
68-
69-
debug!("line={} kind={} msg={}", line_num - adjust_line, kind, msg);
26+
rdr.lines().enumerate().filter_map(|(line_no, ln)| {
27+
parse_expected(line_no + 1, ln.unwrap(), re)
28+
}).collect()
29+
}
7030

71-
return vec!(ExpectedError{
72-
line: line_num - adjust_line,
73-
kind: kind,
74-
msg: msg,
75-
});
31+
fn parse_expected(line_num: uint, line: &str, re: &Regex) -> Option<ExpectedError> {
32+
re.captures(line).and_then(|caps| {
33+
let adjusts = caps.name("adjusts").len();
34+
let kind = caps.name("kind").to_ascii().to_lower().into_str().to_strbuf();
35+
let msg = caps.name("msg").trim().to_strbuf();
36+
37+
debug!("line={} kind={} msg={}", line_num, kind, msg);
38+
Some(ExpectedError {
39+
line: line_num - adjusts,
40+
kind: kind,
41+
msg: msg,
42+
})
43+
})
7644
}

src/compiletest/runtest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -79,7 +79,7 @@ fn run_cfail_test(config: &Config, props: &TestProps, testfile: &Path) {
7979

8080
check_correct_failure_status(&proc_res);
8181

82-
let expected_errors = errors::load_errors(testfile);
82+
let expected_errors = errors::load_errors(&config.cfail_regex, testfile);
8383
if !expected_errors.is_empty() {
8484
if !props.error_patterns.is_empty() {
8585
fatal("both error pattern and expected errors \

0 commit comments

Comments
 (0)