From 2d01045ad240431776f853dcaff5212088bbf763 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Fri, 8 Aug 2025 10:50:40 +0100 Subject: [PATCH] WIP integrate new Mode/ParsedMode into rest of code --- crates/core/src/main.rs | 10 ++++----- crates/format/src/case.rs | 4 ++-- crates/format/src/metadata.rs | 25 +++++++-------------- crates/format/src/mode.rs | 42 ++++++++++++++++++++++++++--------- crates/report/src/reporter.rs | 4 ++-- 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/crates/core/src/main.rs b/crates/core/src/main.rs index 4221b8d..8b28599 100644 --- a/crates/core/src/main.rs +++ b/crates/core/src/main.rs @@ -34,7 +34,7 @@ use revive_dt_format::{ corpus::Corpus, input::Input, metadata::{ContractInstance, ContractPathAndIdent, Metadata, MetadataFile}, - mode::SolcMode, + mode::Mode, }; use revive_dt_node::pool::NodePool; use revive_dt_report::reporter::{Report, Span}; @@ -44,7 +44,7 @@ static TEMP_DIR: LazyLock = LazyLock::new(|| TempDir::new().unwrap()); type CompilationCache<'a> = Arc< RwLock< HashMap< - (&'a Path, SolcMode, TestingPlatform), + (&'a Path, Mode, TestingPlatform), Arc>>>, >, >, @@ -145,7 +145,7 @@ where .enumerate() .flat_map(move |(case_idx, case)| { metadata - .solc_modes() + .test_modes() .into_iter() .map(move |solc_mode| (path, metadata, case_idx, case, solc_mode)) }) @@ -587,7 +587,7 @@ where async fn get_or_build_contracts<'a, P: Platform>( metadata: &'a Metadata, metadata_file_path: &'a Path, - mode: SolcMode, + mode: Mode, config: &Arguments, compilation_cache: CompilationCache<'a>, deployed_libraries: &HashMap, @@ -643,7 +643,7 @@ async fn get_or_build_contracts<'a, P: Platform>( async fn compile_contracts( metadata: &Metadata, metadata_file_path: &Path, - mode: &SolcMode, + mode: &Mode, config: &Arguments, deployed_libraries: &HashMap, ) -> anyhow::Result<(Version, CompilerOutput)> { diff --git a/crates/format/src/case.rs b/crates/format/src/case.rs index 4e1f958..d9e9551 100644 --- a/crates/format/src/case.rs +++ b/crates/format/src/case.rs @@ -4,14 +4,14 @@ use revive_dt_common::macros::define_wrapper_type; use crate::{ input::{Expected, Input}, - mode::Mode, + mode::ParsedMode, }; #[derive(Debug, Default, Deserialize, Clone, Eq, PartialEq)] pub struct Case { pub name: Option, pub comment: Option, - pub modes: Option>, + pub modes: Option>, pub inputs: Vec, pub group: Option, pub expected: Option, diff --git a/crates/format/src/metadata.rs b/crates/format/src/metadata.rs index aad74fc..db8ec54 100644 --- a/crates/format/src/metadata.rs +++ b/crates/format/src/metadata.rs @@ -13,7 +13,7 @@ use revive_dt_common::{iterators::FilesWithExtensionIterator, macros::define_wra use crate::{ case::Case, - mode::{Mode, SolcMode}, + mode::{Mode, ParsedMode}, }; pub const METADATA_FILE_EXTENSION: &str = "json"; @@ -51,26 +51,17 @@ pub struct Metadata { // TODO: Convert into wrapper types for clarity. pub libraries: Option>>, pub ignore: Option, - pub modes: Option>, + pub modes: Option>, pub file_path: Option, } impl Metadata { - /// Returns the solc modes of this metadata, inserting a default mode if not present. - pub fn solc_modes(&self) -> Vec { - self.modes - .to_owned() - .unwrap_or_else(|| vec![Mode::Solidity(Default::default())]) - .iter() - .filter_map(|mode| match mode { - Mode::Solidity(solc_mode) => Some(solc_mode), - Mode::Unknown(mode) => { - tracing::debug!("compiler: ignoring unknown mode '{mode}'"); - None - } - }) - .cloned() - .collect() + /// Returns the modes that we should test from this metadata. + pub fn test_modes(&self) -> Vec { + match &self.modes { + Some(modes) => Mode::from_parsed_modes(modes.iter()).collect(), + None => Mode::all().collect() + } } /// Returns the base directory of this metadata. diff --git a/crates/format/src/mode.rs b/crates/format/src/mode.rs index 2484f37..cb0c71e 100644 --- a/crates/format/src/mode.rs +++ b/crates/format/src/mode.rs @@ -14,14 +14,14 @@ use std::collections::HashSet; /// in its requirements, and then expanding it out into a list of [`TestMode`]s. /// /// Use [`ParsedMode::to_test_modes()`] to do this. -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct TestMode { +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct Mode { pub pipeline: ModePipeline, pub optimize_setting: ModeOptimizerSetting, pub version: Option, } -impl Display for TestMode { +impl Display for Mode { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fmt_mode_parts( Some(&self.pipeline), @@ -33,7 +33,7 @@ impl Display for TestMode { } } -impl TestMode { +impl Mode { /// Return a set of [`TestMode`]s that correspond to the given [`ParsedMode`]s. /// This avoids any duplicate entries. pub fn from_parsed_modes<'a>(parsed: impl Iterator) -> impl Iterator { @@ -62,7 +62,7 @@ impl TestMode { /// ``` /// /// We can parse valid mode strings into [`ParsedMode`] using [`ParsedMode::from_str`]. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct ParsedMode { pub pipeline: Option, pub optimize_flag: Option, @@ -118,6 +118,26 @@ impl FromStr for ParsedMode { } } +impl<'de> Deserialize<'de> for ParsedMode { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let mode_string = String::deserialize(deserializer)?; + ParsedMode::from_str(&mode_string).map_err(serde::de::Error::custom) + } +} + +impl Serialize for ParsedMode { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let mode_string = self.to_string(); + serializer.serialize_str(&mode_string) + } +} + impl Display for ParsedMode { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fmt_mode_parts( @@ -163,7 +183,7 @@ fn fmt_mode_parts( impl ParsedMode { /// This takes a [`ParsedMode`] and expands it into a list of [`TestMode`]s that we should try. - pub fn to_test_modes(&self) -> impl Iterator { + pub fn to_test_modes(&self) -> impl Iterator { let pipeline_iter = self.pipeline.as_ref().map_or_else( || EitherIter::A(ModePipeline::test_cases()), |p| EitherIter::B(std::iter::once(*p)), @@ -185,7 +205,7 @@ impl ParsedMode { pipeline_iter.flat_map(move |pipeline| { optimize_settings_iter.clone().map(move |optimize_setting| { - TestMode { + Mode { pipeline, optimize_setting, version: self.version.clone(), @@ -208,7 +228,7 @@ pub enum ParseModeError { } /// What do we want the compiler to do? -#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] pub enum ModePipeline { /// Compile Solidity code via Yul IR Y, @@ -247,7 +267,7 @@ impl ModePipeline { } } -#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] pub enum ModeOptimizerSetting { /// 0 / -: Don't apply any optimizations M0, @@ -395,7 +415,7 @@ mod tests { - +/* @@ -500,3 +520,5 @@ impl<'de> Deserialize<'de> for Mode { Ok(Self::Unknown(mode_string)) } } + +*/ \ No newline at end of file diff --git a/crates/report/src/reporter.rs b/crates/report/src/reporter.rs index 9b9303d..ef5a677 100644 --- a/crates/report/src/reporter.rs +++ b/crates/report/src/reporter.rs @@ -16,7 +16,7 @@ use revive_dt_compiler::{CompilerInput, CompilerOutput}; use serde::{Deserialize, Serialize}; use revive_dt_config::{Arguments, TestingPlatform}; -use revive_dt_format::{corpus::Corpus, mode::SolcMode}; +use revive_dt_format::{corpus::Corpus, mode::Mode}; use crate::analyzer::CompilerStatistics; @@ -48,7 +48,7 @@ pub struct CompilationTask { /// The observed compiler output. pub json_output: Option, /// The observed compiler mode. - pub mode: SolcMode, + pub mode: Mode, /// The observed compiler version. pub compiler_version: String, /// The observed error, if any.