1 use self::WhichLine::*;
5 use std::io::prelude::*;
6 use std::io::BufReader;
12 #[derive(Clone, Debug, PartialEq)]
21 impl FromStr for ErrorKind {
23 fn from_str(s: &str) -> Result<Self, Self::Err> {
24 let s = s.to_uppercase();
25 let part0: &str = s.split(':').next().unwrap();
27 "HELP" => Ok(ErrorKind::Help),
28 "ERROR" => Ok(ErrorKind::Error),
29 "NOTE" => Ok(ErrorKind::Note),
30 "SUGGESTION" => Ok(ErrorKind::Suggestion),
31 "WARN" | "WARNING" => Ok(ErrorKind::Warning),
37 impl fmt::Display for ErrorKind {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 ErrorKind::Help => write!(f, "help message"),
41 ErrorKind::Error => write!(f, "error"),
42 ErrorKind::Note => write!(f, "note"),
43 ErrorKind::Suggestion => write!(f, "suggestion"),
44 ErrorKind::Warning => write!(f, "warning"),
52 /// What kind of message we expect (e.g., warning, error, suggestion).
53 /// `None` if not specified or unknown message kind.
54 pub kind: Option<ErrorKind>,
58 #[derive(PartialEq, Debug)]
61 FollowPrevious(usize),
62 AdjustBackward(usize),
65 /// Looks for either "//~| KIND MESSAGE" or "//~^^... KIND MESSAGE"
66 /// The former is a "follow" that inherits its target from the preceding line;
67 /// the latter is an "adjusts" that goes that many lines up.
69 /// Goal is to enable tests both like: //~^^^ ERROR go up three
70 /// and also //~^ ERROR message one for the preceding line, and
71 /// //~| ERROR message two for that same line.
73 /// If cfg is not None (i.e., in an incremental test), then we look
74 /// for `//[X]~` instead, where `X` is the current `cfg`.
75 pub fn load_errors(testfile: &Path, cfg: Option<&str>) -> Vec<Error> {
76 let rdr = BufReader::new(File::open(testfile).unwrap());
78 // `last_nonfollow_error` tracks the most recently seen
79 // line with an error template that did not use the
80 // follow-syntax, "//~| ...".
82 // (pnkfelix could not find an easy way to compose Iterator::scan
83 // and Iterator::filter_map to pass along this information into
84 // `parse_expected`. So instead I am storing that state here and
85 // updating it in the map callback below.)
86 let mut last_nonfollow_error = None;
89 Some(rev) => format!("//[{}]~", rev),
90 None => "//~".to_string(),
95 .filter_map(|(line_num, line)| {
96 parse_expected(last_nonfollow_error, line_num + 1, &line.unwrap(), &tag).map(
99 FollowPrevious(_) => {}
100 _ => last_nonfollow_error = Some(error.line_num),
110 last_nonfollow_error: Option<usize>,
114 ) -> Option<(WhichLine, Error)> {
115 let start = line.find(tag)?;
116 let (follow, adjusts) = if line[start + tag.len()..].chars().next().unwrap() == '|' {
121 line[start + tag.len()..]
123 .take_while(|c| *c == '^')
127 let kind_start = start + tag.len() + adjusts + (follow as usize);
129 match line[kind_start..]
132 .expect("Encountered unexpected empty comment")
133 .parse::<ErrorKind>()
136 // If we find `//~ ERROR foo` or something like that:
138 let letters = line[kind_start..].chars();
140 .skip_while(|c| c.is_whitespace())
141 .skip_while(|c| !c.is_whitespace())
142 .collect::<String>();
145 // Otherwise we found `//~ foo`:
147 let letters = line[kind_start..].chars();
149 .skip_while(|c| c.is_whitespace())
150 .collect::<String>();
153 let msg = msg.trim().to_owned();
155 let (which, line_num) = if follow {
156 assert_eq!(adjusts, 0, "use either //~| or //~^, not both.");
157 let line_num = last_nonfollow_error.expect(
158 "encountered //~| without \
159 preceding //~^ line.",
161 (FollowPrevious(line_num), line_num)
163 let which = if adjusts > 0 {
164 AdjustBackward(adjusts)
168 let line_num = line_num - adjusts;
173 "line={} tag={:?} which={:?} kind={:?} msg={:?}",
174 line_num, tag, which, kind, msg