WIP integrate new Mode/ParsedMode into rest of code

This commit is contained in:
James Wilson
2025-08-08 10:50:40 +01:00
parent bd966300a5
commit 2d01045ad2
5 changed files with 49 additions and 36 deletions
+5 -5
View File
@@ -34,7 +34,7 @@ use revive_dt_format::{
corpus::Corpus, corpus::Corpus,
input::Input, input::Input,
metadata::{ContractInstance, ContractPathAndIdent, Metadata, MetadataFile}, metadata::{ContractInstance, ContractPathAndIdent, Metadata, MetadataFile},
mode::SolcMode, mode::Mode,
}; };
use revive_dt_node::pool::NodePool; use revive_dt_node::pool::NodePool;
use revive_dt_report::reporter::{Report, Span}; use revive_dt_report::reporter::{Report, Span};
@@ -44,7 +44,7 @@ static TEMP_DIR: LazyLock<TempDir> = LazyLock::new(|| TempDir::new().unwrap());
type CompilationCache<'a> = Arc< type CompilationCache<'a> = Arc<
RwLock< RwLock<
HashMap< HashMap<
(&'a Path, SolcMode, TestingPlatform), (&'a Path, Mode, TestingPlatform),
Arc<Mutex<Option<Arc<(Version, CompilerOutput)>>>>, Arc<Mutex<Option<Arc<(Version, CompilerOutput)>>>>,
>, >,
>, >,
@@ -145,7 +145,7 @@ where
.enumerate() .enumerate()
.flat_map(move |(case_idx, case)| { .flat_map(move |(case_idx, case)| {
metadata metadata
.solc_modes() .test_modes()
.into_iter() .into_iter()
.map(move |solc_mode| (path, metadata, case_idx, case, solc_mode)) .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>( async fn get_or_build_contracts<'a, P: Platform>(
metadata: &'a Metadata, metadata: &'a Metadata,
metadata_file_path: &'a Path, metadata_file_path: &'a Path,
mode: SolcMode, mode: Mode,
config: &Arguments, config: &Arguments,
compilation_cache: CompilationCache<'a>, compilation_cache: CompilationCache<'a>,
deployed_libraries: &HashMap<ContractInstance, (Address, JsonAbi)>, deployed_libraries: &HashMap<ContractInstance, (Address, JsonAbi)>,
@@ -643,7 +643,7 @@ async fn get_or_build_contracts<'a, P: Platform>(
async fn compile_contracts<P: Platform>( async fn compile_contracts<P: Platform>(
metadata: &Metadata, metadata: &Metadata,
metadata_file_path: &Path, metadata_file_path: &Path,
mode: &SolcMode, mode: &Mode,
config: &Arguments, config: &Arguments,
deployed_libraries: &HashMap<ContractInstance, (Address, JsonAbi)>, deployed_libraries: &HashMap<ContractInstance, (Address, JsonAbi)>,
) -> anyhow::Result<(Version, CompilerOutput)> { ) -> anyhow::Result<(Version, CompilerOutput)> {
+2 -2
View File
@@ -4,14 +4,14 @@ use revive_dt_common::macros::define_wrapper_type;
use crate::{ use crate::{
input::{Expected, Input}, input::{Expected, Input},
mode::Mode, mode::ParsedMode,
}; };
#[derive(Debug, Default, Deserialize, Clone, Eq, PartialEq)] #[derive(Debug, Default, Deserialize, Clone, Eq, PartialEq)]
pub struct Case { pub struct Case {
pub name: Option<String>, pub name: Option<String>,
pub comment: Option<String>, pub comment: Option<String>,
pub modes: Option<Vec<Mode>>, pub modes: Option<Vec<ParsedMode>>,
pub inputs: Vec<Input>, pub inputs: Vec<Input>,
pub group: Option<String>, pub group: Option<String>,
pub expected: Option<Expected>, pub expected: Option<Expected>,
+8 -17
View File
@@ -13,7 +13,7 @@ use revive_dt_common::{iterators::FilesWithExtensionIterator, macros::define_wra
use crate::{ use crate::{
case::Case, case::Case,
mode::{Mode, SolcMode}, mode::{Mode, ParsedMode},
}; };
pub const METADATA_FILE_EXTENSION: &str = "json"; pub const METADATA_FILE_EXTENSION: &str = "json";
@@ -51,26 +51,17 @@ pub struct Metadata {
// TODO: Convert into wrapper types for clarity. // TODO: Convert into wrapper types for clarity.
pub libraries: Option<BTreeMap<PathBuf, BTreeMap<ContractIdent, ContractInstance>>>, pub libraries: Option<BTreeMap<PathBuf, BTreeMap<ContractIdent, ContractInstance>>>,
pub ignore: Option<bool>, pub ignore: Option<bool>,
pub modes: Option<Vec<Mode>>, pub modes: Option<Vec<ParsedMode>>,
pub file_path: Option<PathBuf>, pub file_path: Option<PathBuf>,
} }
impl Metadata { impl Metadata {
/// Returns the solc modes of this metadata, inserting a default mode if not present. /// Returns the modes that we should test from this metadata.
pub fn solc_modes(&self) -> Vec<SolcMode> { pub fn test_modes(&self) -> Vec<Mode> {
self.modes match &self.modes {
.to_owned() Some(modes) => Mode::from_parsed_modes(modes.iter()).collect(),
.unwrap_or_else(|| vec![Mode::Solidity(Default::default())]) None => Mode::all().collect()
.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 base directory of this metadata. /// Returns the base directory of this metadata.
+32 -10
View File
@@ -14,14 +14,14 @@ use std::collections::HashSet;
/// in its requirements, and then expanding it out into a list of [`TestMode`]s. /// in its requirements, and then expanding it out into a list of [`TestMode`]s.
/// ///
/// Use [`ParsedMode::to_test_modes()`] to do this. /// Use [`ParsedMode::to_test_modes()`] to do this.
#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TestMode { pub struct Mode {
pub pipeline: ModePipeline, pub pipeline: ModePipeline,
pub optimize_setting: ModeOptimizerSetting, pub optimize_setting: ModeOptimizerSetting,
pub version: Option<semver::VersionReq>, pub version: Option<semver::VersionReq>,
} }
impl Display for TestMode { impl Display for Mode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt_mode_parts( fmt_mode_parts(
Some(&self.pipeline), 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. /// Return a set of [`TestMode`]s that correspond to the given [`ParsedMode`]s.
/// This avoids any duplicate entries. /// This avoids any duplicate entries.
pub fn from_parsed_modes<'a>(parsed: impl Iterator<Item = &'a ParsedMode>) -> impl Iterator<Item = Self> { pub fn from_parsed_modes<'a>(parsed: impl Iterator<Item = &'a ParsedMode>) -> impl Iterator<Item = Self> {
@@ -62,7 +62,7 @@ impl TestMode {
/// ``` /// ```
/// ///
/// We can parse valid mode strings into [`ParsedMode`] using [`ParsedMode::from_str`]. /// 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 struct ParsedMode {
pub pipeline: Option<ModePipeline>, pub pipeline: Option<ModePipeline>,
pub optimize_flag: Option<bool>, pub optimize_flag: Option<bool>,
@@ -118,6 +118,26 @@ impl FromStr for ParsedMode {
} }
} }
impl<'de> Deserialize<'de> for ParsedMode {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mode_string = self.to_string();
serializer.serialize_str(&mode_string)
}
}
impl Display for ParsedMode { impl Display for ParsedMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt_mode_parts( fmt_mode_parts(
@@ -163,7 +183,7 @@ fn fmt_mode_parts(
impl ParsedMode { impl ParsedMode {
/// This takes a [`ParsedMode`] and expands it into a list of [`TestMode`]s that we should try. /// 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<Item = TestMode> { pub fn to_test_modes(&self) -> impl Iterator<Item = Mode> {
let pipeline_iter = self.pipeline.as_ref().map_or_else( let pipeline_iter = self.pipeline.as_ref().map_or_else(
|| EitherIter::A(ModePipeline::test_cases()), || EitherIter::A(ModePipeline::test_cases()),
|p| EitherIter::B(std::iter::once(*p)), |p| EitherIter::B(std::iter::once(*p)),
@@ -185,7 +205,7 @@ impl ParsedMode {
pipeline_iter.flat_map(move |pipeline| { pipeline_iter.flat_map(move |pipeline| {
optimize_settings_iter.clone().map(move |optimize_setting| { optimize_settings_iter.clone().map(move |optimize_setting| {
TestMode { Mode {
pipeline, pipeline,
optimize_setting, optimize_setting,
version: self.version.clone(), version: self.version.clone(),
@@ -208,7 +228,7 @@ pub enum ParseModeError {
} }
/// What do we want the compiler to do? /// 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 { pub enum ModePipeline {
/// Compile Solidity code via Yul IR /// Compile Solidity code via Yul IR
Y, 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 { pub enum ModeOptimizerSetting {
/// 0 / -: Don't apply any optimizations /// 0 / -: Don't apply any optimizations
M0, M0,
@@ -395,7 +415,7 @@ mod tests {
/*
@@ -500,3 +520,5 @@ impl<'de> Deserialize<'de> for Mode {
Ok(Self::Unknown(mode_string)) Ok(Self::Unknown(mode_string))
} }
} }
*/
+2 -2
View File
@@ -16,7 +16,7 @@ use revive_dt_compiler::{CompilerInput, CompilerOutput};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use revive_dt_config::{Arguments, TestingPlatform}; 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; use crate::analyzer::CompilerStatistics;
@@ -48,7 +48,7 @@ pub struct CompilationTask {
/// The observed compiler output. /// The observed compiler output.
pub json_output: Option<CompilerOutput>, pub json_output: Option<CompilerOutput>,
/// The observed compiler mode. /// The observed compiler mode.
pub mode: SolcMode, pub mode: Mode,
/// The observed compiler version. /// The observed compiler version.
pub compiler_version: String, pub compiler_version: String,
/// The observed error, if any. /// The observed error, if any.