From 8267084659bb524ac573957dcf6e1cb4d01b06bd Mon Sep 17 00:00:00 2001 From: grandizzy Date: Thu, 16 Jan 2025 10:08:15 +0200 Subject: [PATCH] fix(config): normalize optimizer settings --- crates/config/src/lib.rs | 37 +++++++++++++++++++++++++--------- crates/forge/bin/cmd/config.rs | 1 + 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index c7762fe6a5bfd..44c36fffd5c38 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -669,15 +669,8 @@ impl Config { add_profile(&Self::DEFAULT_PROFILE); add_profile(&config.profile); - // Ref: https://github.com/foundry-rs/foundry/issues/9665 - // Enables the optimizer if the `optimizer_runs` has been set. - let optimizer = config.optimizer(); - if optimizer.runs.is_some_and(|runs| runs > 0) && optimizer.enabled.is_none() { - config.optimizer = Some(true); - } else if optimizer.runs.is_none() && optimizer.enabled.is_some_and(|enabled| enabled) { - // Default optimizer runs set to 200 if `optimizer = true`. - config.optimizer_runs = Some(200); - }; + config.normalize_optimizer_settings(); + Ok(config) } @@ -843,11 +836,37 @@ impl Config { self } + /// Normalizes optimizer settings. + /// See + pub fn normalized_optimizer_settings(mut self) -> Self { + self.normalize_optimizer_settings(); + self + } + /// Normalizes the evm version if a [SolcReq] is set to a valid version. pub fn normalize_evm_version(&mut self) { self.evm_version = self.get_normalized_evm_version(); } + /// Normalizes optimizer settings: + /// - with default settings, optimizer is set to false and optimizer runs to 200 + /// - if optimizer is set and optimizer runs not specified, then optimizer runs is set to 200 + /// - enable optimizer if not explicitly set and optimizer runs set to a value greater than 0 + pub fn normalize_optimizer_settings(&mut self) { + match (self.optimizer, self.optimizer_runs) { + // Default: set the optimizer to false and optimizer runs to 200. + (None, None) => { + self.optimizer = Some(false); + self.optimizer_runs = Some(200); + } + // Set the optimizer runs to 200 if the `optimizer` config set. + (Some(_), None) => self.optimizer_runs = Some(200), + // Enables optimizer if the `optimizer_runs` has been set with a value greater than 0. + (None, Some(runs)) => self.optimizer = Some(runs > 0), + _ => {} + } + } + /// Returns the normalized [EvmVersion] if a [SolcReq] is set to a valid version or if the solc /// path is a valid solc binary. /// diff --git a/crates/forge/bin/cmd/config.rs b/crates/forge/bin/cmd/config.rs index 42ab29ec26555..d1de49d5255b9 100644 --- a/crates/forge/bin/cmd/config.rs +++ b/crates/forge/bin/cmd/config.rs @@ -37,6 +37,7 @@ impl ConfigArgs { let config = self .try_load_config_unsanitized_emit_warnings()? + .normalized_optimizer_settings() // we explicitly normalize the version, so mimic the behavior when invoking solc .normalized_evm_version();