Skip to content

Commit a46589f

Browse files
authored
Rollup merge of rust-lang#113273 - hi-rustin:rustin-patch-opt-level, r=Kobzol
Use String or Int to set the opt level Address https://github.com/rust-lang/rust/pull/112756/files#r1249345725 Use String or Int to set the opt level. r? ``@jyn514``
2 parents 6a20f68 + 92b5d0c commit a46589f

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

config.example.toml

+12-2
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,20 @@ changelog-seen = 2
400400
# =============================================================================
401401
[rust]
402402

403-
# Whether or not to optimize the compiler and standard library.
403+
# Whether or not to optimize when compiling the compiler and standard library,
404+
# and what level of optimization to use.
404405
# WARNING: Building with optimize = false is NOT SUPPORTED. Due to bootstrapping,
405406
# building without optimizations takes much longer than optimizing. Further, some platforms
406407
# fail to build without this optimization (c.f. #65352).
408+
# The valid options are:
409+
# true - Enable optimizations.
410+
# false - Disable optimizations.
411+
# 0 - Disable optimizations.
412+
# 1 - Basic optimizations.
413+
# 2 - Some optimizations.
414+
# 3 - All optimizations.
415+
# "s" - Optimize for binary size.
416+
# "z" - Optimize for binary size, but also turn off loop vectorization.
407417
#optimize = true
408418

409419
# Indicates that the build should be configured for debugging Rust. A
@@ -757,7 +767,7 @@ changelog-seen = 2
757767
# This option will override the same option under [build] section.
758768
#profiler = build.profiler (bool)
759769

760-
# This option supports enable `rpath` in each target independently,
770+
# This option supports enable `rpath` in each target independently,
761771
# and will override the same option under [rust] section. It only works on Unix platforms
762772
#rpath = rust.rpath (bool)
763773

src/bootstrap/config.rs

+60-13
Original file line numberDiff line numberDiff line change
@@ -875,11 +875,10 @@ impl Default for StringOrBool {
875875
}
876876
}
877877

878-
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
879-
#[serde(untagged)]
878+
#[derive(Clone, Debug, PartialEq, Eq)]
880879
pub enum RustOptimize {
881-
#[serde(deserialize_with = "deserialize_and_validate_opt_level")]
882880
String(String),
881+
Int(u8),
883882
Bool(bool),
884883
}
885884

@@ -889,26 +888,74 @@ impl Default for RustOptimize {
889888
}
890889
}
891890

892-
fn deserialize_and_validate_opt_level<'de, D>(d: D) -> Result<String, D::Error>
893-
where
894-
D: serde::de::Deserializer<'de>,
895-
{
896-
let v = String::deserialize(d)?;
897-
if ["0", "1", "2", "3", "s", "z"].iter().find(|x| **x == v).is_some() {
898-
Ok(v)
899-
} else {
900-
Err(format!(r#"unrecognized option for rust optimize: "{}", expected one of "0", "1", "2", "3", "s", "z""#, v)).map_err(serde::de::Error::custom)
891+
impl<'de> Deserialize<'de> for RustOptimize {
892+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
893+
where
894+
D: Deserializer<'de>,
895+
{
896+
deserializer.deserialize_any(OptimizeVisitor)
897+
}
898+
}
899+
900+
struct OptimizeVisitor;
901+
902+
impl<'de> serde::de::Visitor<'de> for OptimizeVisitor {
903+
type Value = RustOptimize;
904+
905+
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
906+
formatter.write_str(r#"one of: 0, 1, 2, 3, "s", "z", true, false"#)
907+
}
908+
909+
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
910+
where
911+
E: serde::de::Error,
912+
{
913+
if ["s", "z"].iter().find(|x| **x == value).is_some() {
914+
Ok(RustOptimize::String(value.to_string()))
915+
} else {
916+
Err(format_optimize_error_msg(value)).map_err(serde::de::Error::custom)
917+
}
918+
}
919+
920+
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
921+
where
922+
E: serde::de::Error,
923+
{
924+
if matches!(value, 0..=3) {
925+
Ok(RustOptimize::Int(value as u8))
926+
} else {
927+
Err(format_optimize_error_msg(value)).map_err(serde::de::Error::custom)
928+
}
929+
}
930+
931+
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
932+
where
933+
E: serde::de::Error,
934+
{
935+
Ok(RustOptimize::Bool(value))
901936
}
902937
}
903938

939+
fn format_optimize_error_msg(v: impl std::fmt::Display) -> String {
940+
format!(
941+
r#"unrecognized option for rust optimize: "{}", expected one of 0, 1, 2, 3, "s", "z", true, false"#,
942+
v
943+
)
944+
}
945+
904946
impl RustOptimize {
905947
pub(crate) fn is_release(&self) -> bool {
906-
if let RustOptimize::Bool(true) | RustOptimize::String(_) = &self { true } else { false }
948+
match &self {
949+
RustOptimize::Bool(true) | RustOptimize::String(_) => true,
950+
RustOptimize::Int(i) => *i > 0,
951+
RustOptimize::Bool(false) => false,
952+
}
907953
}
908954

909955
pub(crate) fn get_opt_level(&self) -> Option<String> {
910956
match &self {
911957
RustOptimize::String(s) => Some(s.clone()),
958+
RustOptimize::Int(i) => Some(i.to_string()),
912959
RustOptimize::Bool(_) => None,
913960
}
914961
}

src/bootstrap/config/tests.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ fn rust_optimize() {
184184
assert_eq!(parse("").rust_optimize.is_release(), true);
185185
assert_eq!(parse("rust.optimize = false").rust_optimize.is_release(), false);
186186
assert_eq!(parse("rust.optimize = true").rust_optimize.is_release(), true);
187-
assert_eq!(parse("rust.optimize = \"1\"").rust_optimize.get_opt_level(), Some("1".to_string()));
187+
assert_eq!(parse("rust.optimize = 0").rust_optimize.is_release(), false);
188+
assert_eq!(parse("rust.optimize = 1").rust_optimize.is_release(), true);
189+
assert_eq!(parse("rust.optimize = 1").rust_optimize.get_opt_level(), Some("1".to_string()));
190+
assert_eq!(parse("rust.optimize = \"s\"").rust_optimize.is_release(), true);
188191
assert_eq!(parse("rust.optimize = \"s\"").rust_optimize.get_opt_level(), Some("s".to_string()));
189192
}
190193

0 commit comments

Comments
 (0)