Skip to content

Commit

Permalink
Make tidy error code parsing robust against comments
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Mar 20, 2024
1 parent 958a022 commit 7f9a4af
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/tools/tidy/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,50 +71,67 @@ fn extract_error_codes(root_path: &Path, errors: &mut Vec<String>) -> Vec<String
let path = root_path.join(Path::new(ERROR_CODES_PATH));
let file =
fs::read_to_string(&path).unwrap_or_else(|e| panic!("failed to read `{path:?}`: {e}"));
let path = path.display();

let mut error_codes = Vec::new();

for line in file.lines() {
for (line_index, line) in file.lines().enumerate() {
let line_index = line_index + 1;
let line = line.trim();

if line.starts_with('E') {
let split_line = line.split_once(':');

// Extract the error code from the line. Emit a fatal error if it is not in the correct
// format.
let err_code = if let Some(err_code) = split_line {
err_code.0.to_owned()
} else {
let Some(split_line) = split_line else {
errors.push(format!(
"Expected a line with the format `Eabcd: abcd, \
"{path}:{line_index}: Expected a line with the format `Eabcd: abcd, \
but got \"{}\" without a `:` delimiter",
line,
));
continue;
};

let err_code = split_line.0.to_owned();

// If this is a duplicate of another error code, emit a fatal error.
if error_codes.contains(&err_code) {
errors.push(format!("Found duplicate error code: `{}`", err_code));
errors.push(format!(
"{path}:{line_index}: Found duplicate error code: `{}`",
err_code
));
continue;
}

let mut chars = err_code.chars();
chars.next();
assert_eq!(chars.next(), Some('E'));
let error_num_as_str = chars.as_str();

// Ensure that the line references the correct markdown file.
let expected_filename = format!(" {},", error_num_as_str);
if expected_filename != split_line.unwrap().1 {
let rest = split_line.1.split_once(',');
let Some(rest) = rest else {
errors.push(format!(
"{path}:{line_index}: Expected a line with the format `Eabcd: abcd, \
but got \"{}\" without a `,` delimiter",
line,
));
continue;
};
if error_num_as_str != rest.0.trim() {
errors.push(format!(
"`{}:` should be followed by `{}` but instead found `{}` in \
"{path}:{line_index}: `{}:` should be followed by `{},` but instead found `{}` in \
`compiler/rustc_error_codes/src/lib.rs`",
err_code,
expected_filename,
split_line.unwrap().1,
error_num_as_str,
split_line.1,
));
continue;
}
if !rest.1.trim().is_empty() && !rest.1.trim().starts_with("//") {
errors.push(format!("{path}:{line_index}: should only have one error per line"));
continue;
}

error_codes.push(err_code);
}
Expand Down Expand Up @@ -146,7 +163,7 @@ fn check_error_codes_docs(
return;
}

// Make sure that the file is referenced in `error_codes.rs`
// Make sure that the file is referenced in `rustc_error_codes/src/lib.rs`
let filename = path.file_name().unwrap().to_str().unwrap().split_once('.');
let err_code = filename.unwrap().0; // `unwrap` is ok because we know the filename is in the correct format.

Expand Down

0 comments on commit 7f9a4af

Please sign in to comment.