Skip to content

Commit 985cddf

Browse files
committed
Use enum for message kind in compiletest harness.
1 parent 2de6ddd commit 985cddf

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

src/compiletest/errors.rs

+44-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,54 @@
99
// except according to those terms.
1010
use self::WhichLine::*;
1111

12+
use std::fmt;
1213
use std::fs::File;
1314
use std::io::BufReader;
1415
use std::io::prelude::*;
1516
use std::path::Path;
17+
use std::str::FromStr;
18+
19+
#[derive(Clone, Debug, PartialEq)]
20+
pub enum ErrorKind {
21+
Help,
22+
Error,
23+
Note,
24+
Suggestion,
25+
Warning,
26+
}
27+
28+
impl FromStr for ErrorKind {
29+
type Err = ();
30+
fn from_str(s: &str) -> Result<Self, Self::Err> {
31+
match &s.trim_right_matches(':') as &str {
32+
"HELP" => Ok(ErrorKind::Help),
33+
"ERROR" => Ok(ErrorKind::Error),
34+
"NOTE" => Ok(ErrorKind::Note),
35+
"SUGGESTION" => Ok(ErrorKind::Suggestion),
36+
"WARN" => Ok(ErrorKind::Warning),
37+
"WARNING" => Ok(ErrorKind::Warning),
38+
_ => Err(()),
39+
}
40+
}
41+
}
42+
43+
impl fmt::Display for ErrorKind {
44+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45+
match *self {
46+
ErrorKind::Help => write!(f, "help"),
47+
ErrorKind::Error => write!(f, "error"),
48+
ErrorKind::Note => write!(f, "note"),
49+
ErrorKind::Suggestion => write!(f, "suggestion"),
50+
ErrorKind::Warning => write!(f, "warning"),
51+
}
52+
}
53+
}
1654

1755
pub struct ExpectedError {
1856
pub line_num: usize,
19-
pub kind: String,
57+
/// What kind of message we expect (e.g. warning, error, suggestion).
58+
/// `None` if not specified or unknown message kind.
59+
pub kind: Option<ErrorKind>,
2060
pub msg: String,
2161
}
2262

@@ -84,8 +124,9 @@ fn parse_expected(last_nonfollow_error: Option<usize>,
84124
let letters = line[kind_start..].chars();
85125
let kind = letters.skip_while(|c| c.is_whitespace())
86126
.take_while(|c| !c.is_whitespace())
87-
.flat_map(|c| c.to_lowercase())
88-
.collect::<String>();
127+
.collect::<String>()
128+
.parse::<ErrorKind>()
129+
.ok();
89130
let letters = line[kind_start..].chars();
90131
let msg = letters.skip_while(|c| c.is_whitespace())
91132
.skip_while(|c| !c.is_whitespace())

src/compiletest/runtest.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use common::Config;
1212
use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
1313
use common::{Codegen, DebugInfoLldb, DebugInfoGdb, Rustdoc, CodegenUnits};
14-
use errors;
14+
use errors::{self, ErrorKind};
1515
use header::TestProps;
1616
use header;
1717
use procsrv;
@@ -1013,8 +1013,8 @@ fn check_expected_errors(revision: Option<&str>,
10131013
expected_errors.iter()
10141014
.fold((false, false),
10151015
|(acc_help, acc_note), ee|
1016-
(acc_help || ee.kind == "help:" || ee.kind == "help",
1017-
acc_note || ee.kind == "note:" || ee.kind == "note"));
1016+
(acc_help || ee.kind == Some(ErrorKind::Help),
1017+
acc_note || ee.kind == Some(ErrorKind::Note)));
10181018

10191019
// Scan and extract our error/warning messages,
10201020
// which look like:
@@ -1032,15 +1032,15 @@ fn check_expected_errors(revision: Option<&str>,
10321032
let mut prev = 0;
10331033
for (i, ee) in expected_errors.iter().enumerate() {
10341034
if !found_flags[i] {
1035-
debug!("prefix={} ee.kind={} ee.msg={} line={}",
1035+
debug!("prefix={} ee.kind={:?} ee.msg={} line={}",
10361036
prefixes[i],
10371037
ee.kind,
10381038
ee.msg,
10391039
line);
10401040
// Suggestions have no line number in their output, so take on the line number of
10411041
// the previous expected error
1042-
if ee.kind == "suggestion" {
1043-
assert!(expected_errors[prev].kind == "help",
1042+
if ee.kind == Some(ErrorKind::Suggestion) {
1043+
assert!(expected_errors[prev].kind == Some(ErrorKind::Help),
10441044
"SUGGESTIONs must be preceded by a HELP");
10451045
if line.contains(&ee.msg) {
10461046
found_flags[i] = true;
@@ -1050,7 +1050,7 @@ fn check_expected_errors(revision: Option<&str>,
10501050
}
10511051
if
10521052
(prefix_matches(line, &prefixes[i]) || continuation(line)) &&
1053-
line.contains(&ee.kind) &&
1053+
(ee.kind.is_none() || line.contains(&ee.kind.as_ref().unwrap().to_string())) &&
10541054
line.contains(&ee.msg)
10551055
{
10561056
found_flags[i] = true;
@@ -1076,7 +1076,10 @@ fn check_expected_errors(revision: Option<&str>,
10761076
if !flag {
10771077
let ee = &expected_errors[i];
10781078
error(revision, &format!("expected {} on line {} not found: {}",
1079-
ee.kind, ee.line_num, ee.msg));
1079+
ee.kind.as_ref()
1080+
.map_or("message".into(),
1081+
|k| k.to_string()),
1082+
ee.line_num, ee.msg));
10801083
not_found += 1;
10811084
}
10821085
}

0 commit comments

Comments
 (0)