Skip to content

Commit

Permalink
fix(config): normalize optimizer settings
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Jan 16, 2025
1 parent 41c6653 commit 8267084
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
37 changes: 28 additions & 9 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -843,11 +836,37 @@ impl Config {
self
}

/// Normalizes optimizer settings.
/// See <https://github.com/foundry-rs/foundry/issues/9665>
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.
///
Expand Down
1 change: 1 addition & 0 deletions crates/forge/bin/cmd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 8267084

Please sign in to comment.