Skip to content

Commit 9829054

Browse files
authored
fix(config): normalize optimizer settings (#9689)
1 parent 41c6653 commit 9829054

File tree

3 files changed

+442
-16
lines changed

3 files changed

+442
-16
lines changed

crates/config/src/lib.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -669,15 +669,8 @@ impl Config {
669669
add_profile(&Self::DEFAULT_PROFILE);
670670
add_profile(&config.profile);
671671

672-
// Ref: https://github.com/foundry-rs/foundry/issues/9665
673-
// Enables the optimizer if the `optimizer_runs` has been set.
674-
let optimizer = config.optimizer();
675-
if optimizer.runs.is_some_and(|runs| runs > 0) && optimizer.enabled.is_none() {
676-
config.optimizer = Some(true);
677-
} else if optimizer.runs.is_none() && optimizer.enabled.is_some_and(|enabled| enabled) {
678-
// Default optimizer runs set to 200 if `optimizer = true`.
679-
config.optimizer_runs = Some(200);
680-
};
672+
config.normalize_optimizer_settings();
673+
681674
Ok(config)
682675
}
683676

@@ -843,11 +836,37 @@ impl Config {
843836
self
844837
}
845838

839+
/// Normalizes optimizer settings.
840+
/// See <https://github.com/foundry-rs/foundry/issues/9665>
841+
pub fn normalized_optimizer_settings(mut self) -> Self {
842+
self.normalize_optimizer_settings();
843+
self
844+
}
845+
846846
/// Normalizes the evm version if a [SolcReq] is set to a valid version.
847847
pub fn normalize_evm_version(&mut self) {
848848
self.evm_version = self.get_normalized_evm_version();
849849
}
850850

851+
/// Normalizes optimizer settings:
852+
/// - with default settings, optimizer is set to false and optimizer runs to 200
853+
/// - if optimizer is set and optimizer runs not specified, then optimizer runs is set to 200
854+
/// - enable optimizer if not explicitly set and optimizer runs set to a value greater than 0
855+
pub fn normalize_optimizer_settings(&mut self) {
856+
match (self.optimizer, self.optimizer_runs) {
857+
// Default: set the optimizer to false and optimizer runs to 200.
858+
(None, None) => {
859+
self.optimizer = Some(false);
860+
self.optimizer_runs = Some(200);
861+
}
862+
// Set the optimizer runs to 200 if the `optimizer` config set.
863+
(Some(_), None) => self.optimizer_runs = Some(200),
864+
// Enables optimizer if the `optimizer_runs` has been set with a value greater than 0.
865+
(None, Some(runs)) => self.optimizer = Some(runs > 0),
866+
_ => {}
867+
}
868+
}
869+
851870
/// Returns the normalized [EvmVersion] if a [SolcReq] is set to a valid version or if the solc
852871
/// path is a valid solc binary.
853872
///
@@ -2670,7 +2689,7 @@ mod tests {
26702689
let roundtrip = Figment::from(Config::from_provider(&original));
26712690
for figment in &[original, roundtrip] {
26722691
let config = Config::from_provider(figment);
2673-
assert_eq!(config, Config::default());
2692+
assert_eq!(config, Config::default().normalized_optimizer_settings());
26742693
}
26752694
Ok(())
26762695
});
@@ -2942,7 +2961,13 @@ mod tests {
29422961
)?;
29432962

29442963
let config = Config::load();
2945-
assert_eq!(config, Config { gas_limit: gas.into(), ..Config::default() });
2964+
assert_eq!(
2965+
config,
2966+
Config {
2967+
gas_limit: gas.into(),
2968+
..Config::default().normalized_optimizer_settings()
2969+
}
2970+
);
29462971

29472972
Ok(())
29482973
});
@@ -3590,7 +3615,7 @@ mod tests {
35903615
]),
35913616
build_info_path: Some("build-info".into()),
35923617
always_use_create_2_factory: true,
3593-
..Config::default()
3618+
..Config::default().normalized_optimizer_settings()
35943619
}
35953620
);
35963621

@@ -3825,7 +3850,7 @@ mod tests {
38253850
eth_rpc_url: Some("https://example.com/".to_string()),
38263851
auto_detect_solc: false,
38273852
evm_version: EvmVersion::Berlin,
3828-
..Config::default()
3853+
..Config::default().normalized_optimizer_settings()
38293854
}
38303855
);
38313856

@@ -3877,7 +3902,7 @@ mod tests {
38773902
src: "mysrc".into(),
38783903
out: "myout".into(),
38793904
verbosity: 3,
3880-
..Config::default()
3905+
..Config::default().normalized_optimizer_settings()
38813906
}
38823907
);
38833908

@@ -3889,7 +3914,7 @@ mod tests {
38893914
src: "other-src".into(),
38903915
out: "myout".into(),
38913916
verbosity: 3,
3892-
..Config::default()
3917+
..Config::default().normalized_optimizer_settings()
38933918
}
38943919
);
38953920

@@ -4192,7 +4217,7 @@ mod tests {
41924217
#[test]
41934218
fn config_roundtrip() {
41944219
figment::Jail::expect_with(|jail| {
4195-
let default = Config::default();
4220+
let default = Config::default().normalized_optimizer_settings();
41964221
let basic = default.clone().into_basic();
41974222
jail.create_file("foundry.toml", &basic.to_string_pretty().unwrap())?;
41984223

crates/forge/bin/cmd/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl ConfigArgs {
3737

3838
let config = self
3939
.try_load_config_unsanitized_emit_warnings()?
40+
.normalized_optimizer_settings()
4041
// we explicitly normalize the version, so mimic the behavior when invoking solc
4142
.normalized_evm_version();
4243

0 commit comments

Comments
 (0)