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    /// 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
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
79/// Looks for either "//~| KIND MESSAGE" or "//~^^... KIND MESSAGE"
80/// The former is a "follow" that inherits its target from the preceding line;
81/// the latter is an "adjusts" that goes that many lines up.
82///
83/// Goal is to enable tests both like: //~^^^ ERROR go up three
84/// and also //~^ ERROR message one for the preceding line, and
85///          //~| ERROR message two for that same line.
86///
87/// If revision is not None, then we look
88/// for `//[X]~` instead, where `X` is the current revision.
89pub fn load_errors(testfile: &Path, revision: Option<&str>) -> Vec<Error> {
90    let rdr = BufReader::new(File::open(testfile).unwrap());
91
92    // `last_nonfollow_error` tracks the most recently seen
93    // line with an error template that did not use the
94    // follow-syntax, "//~| ...".
95    //
96    // (pnkfelix could not find an easy way to compose Iterator::scan
97    // and Iterator::filter_map to pass along this information into
98    // `parse_expected`. So instead I am storing that state here and
99    // updating it in the map callback below.)
100    let mut last_nonfollow_error = None;
101
102    rdr.lines()
103        .enumerate()
104        // We want to ignore utf-8 failures in tests during collection of annotations.
105        .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    // Matches comments like:
128    //     //~
129    //     //~|
130    //     //~^
131    //     //~^^^^^
132    //     //[rev1]~
133    //     //[rev1,rev2]~^^
134    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        // Only error messages that contain our revision between the square brackets apply to us.
142        (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        // If an error has no list of revisions, it applies to all revisions.
151        (Some(_), None) | (None, None) => {}
152    }
153
154    let (follow, adjusts) = match &captures["adjust"] {
155        "|" => (true, 0),
156        circumflexes => (false, circumflexes.len()),
157    };
158
159    // Get the part of the comment after the sigil (e.g. `~^^` or ~|).
160    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    // If we find `//~ ERROR foo` or something like that, skip the first word.
166    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;