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,
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<TempDir> = LazyLock::new(|| TempDir::new().unwrap());
type CompilationCache<'a> = Arc<
RwLock<
HashMap<
(&'a Path, SolcMode, TestingPlatform),
(&'a Path, Mode, TestingPlatform),
Arc<Mutex<Option<Arc<(Version, CompilerOutput)>>>>,
>,
>,
@@ -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<ContractInstance, (Address, JsonAbi)>,
@@ -643,7 +643,7 @@ async fn get_or_build_contracts<'a, P: Platform>(
async fn compile_contracts<P: Platform>(
metadata: &Metadata,
metadata_file_path: &Path,
mode: &SolcMode,
mode: &Mode,
config: &Arguments,
deployed_libraries: &HashMap<ContractInstance, (Address, JsonAbi)>,
) -> anyhow::Result<(Version, CompilerOutput)> {
+2 -2
View File
@@ -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<String>,
pub comment: Option<String>,
pub modes: Option<Vec<Mode>>,
pub modes: Option<Vec<ParsedMode>>,
pub inputs: Vec<Input>,
pub group: Option<String>,
pub expected: Option<Expected>,
+8 -17
View File
@@ -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<BTreeMap<PathBuf, BTreeMap<ContractIdent, ContractInstance>>>,
pub ignore: Option<bool>,
pub modes: Option<Vec<Mode>>,
pub modes: Option<Vec<ParsedMode>>,
pub file_path: Option<PathBuf>,
}
impl Metadata {
/// Returns the solc modes of this metadata, inserting a default mode if not present.
pub fn solc_modes(&self) -> Vec<SolcMode> {
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<Mode> {
match &self.modes {
Some(modes) => Mode::from_parsed_modes(modes.iter()).collect(),
None => Mode::all().collect()
}
}
/// 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.
///
/// 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<semver::VersionReq>,
}
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<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`].
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ParsedMode {
pub pipeline: Option<ModePipeline>,
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 {
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<Item = TestMode> {
pub fn to_test_modes(&self) -> impl Iterator<Item = Mode> {
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))
}
}
*/
+2 -2
View File
@@ -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<CompilerOutput>,
/// The observed compiler mode.
pub mode: SolcMode,
pub mode: Mode,
/// The observed compiler version.
pub compiler_version: String,
/// The observed error, if any.