Skip to content

Commit

Permalink
fmt + clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Apr 16, 2024
1 parent d507b5d commit 6627fad
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 153 deletions.
2 changes: 1 addition & 1 deletion src/compiler/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<FT: FuzzTypeParams> FuzzGenerator<FT> {
}
}

pub fn result<'a>(&'a self) -> &'a FT::Expr {
pub fn result(&self) -> &FT::Expr {
&self.structure
}

Expand Down
14 changes: 11 additions & 3 deletions src/compiler/optimize/above22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ use crate::compiler::StartOfCodegenOptimization;
#[derive(Default, Clone)]
pub struct Strategy23 {}

#[cfg(feature = "fuzz")]
fn enable_cse_merge_fix_so_can_be_disabled_for_tests(opts: Rc<dyn CompilerOpts>) -> bool {
!opts.filename().starts_with("*cl23-pre-cse-merge-fix")
}

#[cfg(not(feature = "fuzz"))]
fn enable_cse_merge_fix_so_can_be_disabled_for_tests(opts: Rc<dyn CompilerOpts>) -> bool {
true
}

impl Strategy23 {
pub fn new() -> Self {
Strategy23 {}
Expand All @@ -40,10 +50,8 @@ impl Optimization for Strategy23 {
mut p0: CompileForm,
) -> Result<CompileForm, CompileErr> {
let mut rebuilt_helpers = Vec::new();
// XXX Come up with a good way to do this.
#[deprecated]
let enable_merge_disable_for_tests =
!opts.filename().starts_with("*cl23-pre-cse-merge-fix");
enable_cse_merge_fix_so_can_be_disabled_for_tests(opts.clone());
for h in p0.helpers.iter() {
if let HelperForm::Defun(inline, d) = h {
eprintln!("cse optimize helper {}", h.to_sexp());
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/optimize/cse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,11 @@ fn merge_cse_binding(body: &BodyForm, binding: Rc<Binding>) -> BodyForm {

type CSEReplacementTargetAndBindings<'a> = Vec<&'a (Vec<BodyformPathArc>, Vec<BindingStackEntry>)>;

/// Given a bodyform, CSE analyze and produce a semantically equivalent bodyform
/// that has common expressions removed into assignments to variables prefixed
/// with cse.
///
/// Note: allow_merge is an option only for regression testing.
pub fn cse_optimize_bodyform(
loc: &Srcloc,
name: &[u8],
Expand Down
1 change: 1 addition & 0 deletions src/compiler/sexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ where
//
// Fuzzing support for SExp
//
#[cfg(any(test, feature = "fuzz"))]
fn find_in_structure_inner(
parents: &mut Vec<Rc<SExp>>,
structure: Rc<SExp>,
Expand Down
2 changes: 0 additions & 2 deletions src/tests/classic/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use rand::distributions::Standard;
#[cfg(test)]
use rand::prelude::*;
#[cfg(test)]
use rand::Rng;
#[cfg(test)]
use rand_chacha::ChaChaRng;

use std::borrow::Borrow;
Expand Down
2 changes: 0 additions & 2 deletions src/tests/compiler/clvm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#[cfg(test)]
use rand::prelude::*;
#[cfg(test)]
use rand::Rng;
#[cfg(test)]
use rand_chacha::ChaChaRng;

use num_bigint::ToBigInt;
Expand Down
43 changes: 6 additions & 37 deletions src/tests/compiler/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rand::prelude::Distribution;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;
use std::borrow::Borrow;
use std::cell::{RefCell, RefMut};
use std::cell::RefCell;
use std::collections::{BTreeSet, HashMap};
use std::fmt::{Debug, Display};
use std::rc::Rc;
Expand All @@ -17,15 +17,14 @@ use crate::compiler::clvm::{convert_to_clvm_rs, run};
use crate::compiler::compiler::{compile_file, DefaultCompilerOpts};
use crate::compiler::comptypes::{BodyForm, CompileErr, CompilerOpts, PrimaryCodegen};
use crate::compiler::dialect::{detect_modern, AcceptedDialect};
use crate::compiler::fuzz::{ExprModifier, FuzzGenerator, FuzzTypeParams, Rule};
use crate::compiler::fuzz::{FuzzGenerator, FuzzTypeParams, Rule};
use crate::compiler::prims::primquote;
use crate::compiler::sexp::{enlist, parse_sexp, SExp};
use crate::compiler::srcloc::Srcloc;
use crate::compiler::BasicCompileContext;

#[derive(Debug)]
pub struct GenError {
message: String,
pub message: String,
}
impl From<&str> for GenError {
fn from(m: &str) -> GenError {
Expand Down Expand Up @@ -59,12 +58,6 @@ impl TestModuleCompilerOpts {
written_files: self.written_files.clone(),
})
}

pub fn get_written_file<'a>(&'a self, name: &str) -> Option<Vec<u8>> {
let files_ref: &RefCell<HashMap<String, Vec<u8>>> = self.written_files.borrow();
let files: &HashMap<String, Vec<u8>> = &files_ref.borrow();
files.get(name).map(|f| f.to_vec())
}
}

impl CompilerOpts for TestModuleCompilerOpts {
Expand Down Expand Up @@ -245,7 +238,7 @@ pub fn simple_seeded_rng(seed: u32) -> ChaCha8Rng {

pub trait PropertyTestState<FT: FuzzTypeParams> {
fn new_state<R: Rng>(rng: &mut R) -> Self;
fn examine(&self, result: &FT::Expr) {}
fn examine(&self, _result: &FT::Expr) {}
}
pub trait PropertyTestRun {
fn filename(&self) -> String {
Expand All @@ -254,7 +247,7 @@ pub trait PropertyTestRun {
fn run_args(&self) -> String {
"()".to_string()
}
fn check(&self, run_result: Rc<SExp>) {}
fn check(&self, _run_result: Rc<SExp>) {}
}

pub struct PropertyTest<FT: FuzzTypeParams> {
Expand All @@ -267,34 +260,14 @@ pub struct PropertyTest<FT: FuzzTypeParams> {
}

impl<FT: FuzzTypeParams> PropertyTest<FT> {
pub fn generate<R: Rng, S: PropertyTestState<FT>>(
rng: &mut R,
top_node: FT::Expr,
rules: &[Rc<dyn Rule<FT>>],
) -> (FT::State, FT::Expr)
where
FT::State: PropertyTestState<FT>,
FT::Error: Debug,
{
let pt = PropertyTest {
run_times: 0,
run_cutoff: 0,
run_expansion: 0,

top_node,
rules: rules.to_vec(),
};
pt.make_result(rng)
}

pub fn run<R>(&self, rng: &mut R)
where
R: Rng + Sized,
FT::State: PropertyTestState<FT> + PropertyTestRun,
FT::Error: Debug,
FT::Expr: ToString + Display,
{
for i in 0..self.run_times {
for _ in 0..self.run_times {
let (mc, result) = self.make_result(rng);
let program_text = result.to_string();

Expand Down Expand Up @@ -328,17 +301,13 @@ impl<FT: FuzzTypeParams> PropertyTest<FT> {
FT::Error: Debug,
FT::State: PropertyTestState<FT>,
{
let srcloc = Srcloc::start("*value*");
let opts: Rc<dyn CompilerOpts> = Rc::new(DefaultCompilerOpts::new("*test*"));

let mut idx = 0;
let mut fuzzgen = FuzzGenerator::new(self.top_node.clone(), &self.rules);
let mut mc = FT::State::new_state(rng);
while fuzzgen
.expand(&mut mc, idx > self.run_expansion, rng)
.expect("should expand")
{
let mut idx = 0;
let mut fuzzgen = FuzzGenerator::new(self.top_node.clone(), &self.rules);
let mut mc = FT::State::new_state(rng);
while fuzzgen
Expand Down
62 changes: 24 additions & 38 deletions src/tests/compiler/optimizer/cse_fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
use num_bigint::ToBigInt;
use rand::{Rng, SeedableRng};
use std::borrow::Borrow;
use std::collections::{BTreeMap, BTreeSet};
use rand::Rng;
use std::collections::BTreeMap;
use std::rc::Rc;

use clvmr::allocator::Allocator;

use crate::classic::clvm::__type_compatibility__::bi_one;
use crate::classic::clvm_tools::stages::stage_0::{DefaultProgramRunner, TRunProgram};
use crate::compiler::clvm::{convert_from_clvm_rs, run};
use crate::compiler::compiler::DefaultCompilerOpts;
use crate::compiler::comptypes::{
BodyForm, CompileErr, CompilerOpts, DefconstData, DefunData, HelperForm,
};
use crate::compiler::comptypes::CompilerOpts;
use crate::compiler::dialect::AcceptedDialect;
use crate::compiler::frontend::compile_helperform;
use crate::compiler::fuzz::{ExprModifier, FuzzGenerator, FuzzTypeParams, Rule};
use crate::compiler::prims::primquote;
use crate::compiler::sexp::{
decode_string, parse_sexp, AtomValue, NodeSel, SExp, SelectNode, ThisNode,
};
use crate::compiler::fuzz::{FuzzTypeParams, Rule};
use crate::compiler::sexp::{AtomValue, NodeSel, SExp, SelectNode, ThisNode};
use crate::compiler::srcloc::Srcloc;

use crate::tests::compiler::fuzz::{
compose_sexp, perform_compile_of_file, simple_run, simple_seeded_rng, GenError,
HasVariableStore, PropertyTest, PropertyTestRun, PropertyTestState, SupportedOperators,
ValueSpecification,
compose_sexp, simple_seeded_rng, GenError, HasVariableStore, PropertyTest, PropertyTestRun,
PropertyTestState, SupportedOperators, ValueSpecification,
};

struct TrickyAssignExpectation {
Expand Down Expand Up @@ -82,8 +68,8 @@ impl Rule<FuzzT> for TestTrickyAssignFuzzTopRule {
state: &mut TrickyAssignExpectation,
tag: &Vec<u8>,
idx: usize,
terminate: bool,
heritage: &[Rc<SExp>],
_terminate: bool,
_heritage: &[Rc<SExp>],
) -> Result<Option<Rc<SExp>>, GenError> {
if tag != b"top" {
return Ok(None);
Expand All @@ -102,8 +88,8 @@ impl Rule<FuzzT> for TestTrickyAssignFuzzTestFormRule {
state: &mut TrickyAssignExpectation,
tag: &Vec<u8>,
idx: usize,
terminate: bool,
heritage: &[Rc<SExp>],
_terminate: bool,
_heritage: &[Rc<SExp>],
) -> Result<Option<Rc<SExp>>, GenError> {
if tag != b"assign-test-form" {
return Ok(None);
Expand All @@ -129,7 +115,7 @@ impl Rule<FuzzT> for TestTrickyAssignFuzzTestFormRule {
}

fn find_var_name_in_heritage(heritage: &[Rc<SExp>]) -> Vec<u8> {
let NodeSel::Cons((varloc, varname), _) = NodeSel::Cons(AtomValue::Here(()), ThisNode)
let NodeSel::Cons((_varloc, varname), _) = NodeSel::Cons(AtomValue::Here(()), ThisNode)
.select_nodes(heritage[heritage.len() - 2].clone())
.unwrap();
varname.clone()
Expand All @@ -143,8 +129,8 @@ impl Rule<FuzzT> for TestTrickyAssignVarDefConstantRule {
&self,
state: &mut TrickyAssignExpectation,
tag: &Vec<u8>,
idx: usize,
terminate: bool,
_idx: usize,
_terminate: bool,
heritage: &[Rc<SExp>],
) -> Result<Option<Rc<SExp>>, GenError> {
if tag != b"vardef" || heritage.len() < 3 {
Expand All @@ -169,8 +155,8 @@ impl Rule<FuzzT> for TestTrickyAssignVarDefBinopRule {
&self,
state: &mut TrickyAssignExpectation,
tag: &Vec<u8>,
idx: usize,
terminate: bool,
_idx: usize,
_terminate: bool,
heritage: &[Rc<SExp>],
) -> Result<Option<Rc<SExp>>, GenError> {
if tag != b"vardef" || state.var_defs.is_empty() || heritage.len() < 3 {
Expand Down Expand Up @@ -201,9 +187,9 @@ impl Rule<FuzzT> for TestTrickyAssignFinalExpr {
&self,
state: &mut TrickyAssignExpectation,
tag: &Vec<u8>,
idx: usize,
terminate: bool,
heritage: &[Rc<SExp>],
_idx: usize,
_terminate: bool,
_heritage: &[Rc<SExp>],
) -> Result<Option<Rc<SExp>>, GenError> {
if tag != b"final-expr" || state.var_defs.is_empty() {
return Ok(None);
Expand All @@ -229,9 +215,9 @@ impl Rule<FuzzT> for TestTrickyAssignFinalBinopRule {
&self,
state: &mut TrickyAssignExpectation,
tag: &Vec<u8>,
idx: usize,
terminate: bool,
heritage: &[Rc<SExp>],
_idx: usize,
_terminate: bool,
_heritage: &[Rc<SExp>],
) -> Result<Option<Rc<SExp>>, GenError> {
if tag != b"final-expr" || state.var_defs.is_empty() {
return Ok(None);
Expand All @@ -254,7 +240,7 @@ impl Rule<FuzzT> for TestTrickyAssignFinalBinopRule {
}

impl PropertyTestState<FuzzT> for TrickyAssignExpectation {
fn new_state<R: Rng>(r: &mut R) -> Self {
fn new_state<R: Rng>(_r: &mut R) -> Self {
let opts: Rc<dyn CompilerOpts> = Rc::new(DefaultCompilerOpts::new("*test*"));
TrickyAssignExpectation::new(
opts.set_dialect(AcceptedDialect {
Expand Down Expand Up @@ -282,7 +268,7 @@ fn test_property_fuzz_cse_binding() {
let mut rng = simple_seeded_rng(0x02020202);
let test = PropertyTest {
run_times: 500,
run_cutoff: 100,
run_cutoff: 300,
run_expansion: 20,

top_node: compose_sexp(srcloc.clone(), "${0:top}"),
Expand Down
Loading

0 comments on commit 6627fad

Please sign in to comment.