1use std::ffi::OsStr;
20use std::fs;
21use std::path::Path;
22
23use regex::Regex;
24
25use crate::walk::{filter_dirs, walk, walk_many};
26
27const ERROR_CODES_PATH: &str = "compiler/rustc_error_codes/src/lib.rs";
28const ERROR_DOCS_PATH: &str = "compiler/rustc_error_codes/src/error_codes/";
29const ERROR_TESTS_PATH: &str = "tests/ui/error-codes/";
30
31const IGNORE_DOCTEST_CHECK: &[&str] = &["E0464", "E0570", "E0601", "E0602", "E0717"];
33
34const IGNORE_UI_TEST_CHECK: &[&str] =
36 &["E0461", "E0465", "E0514", "E0554", "E0640", "E0717", "E0729"];
37
38macro_rules! verbose_print {
39 ($verbose:expr, $($fmt:tt)*) => {
40 if $verbose {
41 println!("{}", format_args!($($fmt)*));
42 }
43 };
44}
45
46pub fn check(
47 root_path: &Path,
48 search_paths: &[&Path],
49 verbose: bool,
50 ci_info: &crate::CiInfo,
51 bad: &mut bool,
52) {
53 let mut errors = Vec::new();
54
55 check_removed_error_code_explanation(ci_info, bad);
57
58 let error_codes = extract_error_codes(root_path, &mut errors);
60 if verbose {
61 println!("Found {} error codes", error_codes.len());
62 println!("Highest error code: `{}`", error_codes.iter().max().unwrap());
63 }
64
65 let no_longer_emitted = check_error_codes_docs(root_path, &error_codes, &mut errors, verbose);
67
68 check_error_codes_tests(root_path, &error_codes, &mut errors, verbose, &no_longer_emitted);
70
71 check_error_codes_used(search_paths, &error_codes, &mut errors, &no_longer_emitted, verbose);
73
74 for error in errors {
76 tidy_error!(bad, "{}", error);
77 }
78}
79
80fn check_removed_error_code_explanation(ci_info: &crate::CiInfo, bad: &mut bool) {
81 let Some(base_commit) = &ci_info.base_commit else {
82 eprintln!("Skipping error code explanation removal check");
83 return;
84 };
85 let Some(diff) = crate::git_diff(base_commit, "--name-status") else {
86 *bad = true;
87 eprintln!("removed error code explanation tidy check: Failed to run git diff");
88 return;
89 };
90 if diff.lines().any(|line| {
91 line.starts_with('D') && line.contains("compiler/rustc_error_codes/src/error_codes/")
92 }) {
93 *bad = true;
94 eprintln!("tidy check error: Error code explanations should never be removed!");
95 eprintln!("Take a look at E0001 to see how to handle it.");
96 return;
97 }
98 println!("No error code explanation was removed!");
99}
100
101fn extract_error_codes(root_path: &Path, errors: &mut Vec<String>) -> Vec<String> {
103 let path = root_path.join(Path::new(ERROR_CODES_PATH));
104 let file =
105 fs::read_to_string(&path).unwrap_or_else(|e| panic!("failed to read `{path:?}`: {e}"));
106 let path = path.display();
107
108 let mut error_codes = Vec::new();
109
110 for (line_index, line) in file.lines().enumerate() {
111 let line_index = line_index + 1;
112 let line = line.trim();
113
114 if line.starts_with('E') {
115 let split_line = line.split_once(':');
116
117 let Some(split_line) = split_line else {
120 errors.push(format!(
121 "{path}:{line_index}: Expected a line with the format `Eabcd: abcd, \
122 but got \"{line}\" without a `:` delimiter",
123 ));
124 continue;
125 };
126
127 let err_code = split_line.0.to_owned();
128
129 if error_codes.contains(&err_code) {
131 errors
132 .push(format!("{path}:{line_index}: Found duplicate error code: `{err_code}`"));
133 continue;
134 }
135
136 let mut chars = err_code.chars();
137 assert_eq!(chars.next(), Some('E'));
138 let error_num_as_str = chars.as_str();
139
140 let rest = split_line.1.split_once(',');
142 let Some(rest) = rest else {
143 errors.push(format!(
144 "{path}:{line_index}: Expected a line with the format `Eabcd: abcd, \
145 but got \"{line}\" without a `,` delimiter",
146 ));
147 continue;
148 };
149 if error_num_as_str != rest.0.trim() {
150 errors.push(format!(
151 "{path}:{line_index}: `{}:` should be followed by `{},` but instead found `{}` in \
152 `compiler/rustc_error_codes/src/lib.rs`",
153 err_code,
154 error_num_as_str,
155 split_line.1,
156 ));
157 continue;
158 }
159 if !rest.1.trim().is_empty() && !rest.1.trim().starts_with("//") {
160 errors.push(format!("{path}:{line_index}: should only have one error per line"));
161 continue;
162 }
163
164 error_codes.push(err_code);
165 }
166 }
167
168 error_codes
169}
170
171fn check_error_codes_docs(
173 root_path: &Path,
174 error_codes: &[String],
175 errors: &mut Vec<String>,
176 verbose: bool,
177) -> Vec<String> {
178 let docs_path = root_path.join(Path::new(ERROR_DOCS_PATH));
179
180 let mut no_longer_emitted_codes = Vec::new();
181
182 walk(&docs_path, |_, _| false, &mut |entry, contents| {
183 let path = entry.path();
184
185 if path.extension() != Some(OsStr::new("md")) {
187 errors.push(format!(
188 "Found unexpected non-markdown file in error code docs directory: {}",
189 path.display()
190 ));
191 return;
192 }
193
194 let filename = path.file_name().unwrap().to_str().unwrap().split_once('.');
196 let err_code = filename.unwrap().0; if error_codes.iter().all(|e| e != err_code) {
199 errors.push(format!(
200 "Found valid file `{}` in error code docs directory without corresponding \
201 entry in `rustc_error_codes/src/lib.rs`",
202 path.display()
203 ));
204 return;
205 }
206
207 let (found_code_example, found_proper_doctest, emit_ignore_warning, no_longer_emitted) =
208 check_explanation_has_doctest(contents, err_code);
209
210 if emit_ignore_warning {
211 verbose_print!(
212 verbose,
213 "warning: Error code `{err_code}` uses the ignore header. This should not be used, add the error code to the \
214 `IGNORE_DOCTEST_CHECK` constant instead."
215 );
216 }
217
218 if no_longer_emitted {
219 no_longer_emitted_codes.push(err_code.to_owned());
220 }
221
222 if !found_code_example {
223 verbose_print!(
224 verbose,
225 "warning: Error code `{err_code}` doesn't have a code example, all error codes are expected to have one \
226 (even if untested)."
227 );
228 return;
229 }
230
231 let test_ignored = IGNORE_DOCTEST_CHECK.contains(&err_code);
232
233 if !found_proper_doctest && !test_ignored {
235 errors.push(format!(
236 "`{}` doesn't use its own error code in compile_fail example",
237 path.display(),
238 ));
239 } else if found_proper_doctest && test_ignored {
240 errors.push(format!(
241 "`{}` has a compile_fail doctest with its own error code, it shouldn't \
242 be listed in `IGNORE_DOCTEST_CHECK`",
243 path.display(),
244 ));
245 }
246 });
247
248 no_longer_emitted_codes
249}
250
251fn check_explanation_has_doctest(explanation: &str, err_code: &str) -> (bool, bool, bool, bool) {
255 let mut found_code_example = false;
256 let mut found_proper_doctest = false;
257
258 let mut emit_ignore_warning = false;
259 let mut no_longer_emitted = false;
260
261 for line in explanation.lines() {
262 let line = line.trim();
263
264 if line.starts_with("```") {
265 found_code_example = true;
266
267 if line.contains("compile_fail") && line.contains(err_code) {
269 found_proper_doctest = true;
270 }
271
272 if line.contains("ignore") {
273 emit_ignore_warning = true;
274 found_proper_doctest = true;
275 }
276 } else if line
277 .starts_with("#### Note: this error code is no longer emitted by the compiler")
278 {
279 no_longer_emitted = true;
280 found_code_example = true;
281 found_proper_doctest = true;
282 }
283 }
284
285 (found_code_example, found_proper_doctest, emit_ignore_warning, no_longer_emitted)
286}
287
288fn check_error_codes_tests(
290 root_path: &Path,
291 error_codes: &[String],
292 errors: &mut Vec<String>,
293 verbose: bool,
294 no_longer_emitted: &[String],
295) {
296 let tests_path = root_path.join(Path::new(ERROR_TESTS_PATH));
297
298 for code in error_codes {
299 let test_path = tests_path.join(format!("{code}.stderr"));
300
301 if !test_path.exists() && !IGNORE_UI_TEST_CHECK.contains(&code.as_str()) {
302 verbose_print!(
303 verbose,
304 "warning: Error code `{code}` needs to have at least one UI test in the `tests/error-codes/` directory`!"
305 );
306 continue;
307 }
308 if IGNORE_UI_TEST_CHECK.contains(&code.as_str()) {
309 if test_path.exists() {
310 errors.push(format!(
311 "Error code `{code}` has a UI test in `tests/ui/error-codes/{code}.rs`, it shouldn't be listed in `EXEMPTED_FROM_TEST`!"
312 ));
313 }
314 continue;
315 }
316
317 let file = match fs::read_to_string(&test_path) {
318 Ok(file) => file,
319 Err(err) => {
320 verbose_print!(
321 verbose,
322 "warning: Failed to read UI test file (`{}`) for `{code}` but the file exists. The test is assumed to work:\n{err}",
323 test_path.display()
324 );
325 continue;
326 }
327 };
328
329 if no_longer_emitted.contains(code) {
330 continue;
332 }
333
334 let mut found_code = false;
335
336 for line in file.lines() {
337 let s = line.trim();
338 if s.starts_with("error[E") && &s[6..11] == code {
340 found_code = true;
341 break;
342 };
343 }
344
345 if !found_code {
346 verbose_print!(
347 verbose,
348 "warning: Error code `{code}` has a UI test file, but doesn't contain its own error code!"
349 );
350 }
351 }
352}
353
354fn check_error_codes_used(
356 search_paths: &[&Path],
357 error_codes: &[String],
358 errors: &mut Vec<String>,
359 no_longer_emitted: &[String],
360 verbose: bool,
361) {
362 let regex = Regex::new(r#"\bE\d{4}\b"#).unwrap();
364
365 let mut found_codes = Vec::new();
366
367 walk_many(search_paths, |path, _is_dir| filter_dirs(path), &mut |entry, contents| {
368 let path = entry.path();
369
370 if path.extension() != Some(OsStr::new("rs")) {
372 return;
373 }
374
375 for line in contents.lines() {
376 if line.trim_start().starts_with("//") {
378 continue;
379 }
380
381 for cap in regex.captures_iter(line) {
382 if let Some(error_code) = cap.get(0) {
383 let error_code = error_code.as_str().to_owned();
384
385 if !error_codes.contains(&error_code) {
386 errors.push(format!("Error code `{error_code}` is used in the compiler but not defined and documented in `compiler/rustc_error_codes/src/lib.rs`."));
388 continue;
389 }
390
391 found_codes.push(error_code);
393 }
394 }
395 }
396 });
397
398 for code in error_codes {
399 if !found_codes.contains(code) && !no_longer_emitted.contains(code) {
400 errors.push(format!(
401 "Error code `{code}` exists, but is not emitted by the compiler!\n\
402 Please mark the code as no longer emitted by adding the following note to the top of the `EXXXX.md` file:\n\
403 `#### Note: this error code is no longer emitted by the compiler`\n\
404 Also, do not forget to mark doctests that no longer apply as `ignore (error is no longer emitted)`."
405 ));
406 }
407
408 if found_codes.contains(code) && no_longer_emitted.contains(code) {
409 verbose_print!(
410 verbose,
411 "warning: Error code `{code}` is used when it's marked as \"no longer emitted\""
412 );
413 }
414 }
415}