]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/errors.rs
Rollup merge of #66325 - BartMassey:master, r=joshtriplett
[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 lazy_static::lazy_static;
11 use log::*;
12 use regex::Regex;
13
14 #[derive(Clone, Debug, PartialEq)]
15 pub enum ErrorKind {
16     Help,
17     Error,
18     Note,
19     Suggestion,
20     Warning,
21 }
22
23 impl FromStr for ErrorKind {
24     type Err = ();
25     fn from_str(s: &str) -> Result<Self, Self::Err> {
26         let s = s.to_uppercase();
27         let part0: &str = s.split(':').next().unwrap();
28         match part0 {
29             "HELP" => Ok(ErrorKind::Help),
30             "ERROR" => Ok(ErrorKind::Error),
31             "NOTE" => Ok(ErrorKind::Note),
32             "SUGGESTION" => Ok(ErrorKind::Suggestion),
33             "WARN" | "WARNING" => Ok(ErrorKind::Warning),
34             _ => Err(()),
35         }
36     }
37 }
38
39 impl fmt::Display for ErrorKind {
40     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41         match *self {
42             ErrorKind::Help => write!(f, "help message"),
43             ErrorKind::Error => write!(f, "error"),
44             ErrorKind::Note => write!(f, "note"),
45             ErrorKind::Suggestion => write!(f, "suggestion"),
46             ErrorKind::Warning => write!(f, "warning"),
47         }
48     }
49 }
50
51 #[derive(Debug)]
52 pub struct Error {
53     pub line_num: usize,
54     /// What kind of message we expect (e.g., warning, error, suggestion).
55     /// `None` if not specified or unknown message kind.
56     pub kind: Option<ErrorKind>,
57     pub msg: String,
58 }
59
60 #[derive(PartialEq, Debug)]
61 enum WhichLine {
62     ThisLine,
63     FollowPrevious(usize),
64     AdjustBackward(usize),
65 }
66
67 /// Looks for either "//~| KIND MESSAGE" or "//~^^... KIND MESSAGE"
68 /// The former is a "follow" that inherits its target from the preceding line;
69 /// the latter is an "adjusts" that goes that many lines up.
70 ///
71 /// Goal is to enable tests both like: //~^^^ ERROR go up three
72 /// and also //~^ ERROR message one for the preceding line, and
73 ///          //~| ERROR message two for that same line.
74 ///
75 /// If cfg is not None (i.e., in an incremental test), then we look
76 /// for `//[X]~` instead, where `X` is the current `cfg`.
77 pub fn load_errors(testfile: &Path, cfg: Option<&str>) -> Vec<Error> {
78     let rdr = BufReader::new(File::open(testfile).unwrap());
79
80     // `last_nonfollow_error` tracks the most recently seen
81     // line with an error template that did not use the
82     // follow-syntax, "//~| ...".
83     //
84     // (pnkfelix could not find an easy way to compose Iterator::scan
85     // and Iterator::filter_map to pass along this information into
86     // `parse_expected`. So instead I am storing that state here and
87     // updating it in the map callback below.)
88     let mut last_nonfollow_error = None;
89
90     rdr.lines()
91         .enumerate()
92         .filter_map(|(line_num, line)| {
93             parse_expected(last_nonfollow_error, line_num + 1, &line.unwrap(), cfg).map(
94                 |(which, error)| {
95                     match which {
96                         FollowPrevious(_) => {}
97                         _ => last_nonfollow_error = Some(error.line_num),
98                     }
99
100                     error
101                 },
102             )
103         })
104         .collect()
105 }
106
107 fn parse_expected(
108     last_nonfollow_error: Option<usize>,
109     line_num: usize,
110     line: &str,
111     cfg: Option<&str>,
112 ) -> Option<(WhichLine, Error)> {
113     // Matches comments like:
114     //     //~
115     //     //~|
116     //     //~^
117     //     //~^^^^^
118     //     //[cfg1]~
119     //     //[cfg1,cfg2]~^^
120     lazy_static! {
121         static ref RE: Regex =
122             Regex::new(r"//(?:\[(?P<cfgs>[\w,]+)])?~(?P<adjust>\||\^*)").unwrap();
123     }
124
125     let captures = RE.captures(line)?;
126
127     match (cfg, captures.name("cfgs")) {
128         // Only error messages that contain our `cfg` betweeen the square brackets apply to us.
129         (Some(cfg), Some(filter)) if !filter.as_str().split(',').any(|s| s == cfg)
130             => return None,
131         (Some(_), Some(_)) => {}
132
133         (None, Some(_)) => panic!("Only tests with revisions should use `//[X]~`"),
134
135         // If an error has no list of revisions, it applies to all revisions.
136         (Some(_), None) | (None, None) => {}
137     }
138
139     let (follow, adjusts) = match &captures["adjust"] {
140         "|" => (true, 0),
141         circumflexes => (false, circumflexes.len()),
142     };
143
144     // Get the part of the comment after the sigil (e.g. `~^^` or ~|).
145     let whole_match = captures.get(0).unwrap();
146     let (_, mut msg) = line.split_at(whole_match.end());
147
148     let first_word = msg
149         .split_whitespace()
150         .next()
151         .expect("Encountered unexpected empty comment");
152
153     // If we find `//~ ERROR foo` or something like that, skip the first word.
154     let kind = first_word.parse::<ErrorKind>().ok();
155     if let Some(_) = kind {
156         msg = &msg.trim_start().split_at(first_word.len()).1;
157     }
158
159     let msg = msg.trim().to_owned();
160
161     let (which, line_num) = if follow {
162         assert_eq!(adjusts, 0, "use either //~| or //~^, not both.");
163         let line_num = last_nonfollow_error.expect(
164             "encountered //~| without \
165              preceding //~^ line.",
166         );
167         (FollowPrevious(line_num), line_num)
168     } else {
169         let which = if adjusts > 0 {
170             AdjustBackward(adjusts)
171         } else {
172             ThisLine
173         };
174         let line_num = line_num - adjusts;
175         (which, line_num)
176     };
177
178     debug!(
179         "line={} tag={:?} which={:?} kind={:?} msg={:?}",
180         line_num, whole_match.as_str(), which, kind, msg
181     );
182     Some((
183         which,
184         Error {
185             line_num,
186             kind,
187             msg,
188         },
189     ))
190 }