From 7aadd0a7f7d48260ea2b516b2a9ef6adce91e024 Mon Sep 17 00:00:00 2001 From: Omar Abdulla Date: Wed, 17 Sep 2025 19:29:23 +0300 Subject: [PATCH] Implement the dyn compiler trait for compilers --- crates/compiler/src/lib.rs | 5 +++- crates/compiler/src/revive_resolc.rs | 38 ++++++++++++++++++++++++---- crates/compiler/src/solc.rs | 37 ++++++++++++++++++++++++--- 3 files changed, 70 insertions(+), 10 deletions(-) diff --git a/crates/compiler/src/lib.rs b/crates/compiler/src/lib.rs index 21af0bc..2266435 100644 --- a/crates/compiler/src/lib.rs +++ b/crates/compiler/src/lib.rs @@ -37,7 +37,10 @@ pub trait DynSolidityCompiler { fn path(&self) -> &Path; /// The low-level compiler interface. - fn build(&self, input: CompilerInput) -> Pin>>>; + fn build( + &self, + input: CompilerInput, + ) -> Pin> + '_>>; /// Does the compiler support the provided mode and version settings. fn supports_mode( diff --git a/crates/compiler/src/revive_resolc.rs b/crates/compiler/src/revive_resolc.rs index 4d02578..0f87ec5 100644 --- a/crates/compiler/src/revive_resolc.rs +++ b/crates/compiler/src/revive_resolc.rs @@ -17,7 +17,8 @@ use revive_solc_json_interface::{ }; use crate::{ - CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolidityCompiler, solc::Solc, + CompilerInput, CompilerOutput, DynSolidityCompiler, ModeOptimizerSetting, ModePipeline, + SolidityCompiler, solc::Solc, }; use alloy::json_abi::JsonAbi; @@ -37,6 +38,31 @@ struct ResolcInner { 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::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 { async fn new( context: impl AsRef @@ -69,7 +95,7 @@ impl SolidityCompiler for Resolc { fn version(&self) -> &Version { // We currently return the solc compiler version since we do not support multiple resolc // compiler versions. - self.0.solc.version() + DynSolidityCompiler::version(&self.0.solc) } 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 .stdin(Stdio::piped()) .stdout(Stdio::piped()) @@ -159,7 +186,7 @@ impl SolidityCompiler for Resolc { } let mut child = command .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 serialized_input = serde_json::to_vec(&input) @@ -281,6 +308,7 @@ impl SolidityCompiler for Resolc { optimize_setting: ModeOptimizerSetting, pipeline: ModePipeline, ) -> bool { - pipeline == ModePipeline::ViaYulIR && self.0.solc.supports_mode(optimize_setting, pipeline) + pipeline == ModePipeline::ViaYulIR + && DynSolidityCompiler::supports_mode(&self.0.solc, optimize_setting, pipeline) } } diff --git a/crates/compiler/src/solc.rs b/crates/compiler/src/solc.rs index dae007e..da5b2f0 100644 --- a/crates/compiler/src/solc.rs +++ b/crates/compiler/src/solc.rs @@ -12,7 +12,10 @@ use revive_dt_common::types::VersionOrRequirement; use revive_dt_config::{ResolcConfiguration, SolcConfiguration, WorkingDirectoryConfiguration}; 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 foundry_compilers_artifacts::{ @@ -36,6 +39,31 @@ struct SolcInner { 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::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 { async fn new( context: impl AsRef @@ -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 .stdin(Stdio::piped()) .stdout(Stdio::piped()) @@ -183,7 +212,7 @@ impl SolidityCompiler for Solc { } let mut child = command .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 serialized_input = serde_json::to_vec(&input) @@ -278,6 +307,6 @@ impl SolidityCompiler for Solc { impl Solc { fn compiler_supports_yul(&self) -> bool { 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 } }