Skip to content

Commit 53597a8

Browse files
committed
Tidy check for test revisions that are mentioned but not declared
If a `[revision]` name appears in a test header directive or error annotation, but isn't declared in the `//@ revisions:` header, that is almost always a mistake. In cases where a revision needs to be temporarily disabled, adding it to an `//@ unused-revision-names:` header will suppress these checks for that name. Adding the wildcard name `*` to the unused list will suppress these checks for the entire file.
1 parent 9157135 commit 53597a8

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

src/tools/compiletest/src/header.rs

+1
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
925925
"test-mir-pass",
926926
"unset-exec-env",
927927
"unset-rustc-env",
928+
("unused-revision-names"/* used by tidy check `unknown_revision` */),
928929
// tidy-alphabetical-end
929930
];
930931

src/tools/tidy/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub mod tests_placement;
8787
pub mod tests_revision_unpaired_stdout_stderr;
8888
pub mod ui_tests;
8989
pub mod unit_tests;
90+
pub mod unknown_revision;
9091
pub mod unstable_book;
9192
pub mod walk;
9293
pub mod x_version;

src/tools/tidy/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ fn main() {
110110
check!(rustdoc_gui_tests, &tests_path);
111111
check!(rustdoc_css_themes, &librustdoc_path);
112112
check!(known_bug, &crashes_path);
113+
check!(unknown_revision, &tests_path);
113114

114115
// Checks that only make sense for the compiler.
115116
check!(error_codes, &root_path, &[&compiler_path, &librustdoc_path], verbose);
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//! Checks that test revision names appearing in header directives and error
2+
//! annotations have actually been declared in `revisions`.
3+
4+
use std::collections::{BTreeSet, HashMap, HashSet};
5+
use std::path::Path;
6+
use std::sync::OnceLock;
7+
8+
use ignore::DirEntry;
9+
use regex::Regex;
10+
11+
use crate::iter_header::{iter_header, HeaderLine};
12+
use crate::walk::{filter_dirs, filter_not_rust, walk};
13+
14+
pub fn check(tests_path: impl AsRef<Path>, bad: &mut bool) {
15+
walk(
16+
tests_path.as_ref(),
17+
|path, is_dir| {
18+
filter_dirs(path) || filter_not_rust(path) || {
19+
// Auxiliary source files for incremental tests can refer to revisions
20+
// declared by the main file, which this check doesn't handle.
21+
is_dir && path.file_name().is_some_and(|name| name == "auxiliary")
22+
}
23+
},
24+
&mut |entry, contents| visit_test_file(entry, contents, bad),
25+
);
26+
}
27+
28+
fn visit_test_file(entry: &DirEntry, contents: &str, bad: &mut bool) {
29+
let mut revisions = HashSet::new();
30+
let mut unused_revision_names = HashSet::new();
31+
32+
// Maps each mentioned revision to the first line it was mentioned on.
33+
let mut mentioned_revisions = HashMap::<&str, usize>::new();
34+
let mut add_mentioned_revision = |line_number: usize, revision| {
35+
let first_line = mentioned_revisions.entry(revision).or_insert(line_number);
36+
*first_line = (*first_line).min(line_number);
37+
};
38+
39+
// Scan all `//@` headers to find declared revisions and mentioned revisions.
40+
iter_header(contents, &mut |HeaderLine { line_number, revision, directive }| {
41+
if let Some(revs) = directive.strip_prefix("revisions:") {
42+
revisions.extend(revs.split_whitespace());
43+
} else if let Some(revs) = directive.strip_prefix("unused-revision-names:") {
44+
unused_revision_names.extend(revs.split_whitespace());
45+
}
46+
47+
if let Some(revision) = revision {
48+
add_mentioned_revision(line_number, revision);
49+
}
50+
});
51+
52+
// If a wildcard appears in `unused-revision-names`, skip all revision name
53+
// checking for this file.
54+
if unused_revision_names.contains(&"*") {
55+
return;
56+
}
57+
58+
// Scan all `//[rev]~` error annotations to find mentioned revisions.
59+
for_each_error_annotation_revision(contents, &mut |ErrorAnnRev { line_number, revision }| {
60+
add_mentioned_revision(line_number, revision);
61+
});
62+
63+
let path = entry.path().display();
64+
65+
// Fail if any revision names appear in both places, since that's probably a mistake.
66+
for rev in revisions.intersection(&unused_revision_names).copied().collect::<BTreeSet<_>>() {
67+
tidy_error!(
68+
bad,
69+
"revision name [{rev}] appears in both `revisions` and `unused-revision-names` in {path}"
70+
);
71+
}
72+
73+
// Compute the set of revisions that were mentioned but not declared,
74+
// sorted by the first line number they appear on.
75+
let mut bad_revisions = mentioned_revisions
76+
.into_iter()
77+
.filter(|(rev, _)| !revisions.contains(rev) && !unused_revision_names.contains(rev))
78+
.map(|(rev, line_number)| (line_number, rev))
79+
.collect::<Vec<_>>();
80+
bad_revisions.sort();
81+
82+
for (line_number, rev) in bad_revisions {
83+
tidy_error!(bad, "unknown revision [{rev}] at {path}:{line_number}");
84+
}
85+
}
86+
87+
struct ErrorAnnRev<'a> {
88+
line_number: usize,
89+
revision: &'a str,
90+
}
91+
92+
fn for_each_error_annotation_revision<'a>(
93+
contents: &'a str,
94+
callback: &mut dyn FnMut(ErrorAnnRev<'a>),
95+
) {
96+
let error_regex = {
97+
// Simplified from the regex used by `parse_expected` in `src/tools/compiletest/src/errors.rs`,
98+
// because we only care about extracting revision names.
99+
static RE: OnceLock<Regex> = OnceLock::new();
100+
RE.get_or_init(|| Regex::new(r"//\[(?<revs>[^]]*)\]~").unwrap())
101+
};
102+
103+
for (line_number, line) in (1..).zip(contents.lines()) {
104+
let Some(captures) = error_regex.captures(line) else { continue };
105+
106+
for revision in captures.name("revs").unwrap().as_str().split(',') {
107+
callback(ErrorAnnRev { line_number, revision });
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)