compiletest/
errors.rs
1use std::fmt;
2use std::fs::File;
3use std::io::BufReader;
4use std::io::prelude::*;
5use std::path::Path;
6use std::str::FromStr;
7use std::sync::OnceLock;
8
9use regex::Regex;
10use tracing::*;
11
12use self::WhichLine::*;
13
14#[derive(Copy, Clone, Debug, PartialEq)]
15pub enum ErrorKind {
16 Help,
17 Error,
18 Note,
19 Suggestion,
20 Warning,
21}
22
23impl 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
39impl 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)]
52pub struct Error {
53 pub line_num: usize,
54 pub kind: Option<ErrorKind>,
57 pub msg: String,
58}
59
60impl Error {
61 pub fn render_for_expected(&self) -> String {
62 use colored::Colorize;
63 format!(
64 "{: <10}line {: >3}: {}",
65 self.kind.map(|kind| kind.to_string()).unwrap_or_default().to_uppercase(),
66 self.line_num,
67 self.msg.cyan(),
68 )
69 }
70}
71
72#[derive(PartialEq, Debug)]
73enum WhichLine {
74 ThisLine,
75 FollowPrevious(usize),
76 AdjustBackward(usize),
77}
78
79pub fn load_errors(testfile: &Path, revision: Option<&str>) -> Vec<Error> {
90 let rdr = BufReader::new(File::open(testfile).unwrap());
91
92 let mut last_nonfollow_error = None;
101
102 rdr.lines()
103 .enumerate()
104 .filter(|(_, line)| line.is_ok())
106 .filter_map(|(line_num, line)| {
107 parse_expected(last_nonfollow_error, line_num + 1, &line.unwrap(), revision).map(
108 |(which, error)| {
109 match which {
110 FollowPrevious(_) => {}
111 _ => last_nonfollow_error = Some(error.line_num),
112 }
113
114 error
115 },
116 )
117 })
118 .collect()
119}
120
121fn parse_expected(
122 last_nonfollow_error: Option<usize>,
123 line_num: usize,
124 line: &str,
125 test_revision: Option<&str>,
126) -> Option<(WhichLine, Error)> {
127 static RE: OnceLock<Regex> = OnceLock::new();
135
136 let captures = RE
137 .get_or_init(|| Regex::new(r"//(?:\[(?P<revs>[\w\-,]+)])?~(?P<adjust>\||\^*)").unwrap())
138 .captures(line)?;
139
140 match (test_revision, captures.name("revs")) {
141 (Some(test_revision), Some(revision_filters)) => {
143 if !revision_filters.as_str().split(',').any(|r| r == test_revision) {
144 return None;
145 }
146 }
147
148 (None, Some(_)) => panic!("Only tests with revisions should use `//[X]~`"),
149
150 (Some(_), None) | (None, None) => {}
152 }
153
154 let (follow, adjusts) = match &captures["adjust"] {
155 "|" => (true, 0),
156 circumflexes => (false, circumflexes.len()),
157 };
158
159 let whole_match = captures.get(0).unwrap();
161 let (_, mut msg) = line.split_at(whole_match.end());
162
163 let first_word = msg.split_whitespace().next().expect("Encountered unexpected empty comment");
164
165 let kind = first_word.parse::<ErrorKind>().ok();
167 if kind.is_some() {
168 msg = &msg.trim_start().split_at(first_word.len()).1;
169 }
170
171 let msg = msg.trim().to_owned();
172
173 let (which, line_num) = if follow {
174 assert_eq!(adjusts, 0, "use either //~| or //~^, not both.");
175 let line_num = last_nonfollow_error.expect(
176 "encountered //~| without \
177 preceding //~^ line.",
178 );
179 (FollowPrevious(line_num), line_num)
180 } else {
181 let which = if adjusts > 0 { AdjustBackward(adjusts) } else { ThisLine };
182 let line_num = line_num - adjusts;
183 (which, line_num)
184 };
185
186 debug!(
187 "line={} tag={:?} which={:?} kind={:?} msg={:?}",
188 line_num,
189 whole_match.as_str(),
190 which,
191 kind,
192 msg
193 );
194 Some((which, Error { line_num, kind, msg }))
195}
196
197#[cfg(test)]
198mod tests;