From 3784cd8514bf2c2248a21df6c19455e8a674ef63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Rodr=C3=ADguez?= Date: Wed, 4 Dec 2024 15:16:59 -0300 Subject: [PATCH] refactor: adapt to CompilerContract trait type (#9423) * refactor: adapt to CompilerContract trait type * chore: cargo fmt * fix: specify MultiCompiler in MultiContractRunner::build * bump * fix --------- Co-authored-by: Arsenii Kulikov --- Cargo.lock | 26 +++++++++++++------------- Cargo.toml | 2 +- crates/cast/bin/cmd/storage.rs | 6 +++--- crates/common/src/compile.rs | 19 ++++++++++++++----- crates/config/src/lib.rs | 15 +++++++++------ crates/evm/traces/src/debug/sources.rs | 4 ++-- crates/forge/bin/cmd/clone.rs | 6 +++--- crates/forge/bin/cmd/coverage.rs | 3 ++- crates/forge/bin/cmd/test/mod.rs | 7 +++++-- crates/forge/src/multi_runner.rs | 8 ++++++-- crates/forge/tests/it/test_helpers.rs | 7 ++++--- crates/test-utils/src/util.rs | 5 ++++- 12 files changed, 66 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87ac75dee06e..e60060bfcb66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3864,9 +3864,9 @@ dependencies = [ [[package]] name = "foundry-compilers" -version = "0.12.4" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "daece74fc0b127e587ac343405283577b4fe9d0195317998c3031778b5f799cd" +checksum = "611e6de7379c57fc353a53e718cd95844e9bd08b2e3ca79a34b76d4a84d38e48" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -3901,9 +3901,9 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts" -version = "0.12.4" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0c6b0571a37cc9860103df548330eaceb36e9438cb0264ec63e5db74031fa4f" +checksum = "868df34b353da95395e61fd83e4a56cb075f462f58d9fa1150c9cf96ccb46637" dependencies = [ "foundry-compilers-artifacts-solc", "foundry-compilers-artifacts-vyper", @@ -3911,9 +3911,9 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts-solc" -version = "0.12.4" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "278cc3f1f4f628793ae6c0db38b0d3fe4124ab0bc10da081b1eb365766512e6d" +checksum = "ac37bffdf6d62cbc4ce03393cc45814d32274807b87486a808a370efbd08b67d" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -3935,9 +3935,9 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts-vyper" -version = "0.12.4" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "196b16fcfbbe69a315d64f74dd817ba6843f7749c64124b937118551414b9b90" +checksum = "8c873d45485dc4b4f351f2f6c6acbb7f5ef8ec27f12e1bd0e6dc016cb9bdda2b" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -3950,9 +3950,9 @@ dependencies = [ [[package]] name = "foundry-compilers-core" -version = "0.12.4" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1eb08a74eb12b281038bbf74cbbd76cc3c85546896628d4c7c769c816ab4104b" +checksum = "2a605a29e2c0b9c54f14540ec3d03a2434fbaabdda8e6565451cdd38ae8fbd00" dependencies = [ "alloy-primitives", "cfg-if", @@ -7112,7 +7112,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" dependencies = [ "anyhow", - "itertools 0.11.0", + "itertools 0.13.0", "proc-macro2", "quote", "syn 2.0.90", @@ -8565,7 +8565,7 @@ dependencies = [ "const-hex", "derive_builder", "dunce", - "itertools 0.11.0", + "itertools 0.13.0", "itoa", "lasso", "match_cfg", @@ -8601,7 +8601,7 @@ dependencies = [ "alloy-primitives", "bitflags 2.6.0", "bumpalo", - "itertools 0.11.0", + "itertools 0.13.0", "memchr", "num-bigint", "num-rational", diff --git a/Cargo.toml b/Cargo.toml index af1d118b9fe8..0fa4b4bdb33f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -169,7 +169,7 @@ foundry-linking = { path = "crates/linking" } # solc & compilation utilities foundry-block-explorers = { version = "0.9.0", default-features = false } -foundry-compilers = { version = "0.12.3", default-features = false } +foundry-compilers = { version = "0.12.5", default-features = false } foundry-fork-db = "0.8.0" solang-parser = "=0.3.3" solar-ast = { version = "=0.1.0", default-features = false } diff --git a/crates/cast/bin/cmd/storage.rs b/crates/cast/bin/cmd/storage.rs index 13fa908bc94a..468f20b27ae1 100644 --- a/crates/cast/bin/cmd/storage.rs +++ b/crates/cast/bin/cmd/storage.rs @@ -20,7 +20,7 @@ use foundry_common::{ shell, }; use foundry_compilers::{ - artifacts::{ConfigurableContractArtifact, StorageLayout}, + artifacts::{ConfigurableContractArtifact, Contract, StorageLayout}, compilers::{ solc::{Solc, SolcCompiler}, Compiler, @@ -284,7 +284,7 @@ fn print_storage(layout: StorageLayout, values: Vec, pretty: bool) "{}", serde_json::to_string_pretty(&serde_json::to_value(StorageReport { layout, values })?)? )?; - return Ok(()) + return Ok(()); } let mut table = Table::new(); @@ -314,7 +314,7 @@ fn print_storage(layout: StorageLayout, values: Vec, pretty: bool) Ok(()) } -fn add_storage_layout_output(project: &mut Project) { +fn add_storage_layout_output>(project: &mut Project) { project.artifacts.additional_values.storage_layout = true; project.update_output_selection(|selection| { selection.0.values_mut().for_each(|contract_selection| { diff --git a/crates/common/src/compile.rs b/crates/common/src/compile.rs index 507f328307c5..47fe226d98db 100644 --- a/crates/common/src/compile.rs +++ b/crates/common/src/compile.rs @@ -10,7 +10,7 @@ use comfy_table::{presets::ASCII_MARKDOWN, Attribute, Cell, CellAlignment, Color use eyre::Result; use foundry_block_explorers::contract::Metadata; use foundry_compilers::{ - artifacts::{remappings::Remapping, BytecodeObject, Source}, + artifacts::{remappings::Remapping, BytecodeObject, Contract, Source}, compilers::{ solc::{Solc, SolcCompiler}, Compiler, @@ -129,7 +129,10 @@ impl ProjectCompiler { } /// Compiles the project. - pub fn compile(mut self, project: &Project) -> Result> { + pub fn compile>( + mut self, + project: &Project, + ) -> Result> { // TODO: Avoid process::exit if !project.paths.has_input_files() && self.files.is_empty() { sh_println!("Nothing to compile")?; @@ -163,7 +166,10 @@ impl ProjectCompiler { /// ProjectCompiler::new().compile_with(|| Ok(prj.compile()?)).unwrap(); /// ``` #[instrument(target = "forge::compile", skip_all)] - fn compile_with(self, f: F) -> Result> + fn compile_with, F>( + self, + f: F, + ) -> Result> where F: FnOnce() -> Result>, { @@ -202,7 +208,10 @@ impl ProjectCompiler { } /// If configured, this will print sizes or names - fn handle_output(&self, output: &ProjectCompileOutput) { + fn handle_output>( + &self, + output: &ProjectCompileOutput, + ) { let print_names = self.print_names.unwrap_or(false); let print_sizes = self.print_sizes.unwrap_or(false); @@ -465,7 +474,7 @@ pub struct ContractInfo { /// If `verify` and it's a standalone script, throw error. Only allowed for projects. /// /// **Note:** this expects the `target_path` to be absolute -pub fn compile_target( +pub fn compile_target>( target_path: &Path, project: &Project, quiet: bool, diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index f2d25461dfaf..08c1ed0ee409 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -35,8 +35,8 @@ use foundry_compilers::{ error::SolcError, multi::{MultiCompilerParsedSource, MultiCompilerRestrictions}, solc::{CliSettings, SolcSettings}, - ConfigurableArtifacts, Graph, Project, ProjectPathsConfig, RestrictionsWithVersion, - VyperLanguage, + ArtifactOutput, ConfigurableArtifacts, Graph, Project, ProjectPathsConfig, + RestrictionsWithVersion, VyperLanguage, }; use regex::Regex; use revm_primitives::{map::AddressHashMap, FixedBytes, SpecId}; @@ -1021,7 +1021,10 @@ impl Config { } /// Cleans the project. - pub fn cleanup(&self, project: &Project) -> Result<(), SolcError> { + pub fn cleanup>( + &self, + project: &Project, + ) -> Result<(), SolcError> { project.cleanup()?; // Remove last test run failures file. @@ -1090,7 +1093,7 @@ impl Config { rx.recv().expect("sender dropped") } Err(RecvTimeoutError::Disconnected) => panic!("sender dropped"), - } + }; } if let Some(ref solc) = self.solc { let solc = match solc { @@ -1291,11 +1294,11 @@ impl Config { ) -> Option, UnresolvedEnvVarError>> { let mut endpoints = self.rpc_endpoints.clone().resolved(); if let Some(endpoint) = endpoints.remove(maybe_alias) { - return Some(endpoint.map(Cow::Owned)) + return Some(endpoint.map(Cow::Owned)); } if let Ok(Some(endpoint)) = mesc::get_endpoint_by_query(maybe_alias, Some("foundry")) { - return Some(Ok(Cow::Owned(endpoint.url))) + return Some(Ok(Cow::Owned(endpoint.url))); } None diff --git a/crates/evm/traces/src/debug/sources.rs b/crates/evm/traces/src/debug/sources.rs index f934be61f00b..b2e37e32d934 100644 --- a/crates/evm/traces/src/debug/sources.rs +++ b/crates/evm/traces/src/debug/sources.rs @@ -3,7 +3,7 @@ use foundry_common::compact_to_contract; use foundry_compilers::{ artifacts::{ sourcemap::{SourceElement, SourceMap}, - Bytecode, ContractBytecodeSome, Libraries, Source, + Bytecode, Contract, ContractBytecodeSome, Libraries, Source, }, multi::MultiCompilerLanguage, Artifact, Compiler, ProjectCompileOutput, @@ -137,7 +137,7 @@ impl ContractSources { Ok(sources) } - pub fn insert( + pub fn insert>( &mut self, output: &ProjectCompileOutput, root: &Path, diff --git a/crates/forge/bin/cmd/clone.rs b/crates/forge/bin/cmd/clone.rs index 193e511836ba..52ec97c48098 100644 --- a/crates/forge/bin/cmd/clone.rs +++ b/crates/forge/bin/cmd/clone.rs @@ -266,7 +266,7 @@ impl CloneArgs { let remappings_txt_content = config.remappings.iter().map(|r| r.to_string()).collect::>().join("\n"); if fs::write(&remappings_txt, remappings_txt_content).is_err() { - return false + return false; } let profile = config.profile.as_str().as_str(); @@ -612,7 +612,7 @@ impl EtherscanClient for Client { mod tests { use super::*; use alloy_primitives::hex; - use foundry_compilers::Artifact; + use foundry_compilers::CompilerContract; use foundry_test_utils::rpc::next_mainnet_etherscan_api_key; use std::collections::BTreeMap; @@ -631,7 +631,7 @@ mod tests { contracts.iter().for_each(|(name, contract)| { if name == contract_name { let compiled_creation_code = - contract.get_bytecode_object().expect("creation code not found"); + contract.bin_ref().expect("creation code not found"); assert!( hex::encode(compiled_creation_code.as_ref()) .starts_with(stripped_creation_code), diff --git a/crates/forge/bin/cmd/coverage.rs b/crates/forge/bin/cmd/coverage.rs index c3d0545c28d9..65b4aad03f52 100644 --- a/crates/forge/bin/cmd/coverage.rs +++ b/crates/forge/bin/cmd/coverage.rs @@ -19,6 +19,7 @@ use foundry_compilers::{ artifacts::{ sourcemap::SourceMap, CompactBytecode, CompactDeployedBytecode, SolcLanguage, Source, }, + compilers::multi::MultiCompiler, Artifact, ArtifactId, Project, ProjectCompileOutput, }; use foundry_config::{Config, SolcReq}; @@ -245,7 +246,7 @@ impl CoverageArgs { .sender(evm_opts.sender) .with_fork(evm_opts.get_fork(&config, env.clone())) .set_coverage(true) - .build(&root, output, env, evm_opts)?; + .build::(&root, output, env, evm_opts)?; let known_contracts = runner.known_contracts.clone(); diff --git a/crates/forge/bin/cmd/test/mod.rs b/crates/forge/bin/cmd/test/mod.rs index dd6d04a98cde..5f245d002769 100644 --- a/crates/forge/bin/cmd/test/mod.rs +++ b/crates/forge/bin/cmd/test/mod.rs @@ -23,7 +23,10 @@ use foundry_cli::{ use foundry_common::{compile::ProjectCompiler, evm::EvmArgs, fs, shell, TestFunctionExt}; use foundry_compilers::{ artifacts::output_selection::OutputSelection, - compilers::{multi::MultiCompilerLanguage, Language}, + compilers::{ + multi::{MultiCompiler, MultiCompilerLanguage}, + Language, + }, utils::source_files_iter, ProjectCompileOutput, }; @@ -353,7 +356,7 @@ impl TestArgs { .with_fork(evm_opts.get_fork(&config, env.clone())) .enable_isolation(evm_opts.isolate) .alphanet(evm_opts.alphanet) - .build(project_root, &output, env, evm_opts)?; + .build::(project_root, &output, env, evm_opts)?; let mut maybe_override_mt = |flag, maybe_regex: Option<&Option>| { if let Some(Some(regex)) = maybe_regex { diff --git a/crates/forge/src/multi_runner.rs b/crates/forge/src/multi_runner.rs index 0372becb208a..9240c6f27eaf 100644 --- a/crates/forge/src/multi_runner.rs +++ b/crates/forge/src/multi_runner.rs @@ -8,7 +8,11 @@ use alloy_json_abi::{Function, JsonAbi}; use alloy_primitives::{Address, Bytes, U256}; use eyre::Result; use foundry_common::{get_contract_name, shell::verbosity, ContractsByArtifact, TestFunctionExt}; -use foundry_compilers::{artifacts::Libraries, Artifact, ArtifactId, ProjectCompileOutput}; +use foundry_compilers::{ + artifacts::{Contract, Libraries}, + compilers::Compiler, + Artifact, ArtifactId, ProjectCompileOutput, +}; use foundry_config::{Config, InlineConfig}; use foundry_evm::{ backend::Backend, @@ -457,7 +461,7 @@ impl MultiContractRunnerBuilder { /// Given an EVM, proceeds to return a runner which is able to execute all tests /// against that evm - pub fn build( + pub fn build>( self, root: &Path, output: &ProjectCompileOutput, diff --git a/crates/forge/tests/it/test_helpers.rs b/crates/forge/tests/it/test_helpers.rs index 937f582f4e4f..3c8500772b4a 100644 --- a/crates/forge/tests/it/test_helpers.rs +++ b/crates/forge/tests/it/test_helpers.rs @@ -5,6 +5,7 @@ use alloy_primitives::U256; use forge::{revm::primitives::SpecId, MultiContractRunner, MultiContractRunnerBuilder}; use foundry_compilers::{ artifacts::{EvmVersion, Libraries, Settings}, + compilers::multi::MultiCompiler, utils::RuntimeOrHandle, Project, ProjectCompileOutput, SolcConfig, Vyper, }; @@ -212,7 +213,7 @@ impl ForgeTestData { builder .enable_isolation(opts.isolate) .sender(config.sender) - .build(root, &self.output, opts.local_evm_env(), opts) + .build::(root, &self.output, opts.local_evm_env(), opts) .unwrap() } @@ -221,7 +222,7 @@ impl ForgeTestData { let mut opts = config_evm_opts(&self.config); opts.verbosity = 5; self.base_runner() - .build(self.project.root(), &self.output, opts.local_evm_env(), opts) + .build::(self.project.root(), &self.output, opts.local_evm_env(), opts) .unwrap() } @@ -237,7 +238,7 @@ impl ForgeTestData { self.base_runner() .with_fork(fork) - .build(self.project.root(), &self.output, env, opts) + .build::(self.project.root(), &self.output, env, opts) .unwrap() } } diff --git a/crates/test-utils/src/util.rs b/crates/test-utils/src/util.rs index fb9eca587f4b..e7410ed607ed 100644 --- a/crates/test-utils/src/util.rs +++ b/crates/test-utils/src/util.rs @@ -1,6 +1,7 @@ use crate::init_tracing; use eyre::{Result, WrapErr}; use foundry_compilers::{ + artifacts::Contract, cache::CompilerCache, compilers::multi::MultiCompiler, error::Result as SolcResult, @@ -420,7 +421,9 @@ pub fn setup_cast_project(test: TestProject) -> (TestProject, TestCommand) { /// /// Test projects are created from a global atomic counter to avoid duplicates. #[derive(Clone, Debug)] -pub struct TestProject { +pub struct TestProject< + T: ArtifactOutput + Default = ConfigurableArtifacts, +> { /// The directory in which this test executable is running. exe_root: PathBuf, /// The project in which the test should run.