Skip to content

Commit c7bdfd4

Browse files
committed
Auto merge of #32341 - frewsxcv:compiletest-enum, r=nikomatsakis
Use enum for message kind in compiletest harness. None
2 parents 2ae05d3 + 73f4321 commit c7bdfd4

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

src/compiletest/errors.rs

+46-6
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

@@ -81,11 +121,11 @@ fn parse_expected(last_nonfollow_error: Option<usize>,
81121
(false, line[start + tag.len()..].chars().take_while(|c| *c == '^').count())
82122
};
83123
let kind_start = start + tag.len() + adjusts + (follow as usize);
84-
let letters = line[kind_start..].chars();
85-
let kind = letters.skip_while(|c| c.is_whitespace())
86-
.take_while(|c| !c.is_whitespace())
87-
.flat_map(|c| c.to_lowercase())
88-
.collect::<String>();
124+
let kind = line[kind_start..].split_whitespace()
125+
.next()
126+
.expect("Encountered unexpected empty comment")
127+
.parse::<ErrorKind>()
128+
.ok();
89129
let letters = line[kind_start..].chars();
90130
let msg = letters.skip_while(|c| c.is_whitespace())
91131
.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;
@@ -1033,8 +1033,8 @@ fn check_expected_errors(revision: Option<&str>,
10331033
expected_errors.iter()
10341034
.fold((false, false),
10351035
|(acc_help, acc_note), ee|
1036-
(acc_help || ee.kind == "help:" || ee.kind == "help",
1037-
acc_note || ee.kind == "note:" || ee.kind == "note"));
1036+
(acc_help || ee.kind == Some(ErrorKind::Help),
1037+
acc_note || ee.kind == Some(ErrorKind::Note)));
10381038

10391039
// Scan and extract our error/warning messages,
10401040
// which look like:
@@ -1052,15 +1052,15 @@ fn check_expected_errors(revision: Option<&str>,
10521052
let mut prev = 0;
10531053
for (i, ee) in expected_errors.iter().enumerate() {
10541054
if !found_flags[i] {
1055-
debug!("prefix={} ee.kind={} ee.msg={} line={}",
1055+
debug!("prefix={} ee.kind={:?} ee.msg={} line={}",
10561056
prefixes[i],
10571057
ee.kind,
10581058
ee.msg,
10591059
line);
10601060
// Suggestions have no line number in their output, so take on the line number of
10611061
// the previous expected error
1062-
if ee.kind == "suggestion" {
1063-
assert!(expected_errors[prev].kind == "help",
1062+
if ee.kind == Some(ErrorKind::Suggestion) {
1063+
assert!(expected_errors[prev].kind == Some(ErrorKind::Help),
10641064
"SUGGESTIONs must be preceded by a HELP");
10651065
if line.contains(&ee.msg) {
10661066
found_flags[i] = true;
@@ -1070,7 +1070,7 @@ fn check_expected_errors(revision: Option<&str>,
10701070
}
10711071
if
10721072
(prefix_matches(line, &prefixes[i]) || continuation(line)) &&
1073-
line.contains(&ee.kind) &&
1073+
(ee.kind.is_none() || line.contains(&ee.kind.as_ref().unwrap().to_string())) &&
10741074
line.contains(&ee.msg)
10751075
{
10761076
found_flags[i] = true;
@@ -1096,7 +1096,10 @@ fn check_expected_errors(revision: Option<&str>,
10961096
if !flag {
10971097
let ee = &expected_errors[i];
10981098
error(revision, &format!("expected {} on line {} not found: {}",
1099-
ee.kind, ee.line_num, ee.msg));
1099+
ee.kind.as_ref()
1100+
.map_or("message".into(),
1101+
|k| k.to_string()),
1102+
ee.line_num, ee.msg));
11001103
not_found += 1;
11011104
}
11021105
}

0 commit comments

Comments
 (0)