]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/errors.rs
5b3936ffc1e3b716c9942d1f285aba7802c86b61
[rust.git] / src / tools / compiletest / src / errors.rs
1 use self::WhichLine::*;
2
3 use std::fmt;
4 use std::fs::File;
5 use std::io::prelude::*;
6 use std::io::BufReader;
7 use std::path::Path;
8 use std::str::FromStr;
9
10 use log::*;
11
12 #[derive(Clone, Debug, PartialEq)]
13 pub enum ErrorKind {
14     Help,
15     Error,
16     Note,
17     Suggestion,
18     Warning,
19 }
20
21 impl FromStr for ErrorKind {
22     type Err = ();
23     fn from_str(s: &str) -> Result<Self, Self::Err> {
24         let s = s.to_uppercase();
25         let part0: &str = s.split(':').next().unwrap();
26         match part0 {
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),
32             _ => Err(()),
33         }
34     }
35 }
36
37 impl fmt::Display for ErrorKind {
38     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39         match *self {
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"),
45         }
46     }
47 }
48
49 #[derive(Debug)]
50 pub struct Error {
51     pub line_num: usize,
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>,
55     pub msg: String,
56 }
57
58 #[derive(PartialEq, Debug)]
59 enum WhichLine {
60     ThisLine,
61     FollowPrevious(usize),
62     AdjustBackward(usize),
63 }
64
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.
68 ///
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.
72 ///
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());
77
78     // `last_nonfollow_error` tracks the most recently seen
79     // line with an error template that did not use the
80     // follow-syntax, "//~| ...".
81     //
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;
87
88     let tag = match cfg {
89         Some(rev) => format!("//[{}]~", rev),
90         None => "//~".to_string(),
91     };
92
93     rdr.lines()
94         .enumerate()
95         .filter_map(|(line_num, line)| {
96             parse_expected(last_nonfollow_error, line_num + 1, &line.unwrap(), &tag).map(
97                 |(which, error)| {
98                     match which {
99                         FollowPrevious(_) => {}
100                         _ => last_nonfollow_error = Some(error.line_num),
101                     }
102                     error
103                 },
104             )
105         })
106         .collect()
107 }
108
109 fn parse_expected(
110     last_nonfollow_error: Option<usize>,
111     line_num: usize,
112     line: &str,
113     tag: &str,
114 ) -> Option<(WhichLine, Error)> {
115     let start = line.find(tag)?;
116     let (follow, adjusts) = if line[start + tag.len()..].chars().next().unwrap() == '|' {
117         (true, 0)
118     } else {
119         (
120             false,
121             line[start + tag.len()..]
122                 .chars()
123                 .take_while(|c| *c == '^')
124                 .count(),
125         )
126     };
127     let kind_start = start + tag.len() + adjusts + (follow as usize);
128     let (kind, msg);
129     match line[kind_start..]
130         .split_whitespace()
131         .next()
132         .expect("Encountered unexpected empty comment")
133         .parse::<ErrorKind>()
134     {
135         Ok(k) => {
136             // If we find `//~ ERROR foo` or something like that:
137             kind = Some(k);
138             let letters = line[kind_start..].chars();
139             msg = letters
140                 .skip_while(|c| c.is_whitespace())
141                 .skip_while(|c| !c.is_whitespace())
142                 .collect::<String>();
143         }
144         Err(_) => {
145             // Otherwise we found `//~ foo`:
146             kind = None;
147             let letters = line[kind_start..].chars();
148             msg = letters
149                 .skip_while(|c| c.is_whitespace())
150                 .collect::<String>();
151         }
152     }
153     let msg = msg.trim().to_owned();
154
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.",
160         );
161         (FollowPrevious(line_num), line_num)
162     } else {
163         let which = if adjusts > 0 {
164             AdjustBackward(adjusts)
165         } else {
166             ThisLine
167         };
168         let line_num = line_num - adjusts;
169         (which, line_num)
170     };
171
172     debug!(
173         "line={} tag={:?} which={:?} kind={:?} msg={:?}",
174         line_num, tag, which, kind, msg
175     );
176     Some((
177         which,
178         Error {
179             line_num,
180             kind,
181             msg,
182         },
183     ))
184 }