Skip to content

Commit b942c6d

Browse files
Rollup merge of #142827 - GuillaumeGomez:tidy-error-code-removal, r=Kobzol
Move error code explanation removal check into tidy Follow-up of #142677. This PR replaces a shell script with rust code. r? ghost
2 parents 8ba69d0 + 4780f21 commit b942c6d

File tree

7 files changed

+103
-69
lines changed

7 files changed

+103
-69
lines changed

src/ci/docker/host-x86_64/mingw-check-1/Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ RUN pip3 install --no-deps --no-cache-dir --require-hashes -r /tmp/reuse-require
3939

4040
COPY host-x86_64/mingw-check-1/check-default-config-profiles.sh /scripts/
4141
COPY host-x86_64/mingw-check-1/validate-toolstate.sh /scripts/
42-
COPY host-x86_64/mingw-check-1/validate-error-codes.sh /scripts/
4342

4443
# Check library crates on all tier 1 targets.
4544
# We disable optimized compiler built-ins because that requires a C toolchain for the target.
@@ -52,7 +51,6 @@ ENV SCRIPT \
5251
python3 ../x.py check --stage 1 --target=i686-pc-windows-gnu --host=i686-pc-windows-gnu && \
5352
python3 ../x.py check --stage 1 --set build.optimized-compiler-builtins=false core alloc std --target=aarch64-unknown-linux-gnu,i686-pc-windows-msvc,i686-unknown-linux-gnu,x86_64-apple-darwin,x86_64-pc-windows-gnu,x86_64-pc-windows-msvc && \
5453
/scripts/validate-toolstate.sh && \
55-
/scripts/validate-error-codes.sh && \
5654
reuse --include-submodules lint && \
5755
python3 ../x.py test collect-license-metadata && \
5856
# Runs checks to ensure that there are no issues in our JS code.

src/ci/docker/host-x86_64/mingw-check-1/validate-error-codes.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ RUN pip3 install --no-deps --no-cache-dir --require-hashes -r /tmp/reuse-require
3939
&& pip3 install virtualenv
4040

4141
COPY host-x86_64/mingw-check-1/validate-toolstate.sh /scripts/
42-
COPY host-x86_64/mingw-check-1/validate-error-codes.sh /scripts/
4342

4443
RUN bash -c 'npm install -g eslint@$(cat /tmp/eslint.version)'
4544

src/tools/tidy/src/error_codes.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,18 @@ macro_rules! verbose_print {
4343
};
4444
}
4545

46-
pub fn check(root_path: &Path, search_paths: &[&Path], verbose: bool, bad: &mut bool) {
46+
pub fn check(
47+
root_path: &Path,
48+
search_paths: &[&Path],
49+
verbose: bool,
50+
ci_info: &crate::CiInfo,
51+
bad: &mut bool,
52+
) {
4753
let mut errors = Vec::new();
4854

55+
// Check that no error code explanation was removed.
56+
check_removed_error_code_explanation(ci_info, bad);
57+
4958
// Stage 1: create list
5059
let error_codes = extract_error_codes(root_path, &mut errors);
5160
if verbose {
@@ -68,6 +77,27 @@ pub fn check(root_path: &Path, search_paths: &[&Path], verbose: bool, bad: &mut
6877
}
6978
}
7079

80+
fn 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+
71101
/// Stage 1: Parses a list of error codes from `error_codes.rs`.
72102
fn extract_error_codes(root_path: &Path, errors: &mut Vec<String>) -> Vec<String> {
73103
let path = root_path.join(Path::new(ERROR_CODES_PATH));

src/tools/tidy/src/lib.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
//! This library contains the tidy lints and exposes it
44
//! to be used by tools.
55
6+
use std::ffi::OsStr;
7+
use std::process::Command;
8+
9+
use build_helper::ci::CiEnv;
10+
use build_helper::git::{GitConfig, get_closest_upstream_commit};
11+
use build_helper::stage0_parser::{Stage0Config, parse_stage0_file};
612
use termcolor::WriteColor;
713

814
macro_rules! static_regex {
@@ -63,6 +69,61 @@ fn tidy_error(args: &str) -> std::io::Result<()> {
6369
Ok(())
6470
}
6571

72+
pub struct CiInfo {
73+
pub git_merge_commit_email: String,
74+
pub nightly_branch: String,
75+
pub base_commit: Option<String>,
76+
pub ci_env: CiEnv,
77+
}
78+
79+
impl CiInfo {
80+
pub fn new(bad: &mut bool) -> Self {
81+
let stage0 = parse_stage0_file();
82+
let Stage0Config { nightly_branch, git_merge_commit_email, .. } = stage0.config;
83+
84+
let mut info = Self {
85+
nightly_branch,
86+
git_merge_commit_email,
87+
ci_env: CiEnv::current(),
88+
base_commit: None,
89+
};
90+
let base_commit = match get_closest_upstream_commit(None, &info.git_config(), info.ci_env) {
91+
Ok(Some(commit)) => Some(commit),
92+
Ok(None) => {
93+
info.error_if_in_ci("no base commit found", bad);
94+
None
95+
}
96+
Err(error) => {
97+
info.error_if_in_ci(&format!("failed to retrieve base commit: {error}"), bad);
98+
None
99+
}
100+
};
101+
info.base_commit = base_commit;
102+
info
103+
}
104+
105+
pub fn git_config(&self) -> GitConfig<'_> {
106+
GitConfig {
107+
nightly_branch: &self.nightly_branch,
108+
git_merge_commit_email: &self.git_merge_commit_email,
109+
}
110+
}
111+
112+
pub fn error_if_in_ci(&self, msg: &str, bad: &mut bool) {
113+
if self.ci_env.is_running_in_ci() {
114+
*bad = true;
115+
eprintln!("tidy check error: {msg}");
116+
} else {
117+
eprintln!("tidy check warning: {msg}. Some checks will be skipped.");
118+
}
119+
}
120+
}
121+
122+
pub fn git_diff<S: AsRef<OsStr>>(base_commit: &str, extra_arg: S) -> Option<String> {
123+
let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?;
124+
Some(String::from_utf8_lossy(&output.stdout).into())
125+
}
126+
66127
pub mod alphabetical;
67128
pub mod bins;
68129
pub mod debug_artifacts;

src/tools/tidy/src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ fn main() {
4848
let extra_checks =
4949
cfg_args.iter().find(|s| s.starts_with("--extra-checks=")).map(String::as_str);
5050

51-
let bad = std::sync::Arc::new(AtomicBool::new(false));
51+
let mut bad = false;
52+
let ci_info = CiInfo::new(&mut bad);
53+
let bad = std::sync::Arc::new(AtomicBool::new(bad));
5254

5355
let drain_handles = |handles: &mut VecDeque<ScopedJoinHandle<'_, ()>>| {
5456
// poll all threads for completion before awaiting the oldest one
@@ -110,12 +112,12 @@ fn main() {
110112
check!(rustdoc_css_themes, &librustdoc_path);
111113
check!(rustdoc_templates, &librustdoc_path);
112114
check!(rustdoc_js, &librustdoc_path, &tools_path, &src_path);
113-
check!(rustdoc_json, &src_path);
115+
check!(rustdoc_json, &src_path, &ci_info);
114116
check!(known_bug, &crashes_path);
115117
check!(unknown_revision, &tests_path);
116118

117119
// Checks that only make sense for the compiler.
118-
check!(error_codes, &root_path, &[&compiler_path, &librustdoc_path], verbose);
120+
check!(error_codes, &root_path, &[&compiler_path, &librustdoc_path], verbose, &ci_info);
119121
check!(fluent_alphabetical, &compiler_path, bless);
120122
check!(fluent_period, &compiler_path);
121123
check!(target_policy, &root_path);

src/tools/tidy/src/rustdoc_json.rs

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,20 @@
11
//! Tidy check to ensure that `FORMAT_VERSION` was correctly updated if `rustdoc-json-types` was
22
//! updated as well.
33
4-
use std::ffi::OsStr;
54
use std::path::Path;
6-
use std::process::Command;
75
use std::str::FromStr;
86

9-
use build_helper::ci::CiEnv;
10-
use build_helper::git::{GitConfig, get_closest_upstream_commit};
11-
use build_helper::stage0_parser::parse_stage0_file;
12-
137
const RUSTDOC_JSON_TYPES: &str = "src/rustdoc-json-types";
148

15-
fn git_diff<S: AsRef<OsStr>>(base_commit: &str, extra_arg: S) -> Option<String> {
16-
let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?;
17-
Some(String::from_utf8_lossy(&output.stdout).into())
18-
}
19-
20-
fn error_if_in_ci(ci_env: CiEnv, msg: &str, bad: &mut bool) {
21-
if ci_env.is_running_in_ci() {
22-
*bad = true;
23-
eprintln!("error in `rustdoc_json` tidy check: {msg}");
24-
} else {
25-
eprintln!("{msg}. Skipping `rustdoc_json` tidy check");
26-
}
27-
}
28-
29-
pub fn check(src_path: &Path, bad: &mut bool) {
9+
pub fn check(src_path: &Path, ci_info: &crate::CiInfo, bad: &mut bool) {
3010
println!("Checking tidy rustdoc_json...");
31-
let stage0 = parse_stage0_file();
32-
let ci_env = CiEnv::current();
33-
let base_commit = match get_closest_upstream_commit(
34-
None,
35-
&GitConfig {
36-
nightly_branch: &stage0.config.nightly_branch,
37-
git_merge_commit_email: &stage0.config.git_merge_commit_email,
38-
},
39-
ci_env,
40-
) {
41-
Ok(Some(commit)) => commit,
42-
Ok(None) => {
43-
error_if_in_ci(ci_env, "no base commit found", bad);
44-
return;
45-
}
46-
Err(error) => {
47-
error_if_in_ci(ci_env, &format!("failed to retrieve base commit: {error}"), bad);
48-
return;
49-
}
11+
let Some(base_commit) = &ci_info.base_commit else {
12+
eprintln!("No base commit, skipping rustdoc_json check");
13+
return;
5014
};
5115

5216
// First we check that `src/rustdoc-json-types` was modified.
53-
match git_diff(&base_commit, "--name-status") {
17+
match crate::git_diff(&base_commit, "--name-status") {
5418
Some(output) => {
5519
if !output
5620
.lines()
@@ -68,7 +32,7 @@ pub fn check(src_path: &Path, bad: &mut bool) {
6832
}
6933
}
7034
// Then we check that if `FORMAT_VERSION` was updated, the `Latest feature:` was also updated.
71-
match git_diff(&base_commit, src_path.join("rustdoc-json-types")) {
35+
match crate::git_diff(&base_commit, src_path.join("rustdoc-json-types")) {
7236
Some(output) => {
7337
let mut format_version_updated = false;
7438
let mut latest_feature_comment_updated = false;

0 commit comments

Comments
 (0)