Skip to content

Commit 1ba55ff

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 1ba55ff

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

src/bootstrap/src/bin/main.rs

+6-5
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;
@@ -155,8 +155,9 @@ fn check_version(config: &Config) -> Option<String> {
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(mut id) = config.change_id.clone() {
159+
if matches!(id, ChangeId::Id(x) if x == latest_change_id) || matches!(id, ChangeId::Ignore)
160+
{
160161
return None;
161162
}
162163

@@ -171,7 +172,7 @@ 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

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

+21-3
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub enum GccCiMode {
193193
/// `bootstrap.example.toml`.
194194
#[derive(Default, Clone)]
195195
pub struct Config {
196-
pub change_id: Option<usize>,
196+
pub change_id: Option<ChangeId>,
197197
pub bypass_bootstrap_lock: bool,
198198
pub ccache: Option<String>,
199199
/// Call Build::ninja() instead of this.
@@ -700,14 +700,32 @@ pub(crate) struct TomlConfig {
700700
profile: Option<String>,
701701
}
702702

703+
/// This enum is used for deserializing change IDs from TOML, allowing both numeric values and the string `"ignore"`.
704+
#[derive(Clone, Debug, PartialEq)]
705+
pub enum ChangeId {
706+
Ignore,
707+
Id(usize),
708+
}
709+
703710
/// Since we use `#[serde(deny_unknown_fields)]` on `TomlConfig`, we need a wrapper type
704711
/// for the "change-id" field to parse it even if other fields are invalid. This ensures
705712
/// that if deserialization fails due to other fields, we can still provide the changelogs
706713
/// to allow developers to potentially find the reason for the failure in the logs..
707714
#[derive(Deserialize, Default)]
708715
pub(crate) struct ChangeIdWrapper {
709-
#[serde(alias = "change-id")]
710-
pub(crate) inner: Option<usize>,
716+
#[serde(alias = "change-id", default, deserialize_with = "deserialize_change_id")]
717+
pub(crate) inner: Option<ChangeId>,
718+
}
719+
720+
fn deserialize_change_id<'de, D: Deserializer<'de>>(
721+
deserializer: D,
722+
) -> Result<Option<ChangeId>, D::Error> {
723+
let value = toml::Value::deserialize(deserializer)?;
724+
Ok(match value {
725+
toml::Value::String(s) if s == "ignore" => Some(ChangeId::Ignore),
726+
toml::Value::Integer(i) => Some(ChangeId::Id(i as usize)),
727+
_ => return Err(serde::de::Error::custom("expected \"ignore\" or an integer")),
728+
})
711729
}
712730

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

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

+3-2
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

+1-1
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

+7-1
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

+5-2
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)