Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat(solc): add Settings sanitize #2329

Merged
merged 1 commit into from
Apr 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 76 additions & 60 deletions ethers-solc/src/artifacts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,67 +103,13 @@ impl CompilerInput {

/// This will remove/adjust values in the `CompilerInput` that are not compatible with this
/// version
pub fn sanitized(mut self, version: &Version) -> Self {
static PRE_V0_6_0: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.6.0").unwrap());
static PRE_V0_7_5: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.7.5").unwrap());
static PRE_V0_8_7: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.8.7").unwrap());
static PRE_V0_8_10: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.8.10").unwrap());
static PRE_V0_8_18: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.8.18").unwrap());

if PRE_V0_6_0.matches(version) {
if let Some(ref mut meta) = self.settings.metadata {
// introduced in <https://docs.soliditylang.org/en/v0.6.0/using-the-compiler.html#compiler-api>
// missing in <https://docs.soliditylang.org/en/v0.5.17/using-the-compiler.html#compiler-api>
meta.bytecode_hash.take();
}
// introduced in <https://docs.soliditylang.org/en/v0.6.0/using-the-compiler.html#compiler-api>
let _ = self.settings.debug.take();
}

if PRE_V0_7_5.matches(version) {
// introduced in 0.7.5 <https://github.com/ethereum/solidity/releases/tag/v0.7.5>
self.settings.via_ir.take();
}

if PRE_V0_8_7.matches(version) {
// lower the disable version from 0.8.10 to 0.8.7, due to `divModNoSlacks`,
// `showUnproved` and `solvers` are implemented
// introduced in <https://github.com/ethereum/solidity/releases/tag/v0.8.7>
self.settings.model_checker = None;
}

if PRE_V0_8_10.matches(version) {
if let Some(ref mut debug) = self.settings.debug {
// introduced in <https://docs.soliditylang.org/en/v0.8.10/using-the-compiler.html#compiler-api>
// <https://github.com/ethereum/solidity/releases/tag/v0.8.10>
debug.debug_info.clear();
}

if let Some(ref mut model_checker) = self.settings.model_checker {
// introduced in <https://github.com/ethereum/solidity/releases/tag/v0.8.10>
model_checker.invariants = None;
}
}

if PRE_V0_8_18.matches(version) {
// introduced in 0.8.18 <https://github.com/ethereum/solidity/releases/tag/v0.8.18>
if let Some(ref mut meta) = self.settings.metadata {
meta.cbor_metadata = None;
}

if let Some(ref mut model_checker) = self.settings.model_checker {
if let Some(ref mut solvers) = model_checker.solvers {
// elf solver introduced in 0.8.18 <https://github.com/ethereum/solidity/releases/tag/v0.8.18>
solvers.retain(|solver| *solver != ModelCheckerSolver::Eld);
}
}
}
pub fn sanitize(&mut self, version: &Version) {
self.settings.sanitize(version)
}

/// Consumes the type and returns a [CompilerInput::sanitized] version
pub fn sanitized(mut self, version: &Version) -> Self {
self.settings.sanitize(version);
self
}

Expand Down Expand Up @@ -353,6 +299,76 @@ impl Settings {
Self { output_selection: output_selection.into(), ..Default::default() }
}

/// Consumes the type and returns a [Settings::sanitize] version
pub fn sanitized(mut self, version: &Version) -> Self {
self.sanitize(version);
self
}

/// This will remove/adjust values in the settings that are not compatible with this
/// version
pub fn sanitize(&mut self, version: &Version) {
static PRE_V0_6_0: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.6.0").unwrap());
static PRE_V0_7_5: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.7.5").unwrap());
static PRE_V0_8_7: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.8.7").unwrap());
static PRE_V0_8_10: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.8.10").unwrap());
static PRE_V0_8_18: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.8.18").unwrap());

if PRE_V0_6_0.matches(version) {
if let Some(ref mut meta) = self.metadata {
// introduced in <https://docs.soliditylang.org/en/v0.6.0/using-the-compiler.html#compiler-api>
// missing in <https://docs.soliditylang.org/en/v0.5.17/using-the-compiler.html#compiler-api>
meta.bytecode_hash.take();
}
// introduced in <https://docs.soliditylang.org/en/v0.6.0/using-the-compiler.html#compiler-api>
let _ = self.debug.take();
}

if PRE_V0_7_5.matches(version) {
// introduced in 0.7.5 <https://github.com/ethereum/solidity/releases/tag/v0.7.5>
self.via_ir.take();
}

if PRE_V0_8_7.matches(version) {
// lower the disable version from 0.8.10 to 0.8.7, due to `divModNoSlacks`,
// `showUnproved` and `solvers` are implemented
// introduced in <https://github.com/ethereum/solidity/releases/tag/v0.8.7>
self.model_checker = None;
}

if PRE_V0_8_10.matches(version) {
if let Some(ref mut debug) = self.debug {
// introduced in <https://docs.soliditylang.org/en/v0.8.10/using-the-compiler.html#compiler-api>
// <https://github.com/ethereum/solidity/releases/tag/v0.8.10>
debug.debug_info.clear();
}

if let Some(ref mut model_checker) = self.model_checker {
// introduced in <https://github.com/ethereum/solidity/releases/tag/v0.8.10>
model_checker.invariants = None;
}
}

if PRE_V0_8_18.matches(version) {
// introduced in 0.8.18 <https://github.com/ethereum/solidity/releases/tag/v0.8.18>
if let Some(ref mut meta) = self.metadata {
meta.cbor_metadata = None;
}

if let Some(ref mut model_checker) = self.model_checker {
if let Some(ref mut solvers) = model_checker.solvers {
// elf solver introduced in 0.8.18 <https://github.com/ethereum/solidity/releases/tag/v0.8.18>
solvers.retain(|solver| *solver != ModelCheckerSolver::Eld);
}
}
}
}

/// Inserts a set of `ContractOutputSelection`
pub fn push_all(&mut self, settings: impl IntoIterator<Item = ContractOutputSelection>) {
for value in settings {
Expand Down