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

fix(solc): use Solc::version_req to parse version req #2607

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion ethers-solc/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,10 @@ impl Solc {
Self::version_req(version.as_str())
}

/// Returns the corresponding SemVer version requirement for the solidity version
/// Returns the corresponding SemVer version requirement for the solidity version.
///
/// Note: This is a workaround for the fact that `VersionReq::parse` does not support whitespace
/// separators and requires comma separated operators. See [VersionReq].
pub fn version_req(version: &str) -> Result<VersionReq> {
let version = version.replace(' ', ",");

Expand Down Expand Up @@ -745,6 +748,13 @@ mod tests {
use super::*;
use crate::{Artifact, CompilerInput};

#[test]
fn test_version_parse() {
let req = Solc::version_req(">=0.6.2 <0.8.21").unwrap();
let semver_req: VersionReq = ">=0.6.2,<0.8.21".parse().unwrap();
assert_eq!(req, semver_req);
}

fn solc() -> Solc {
Solc::default()
}
Expand Down
12 changes: 5 additions & 7 deletions ethers-solc/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@
//! [version pragma](https://docs.soliditylang.org/en/develop/layout-of-source-files.html#version-pragma),
//! which is defined on a per source file basis.

use crate::{
error::Result, utils, IncludePaths, ProjectPathsConfig, SolcError, SolcVersion, Source, Sources,
};
use crate::{error::Result, utils, IncludePaths, ProjectPathsConfig, SolcError, Source, Sources};
use parse::{SolData, SolDataUnit, SolImport};
use rayon::prelude::*;
use semver::VersionReq;
Expand Down Expand Up @@ -908,14 +906,14 @@ impl Node {
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
fn check_available_version(
&self,
all_versions: &[SolcVersion],
all_versions: &[crate::SolcVersion],
offline: bool,
) -> std::result::Result<(), SourceVersionError> {
let Some(data) = &self.data.version else { return Ok(()) };
let v = data.data();

let req: VersionReq =
v.parse().map_err(|err| SourceVersionError::InvalidVersion(v.to_string(), err))?;
let req = crate::Solc::version_req(v)
.map_err(|err| SourceVersionError::InvalidVersion(v.to_string(), err))?;

if !all_versions.iter().any(|v| req.matches(v.as_ref())) {
return if offline {
Expand Down Expand Up @@ -951,7 +949,7 @@ impl<'a> fmt::Display for DisplayNode<'a> {
#[allow(unused)]
enum SourceVersionError {
#[error("Failed to parse solidity version {0}: {1}")]
InvalidVersion(String, semver::Error),
InvalidVersion(String, SolcError),
#[error("No solc version exists that matches the version requirement: {0}")]
NoMatchingVersion(VersionReq),
#[error("No solc version installed that matches the version requirement: {0}")]
Expand Down