Implement the dyn compiler trait for compilers

This commit is contained in:
Omar Abdulla
2025-09-17 19:29:23 +03:00
parent 1a25c8e0ab
commit 7aadd0a7f7
3 changed files with 70 additions and 10 deletions
+4 -1
View File
@@ -37,7 +37,10 @@ pub trait DynSolidityCompiler {
fn path(&self) -> &Path; fn path(&self) -> &Path;
/// The low-level compiler interface. /// The low-level compiler interface.
fn build(&self, input: CompilerInput) -> Pin<Box<dyn Future<Output = Result<CompilerOutput>>>>; fn build(
&self,
input: CompilerInput,
) -> Pin<Box<dyn Future<Output = Result<CompilerOutput>> + '_>>;
/// Does the compiler support the provided mode and version settings. /// Does the compiler support the provided mode and version settings.
fn supports_mode( fn supports_mode(
+33 -5
View File
@@ -17,7 +17,8 @@ use revive_solc_json_interface::{
}; };
use crate::{ use crate::{
CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolidityCompiler, solc::Solc, CompilerInput, CompilerOutput, DynSolidityCompiler, ModeOptimizerSetting, ModePipeline,
SolidityCompiler, solc::Solc,
}; };
use alloy::json_abi::JsonAbi; use alloy::json_abi::JsonAbi;
@@ -37,6 +38,31 @@ struct ResolcInner {
resolc_path: PathBuf, resolc_path: PathBuf,
} }
impl DynSolidityCompiler for Resolc {
fn version(&self) -> &Version {
SolidityCompiler::version(self)
}
fn path(&self) -> &std::path::Path {
SolidityCompiler::path(self)
}
fn build(
&self,
input: CompilerInput,
) -> std::pin::Pin<Box<dyn Future<Output = Result<CompilerOutput>> + '_>> {
Box::pin(SolidityCompiler::build(self, input))
}
fn supports_mode(
&self,
optimizer_setting: ModeOptimizerSetting,
pipeline: ModePipeline,
) -> bool {
SolidityCompiler::supports_mode(self, optimizer_setting, pipeline)
}
}
impl SolidityCompiler for Resolc { impl SolidityCompiler for Resolc {
async fn new( async fn new(
context: impl AsRef<SolcConfiguration> context: impl AsRef<SolcConfiguration>
@@ -69,7 +95,7 @@ impl SolidityCompiler for Resolc {
fn version(&self) -> &Version { fn version(&self) -> &Version {
// We currently return the solc compiler version since we do not support multiple resolc // We currently return the solc compiler version since we do not support multiple resolc
// compiler versions. // compiler versions.
self.0.solc.version() DynSolidityCompiler::version(&self.0.solc)
} }
fn path(&self) -> &std::path::Path { fn path(&self) -> &std::path::Path {
@@ -138,7 +164,8 @@ impl SolidityCompiler for Resolc {
}, },
}; };
let mut command = AsyncCommand::new(self.path()); let path = &self.0.resolc_path;
let mut command = AsyncCommand::new(path);
command command
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
@@ -159,7 +186,7 @@ impl SolidityCompiler for Resolc {
} }
let mut child = command let mut child = command
.spawn() .spawn()
.with_context(|| format!("Failed to spawn resolc at {}", self.path().display()))?; .with_context(|| format!("Failed to spawn resolc at {}", path.display()))?;
let stdin_pipe = child.stdin.as_mut().expect("stdin must be piped"); let stdin_pipe = child.stdin.as_mut().expect("stdin must be piped");
let serialized_input = serde_json::to_vec(&input) let serialized_input = serde_json::to_vec(&input)
@@ -281,6 +308,7 @@ impl SolidityCompiler for Resolc {
optimize_setting: ModeOptimizerSetting, optimize_setting: ModeOptimizerSetting,
pipeline: ModePipeline, pipeline: ModePipeline,
) -> bool { ) -> bool {
pipeline == ModePipeline::ViaYulIR && self.0.solc.supports_mode(optimize_setting, pipeline) pipeline == ModePipeline::ViaYulIR
&& DynSolidityCompiler::supports_mode(&self.0.solc, optimize_setting, pipeline)
} }
} }
+33 -4
View File
@@ -12,7 +12,10 @@ use revive_dt_common::types::VersionOrRequirement;
use revive_dt_config::{ResolcConfiguration, SolcConfiguration, WorkingDirectoryConfiguration}; use revive_dt_config::{ResolcConfiguration, SolcConfiguration, WorkingDirectoryConfiguration};
use revive_dt_solc_binaries::download_solc; use revive_dt_solc_binaries::download_solc;
use crate::{CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolidityCompiler}; use crate::{
CompilerInput, CompilerOutput, DynSolidityCompiler, ModeOptimizerSetting, ModePipeline,
SolidityCompiler,
};
use anyhow::{Context as _, Result}; use anyhow::{Context as _, Result};
use foundry_compilers_artifacts::{ use foundry_compilers_artifacts::{
@@ -36,6 +39,31 @@ struct SolcInner {
solc_version: Version, solc_version: Version,
} }
impl DynSolidityCompiler for Solc {
fn version(&self) -> &Version {
SolidityCompiler::version(self)
}
fn path(&self) -> &std::path::Path {
SolidityCompiler::path(self)
}
fn build(
&self,
input: CompilerInput,
) -> std::pin::Pin<Box<dyn Future<Output = Result<CompilerOutput>> + '_>> {
Box::pin(SolidityCompiler::build(self, input))
}
fn supports_mode(
&self,
optimizer_setting: ModeOptimizerSetting,
pipeline: ModePipeline,
) -> bool {
SolidityCompiler::supports_mode(self, optimizer_setting, pipeline)
}
}
impl SolidityCompiler for Solc { impl SolidityCompiler for Solc {
async fn new( async fn new(
context: impl AsRef<SolcConfiguration> context: impl AsRef<SolcConfiguration>
@@ -162,7 +190,8 @@ impl SolidityCompiler for Solc {
}, },
}; };
let mut command = AsyncCommand::new(self.path()); let path = &self.0.solc_path;
let mut command = AsyncCommand::new(path);
command command
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
@@ -183,7 +212,7 @@ impl SolidityCompiler for Solc {
} }
let mut child = command let mut child = command
.spawn() .spawn()
.with_context(|| format!("Failed to spawn solc at {}", self.path().display()))?; .with_context(|| format!("Failed to spawn solc at {}", path.display()))?;
let stdin = child.stdin.as_mut().expect("should be piped"); let stdin = child.stdin.as_mut().expect("should be piped");
let serialized_input = serde_json::to_vec(&input) let serialized_input = serde_json::to_vec(&input)
@@ -278,6 +307,6 @@ impl SolidityCompiler for Solc {
impl Solc { impl Solc {
fn compiler_supports_yul(&self) -> bool { fn compiler_supports_yul(&self) -> bool {
const SOLC_VERSION_SUPPORTING_VIA_YUL_IR: Version = Version::new(0, 8, 13); const SOLC_VERSION_SUPPORTING_VIA_YUL_IR: Version = Version::new(0, 8, 13);
self.version() >= &SOLC_VERSION_SUPPORTING_VIA_YUL_IR DynSolidityCompiler::version(self) >= &SOLC_VERSION_SUPPORTING_VIA_YUL_IR
} }
} }