Skip to content

Commit 175a537

Browse files
committed
feat(config): Add ChangeId enum for suppressing warnings
Introduces the `ChangeId` enum to allow suppressing `change_id` warnings. Now, `ChangeId` supports both numeric values and the string literal `"ignore"`. Numeric values behave as expected, while `"ignore"` is used to suppress warning messages.
1 parent 7d49ae9 commit 175a537

File tree

6 files changed

+85
-17
lines changed

6 files changed

+85
-17
lines changed

src/bootstrap/src/bin/main.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use std::str::FromStr;
1111
use std::{env, process};
1212

1313
use bootstrap::{
14-
Build, CONFIG_CHANGE_HISTORY, Config, Flags, Subcommand, debug, find_recent_config_change_ids,
15-
human_readable_changes, t,
14+
Build, CONFIG_CHANGE_HISTORY, ChangeId, Config, Flags, Subcommand, debug,
15+
find_recent_config_change_ids, human_readable_changes, t,
1616
};
1717
#[cfg(feature = "tracing")]
1818
use tracing::instrument;
@@ -31,7 +31,7 @@ fn main() {
3131
debug!("parsing flags");
3232
let flags = Flags::parse(&args);
3333
debug!("parsing config based on flags");
34-
let config = Config::parse(flags);
34+
let mut config = Config::parse(flags);
3535

3636
let mut build_lock;
3737
let _build_lock_guard;
@@ -75,7 +75,7 @@ fn main() {
7575
{
7676
None
7777
} else {
78-
check_version(&config)
78+
check_version(&mut config)
7979
};
8080

8181
// NOTE: Since `./configure` generates a `bootstrap.toml`, distro maintainers will see the
@@ -149,14 +149,15 @@ fn main() {
149149
}
150150
}
151151

152-
fn check_version(config: &Config) -> Option<String> {
152+
fn check_version(config: &mut Config) -> Option<String> {
153153
let mut msg = String::new();
154154

155155
let latest_change_id = CONFIG_CHANGE_HISTORY.last().unwrap().change_id;
156156
let warned_id_path = config.out.join("bootstrap").join(".last-warned-change-id");
157157

158-
if let Some(mut id) = config.change_id {
159-
if id == latest_change_id {
158+
if let Some(ref mut id) = config.change_id {
159+
if matches!(id, ChangeId::Id(x) if *x == latest_change_id) || matches!(id, ChangeId::Ignore)
160+
{
160161
return None;
161162
}
162163

@@ -171,11 +172,11 @@ fn check_version(config: &Config) -> Option<String> {
171172
// Otherwise, we may retrieve all the changes if it's not the highest value.
172173
// For better understanding, refer to `change_tracker::find_recent_config_change_ids`.
173174
if CONFIG_CHANGE_HISTORY.iter().any(|config| config.change_id == last_warned_id) {
174-
id = last_warned_id;
175+
*id = ChangeId::Id(last_warned_id);
175176
}
176177
};
177178

178-
let changes = find_recent_config_change_ids(id);
179+
let changes = find_recent_config_change_ids(id.clone());
179180

180181
if changes.is_empty() {
181182
return None;

src/bootstrap/src/core/config/config.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::{cmp, env, fs};
1616
use build_helper::ci::CiEnv;
1717
use build_helper::exit;
1818
use build_helper::git::{GitConfig, get_closest_merge_commit, output_result};
19+
use serde::de::Visitor;
1920
use serde::{Deserialize, Deserializer};
2021
use serde_derive::Deserialize;
2122
#[cfg(feature = "tracing")]
@@ -183,6 +184,62 @@ pub enum GccCiMode {
183184
DownloadFromCi,
184185
}
185186

187+
/// This enum is used for deserializing change IDs from TOML, allowing both numeric values and the string `"ignore"`.
188+
#[derive(Clone, Debug, PartialEq)]
189+
pub enum ChangeId {
190+
Ignore,
191+
Id(usize),
192+
}
193+
194+
impl<'de> Deserialize<'de> for ChangeId {
195+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
196+
where
197+
D: Deserializer<'de>,
198+
{
199+
struct ChangeIdVisitor;
200+
201+
impl Visitor<'_> for ChangeIdVisitor {
202+
type Value = ChangeId;
203+
204+
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
205+
formatter.write_str("a usize number or the string \"ignore\"")
206+
}
207+
208+
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
209+
where
210+
E: serde::de::Error,
211+
{
212+
if value >= 0 {
213+
Ok(ChangeId::Id(value as usize))
214+
} else {
215+
Err(E::custom(format!(
216+
"invalid negative number `{}`, expected a non-negative usize or \"ignore\"",
217+
value
218+
)))
219+
}
220+
}
221+
222+
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
223+
where
224+
E: serde::de::Error,
225+
{
226+
let value = value.trim();
227+
if value.eq_ignore_ascii_case("ignore") {
228+
Ok(ChangeId::Ignore)
229+
} else if let Ok(num) = value.parse::<usize>() {
230+
Ok(ChangeId::Id(num))
231+
} else {
232+
Err(E::custom(format!(
233+
"invalid value `{}`, expected a usize number or \"ignore\"",
234+
value
235+
)))
236+
}
237+
}
238+
}
239+
deserializer.deserialize_any(ChangeIdVisitor)
240+
}
241+
}
242+
186243
/// Global configuration for the entire build and/or bootstrap.
187244
///
188245
/// This structure is parsed from `bootstrap.toml`, and some of the fields are inferred from `git` or build-time parameters.
@@ -193,7 +250,7 @@ pub enum GccCiMode {
193250
/// `bootstrap.example.toml`.
194251
#[derive(Default, Clone)]
195252
pub struct Config {
196-
pub change_id: Option<usize>,
253+
pub change_id: Option<ChangeId>,
197254
pub bypass_bootstrap_lock: bool,
198255
pub ccache: Option<String>,
199256
/// Call Build::ninja() instead of this.
@@ -707,7 +764,7 @@ pub(crate) struct TomlConfig {
707764
#[derive(Deserialize, Default)]
708765
pub(crate) struct ChangeIdWrapper {
709766
#[serde(alias = "change-id")]
710-
pub(crate) inner: Option<usize>,
767+
pub(crate) inner: Option<ChangeId>,
711768
}
712769

713770
/// Describes how to handle conflicts in merging two [`TomlConfig`]

src/bootstrap/src/core/config/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use serde::Deserialize;
1010

1111
use super::flags::Flags;
1212
use super::{ChangeIdWrapper, Config, RUSTC_IF_UNCHANGED_ALLOWED_PATHS};
13+
use crate::ChangeId;
1314
use crate::core::build_steps::clippy::{LintConfig, get_clippy_rules_in_order};
1415
use crate::core::build_steps::llvm;
1516
use crate::core::build_steps::llvm::LLVM_INVALIDATION_PATHS;
@@ -171,7 +172,7 @@ runner = "x86_64-runner"
171172
)
172173
},
173174
);
174-
assert_eq!(config.change_id, Some(1), "setting top-level value");
175+
assert_eq!(config.change_id, Some(ChangeId::Id(1)), "setting top-level value");
175176
assert_eq!(
176177
config.rust_lto,
177178
crate::core::config::RustcLto::Fat,
@@ -311,7 +312,7 @@ fn parse_change_id_with_unknown_field() {
311312
"#;
312313

313314
let change_id_wrapper: ChangeIdWrapper = toml::from_str(config).unwrap();
314-
assert_eq!(change_id_wrapper.inner, Some(3461));
315+
assert_eq!(change_id_wrapper.inner, Some(ChangeId::Id(3461)));
315316
}
316317

317318
#[test]

src/bootstrap/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ mod core;
4545
mod utils;
4646

4747
pub use core::builder::PathSet;
48-
pub use core::config::Config;
4948
pub use core::config::flags::{Flags, Subcommand};
49+
pub use core::config::{ChangeId, Config};
5050

5151
#[cfg(feature = "tracing")]
5252
use tracing::{instrument, span};

src/bootstrap/src/utils/change_tracker.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
use std::fmt::Display;
66

7+
use crate::core::config::ChangeId;
8+
79
#[cfg(test)]
810
mod tests;
911

@@ -35,7 +37,11 @@ impl Display for ChangeSeverity {
3537
}
3638
}
3739

38-
pub fn find_recent_config_change_ids(current_id: usize) -> Vec<ChangeInfo> {
40+
pub fn find_recent_config_change_ids(current_id: ChangeId) -> Vec<ChangeInfo> {
41+
let current_id = match current_id {
42+
ChangeId::Id(x) => x,
43+
ChangeId::Ignore => return Vec::new(),
44+
};
3945
if !CONFIG_CHANGE_HISTORY.iter().any(|config| config.change_id == current_id) {
4046
// If the current change-id is greater than the most recent one, return
4147
// an empty list (it may be due to switching from a recent branch to an

src/bootstrap/src/utils/change_tracker/tests.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ use crate::{CONFIG_CHANGE_HISTORY, find_recent_config_change_ids};
33
#[test]
44
fn test_find_recent_config_change_ids() {
55
// If change-id is greater than the most recent one, result should be empty.
6-
assert!(find_recent_config_change_ids(usize::MAX).is_empty());
6+
assert!(find_recent_config_change_ids(crate::ChangeId::Id(usize::MAX)).is_empty());
77

88
// There is no change-id equal to or less than 0, result should include the entire change history.
9-
assert_eq!(find_recent_config_change_ids(0).len(), CONFIG_CHANGE_HISTORY.len());
9+
assert_eq!(
10+
find_recent_config_change_ids(crate::ChangeId::Id(0)).len(),
11+
CONFIG_CHANGE_HISTORY.len()
12+
);
1013
}

0 commit comments

Comments
 (0)