1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
use std::{ffi::OsStr, fs, path::Path};
use regex::Regex;
use crate::walk::{filter_dirs, walk, walk_many};
const ERROR_CODES_PATH: &str = "compiler/rustc_error_codes/src/error_codes.rs";
const ERROR_DOCS_PATH: &str = "compiler/rustc_error_codes/src/error_codes/";
const ERROR_TESTS_PATH: &str = "tests/ui/error-codes/";
const IGNORE_DOCTEST_CHECK: &[&str] = &["E0464", "E0570", "E0601", "E0602", "E0640", "E0717"];
const IGNORE_UI_TEST_CHECK: &[&str] =
&["E0461", "E0465", "E0514", "E0554", "E0640", "E0717", "E0729"];
macro_rules! verbose_print {
($verbose:expr, $($fmt:tt)*) => {
if $verbose {
println!("{}", format_args!($($fmt)*));
}
};
}
pub fn check(root_path: &Path, search_paths: &[&Path], verbose: bool, bad: &mut bool) {
let mut errors = Vec::new();
let error_codes = extract_error_codes(root_path, &mut errors);
if verbose {
println!("Found {} error codes", error_codes.len());
println!("Highest error code: `{}`", error_codes.iter().max().unwrap());
}
let no_longer_emitted = check_error_codes_docs(root_path, &error_codes, &mut errors, verbose);
check_error_codes_tests(root_path, &error_codes, &mut errors, verbose, &no_longer_emitted);
check_error_codes_used(search_paths, &error_codes, &mut errors, &no_longer_emitted, verbose);
for error in errors {
tidy_error!(bad, "{}", error);
}
}
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 mut error_codes = Vec::new();
for line in file.lines() {
let line = line.trim();
if line.starts_with('E') {
let split_line = line.split_once(':');
let err_code = if let Some(err_code) = split_line {
err_code.0.to_owned()
} else {
errors.push(format!(
"Expected a line with the format `Exxxx: include_str!(\"..\")`, but got \"{}\" \
without a `:` delimiter",
line,
));
continue;
};
if error_codes.contains(&err_code) {
errors.push(format!("Found duplicate error code: `{}`", err_code));
continue;
}
let expected_filename = format!(" include_str!(\"./error_codes/{}.md\"),", err_code);
if expected_filename != split_line.unwrap().1 {
errors.push(format!(
"Error code `{}` expected to reference docs with `{}` but instead found `{}` in \
`compiler/rustc_error_codes/src/error_codes.rs`",
err_code,
expected_filename,
split_line.unwrap().1,
));
continue;
}
error_codes.push(err_code);
}
}
error_codes
}
fn check_error_codes_docs(
root_path: &Path,
error_codes: &[String],
errors: &mut Vec<String>,
verbose: bool,
) -> Vec<String> {
let docs_path = root_path.join(Path::new(ERROR_DOCS_PATH));
let mut no_longer_emitted_codes = Vec::new();
walk(&docs_path, |_| false, &mut |entry, contents| {
let path = entry.path();
if path.extension() != Some(OsStr::new("md")) {
errors.push(format!(
"Found unexpected non-markdown file in error code docs directory: {}",
path.display()
));
return;
}
let filename = path.file_name().unwrap().to_str().unwrap().split_once('.');
let err_code = filename.unwrap().0; if error_codes.iter().all(|e| e != err_code) {
errors.push(format!(
"Found valid file `{}` in error code docs directory without corresponding \
entry in `error_code.rs`",
path.display()
));
return;
}
let (found_code_example, found_proper_doctest, emit_ignore_warning, no_longer_emitted) =
check_explanation_has_doctest(&contents, &err_code);
if emit_ignore_warning {
verbose_print!(
verbose,
"warning: Error code `{err_code}` uses the ignore header. This should not be used, add the error code to the \
`IGNORE_DOCTEST_CHECK` constant instead."
);
}
if no_longer_emitted {
no_longer_emitted_codes.push(err_code.to_owned());
}
if !found_code_example {
verbose_print!(
verbose,
"warning: Error code `{err_code}` doesn't have a code example, all error codes are expected to have one \
(even if untested)."
);
return;
}
let test_ignored = IGNORE_DOCTEST_CHECK.contains(&&err_code);
if !found_proper_doctest && !test_ignored {
errors.push(format!(
"`{}` doesn't use its own error code in compile_fail example",
path.display(),
));
} else if found_proper_doctest && test_ignored {
errors.push(format!(
"`{}` has a compile_fail doctest with its own error code, it shouldn't \
be listed in `IGNORE_DOCTEST_CHECK`",
path.display(),
));
}
});
no_longer_emitted_codes
}
fn check_explanation_has_doctest(explanation: &str, err_code: &str) -> (bool, bool, bool, bool) {
let mut found_code_example = false;
let mut found_proper_doctest = false;
let mut emit_ignore_warning = false;
let mut no_longer_emitted = false;
for line in explanation.lines() {
let line = line.trim();
if line.starts_with("```") {
found_code_example = true;
if line.contains("compile_fail") && line.contains(err_code) {
found_proper_doctest = true;
}
if line.contains("ignore") {
emit_ignore_warning = true;
found_proper_doctest = true;
}
} else if line
.starts_with("#### Note: this error code is no longer emitted by the compiler")
{
no_longer_emitted = true;
found_code_example = true;
found_proper_doctest = true;
}
}
(found_code_example, found_proper_doctest, emit_ignore_warning, no_longer_emitted)
}
fn check_error_codes_tests(
root_path: &Path,
error_codes: &[String],
errors: &mut Vec<String>,
verbose: bool,
no_longer_emitted: &[String],
) {
let tests_path = root_path.join(Path::new(ERROR_TESTS_PATH));
for code in error_codes {
let test_path = tests_path.join(format!("{}.stderr", code));
if !test_path.exists() && !IGNORE_UI_TEST_CHECK.contains(&code.as_str()) {
verbose_print!(
verbose,
"warning: Error code `{code}` needs to have at least one UI test in the `tests/error-codes/` directory`!"
);
continue;
}
if IGNORE_UI_TEST_CHECK.contains(&code.as_str()) {
if test_path.exists() {
errors.push(format!(
"Error code `{code}` has a UI test in `tests/ui/error-codes/{code}.rs`, it shouldn't be listed in `EXEMPTED_FROM_TEST`!"
));
}
continue;
}
let file = match fs::read_to_string(&test_path) {
Ok(file) => file,
Err(err) => {
verbose_print!(
verbose,
"warning: Failed to read UI test file (`{}`) for `{code}` but the file exists. The test is assumed to work:\n{err}",
test_path.display()
);
continue;
}
};
if no_longer_emitted.contains(code) {
continue;
}
let mut found_code = false;
for line in file.lines() {
let s = line.trim();
if s.starts_with("error[E") {
if &s[6..11] == code {
found_code = true;
break;
}
};
}
if !found_code {
verbose_print!(
verbose,
"warning: Error code {code}`` has a UI test file, but doesn't contain its own error code!"
);
}
}
}
fn check_error_codes_used(
search_paths: &[&Path],
error_codes: &[String],
errors: &mut Vec<String>,
no_longer_emitted: &[String],
verbose: bool,
) {
let regex = Regex::new(r#"[(,"\s](E\d{4})[,)"]"#).unwrap();
let mut found_codes = Vec::new();
walk_many(search_paths, filter_dirs, &mut |entry, contents| {
let path = entry.path();
if path.extension() != Some(OsStr::new("rs")) {
return;
}
for line in contents.lines() {
if line.trim_start().starts_with("//") {
continue;
}
for cap in regex.captures_iter(line) {
if let Some(error_code) = cap.get(1) {
let error_code = error_code.as_str().to_owned();
if !error_codes.contains(&error_code) {
errors.push(format!("Error code `{}` is used in the compiler but not defined and documented in `compiler/rustc_error_codes/src/error_codes.rs`.", error_code));
continue;
}
found_codes.push(error_code);
}
}
}
});
for code in error_codes {
if !found_codes.contains(code) && !no_longer_emitted.contains(code) {
errors.push(format!("Error code `{code}` exists, but is not emitted by the compiler!"))
}
if found_codes.contains(code) && no_longer_emitted.contains(code) {
verbose_print!(
verbose,
"warning: Error code `{code}` is used when it's marked as \"no longer emitted\""
);
}
}
}