From 5cb1e230082339d4c62f49eeb05dbfaba1fa6940 Mon Sep 17 00:00:00 2001 From: rvcas Date: Thu, 4 Apr 2024 17:02:29 -0400 Subject: [PATCH] fix: program generate should only run after params and args are validated --- ...lueprint__validator__tests__free_vars.snap | 24 ++++ .../aiken-project/src/blueprint/validator.rs | 131 ++++++++++-------- 2 files changed, 100 insertions(+), 55 deletions(-) create mode 100644 crates/aiken-project/src/blueprint/snapshots/aiken_project__blueprint__validator__tests__free_vars.snap diff --git a/crates/aiken-project/src/blueprint/snapshots/aiken_project__blueprint__validator__tests__free_vars.snap b/crates/aiken-project/src/blueprint/snapshots/aiken_project__blueprint__validator__tests__free_vars.snap new file mode 100644 index 00000000..cdde7523 --- /dev/null +++ b/crates/aiken-project/src/blueprint/snapshots/aiken_project__blueprint__validator__tests__free_vars.snap @@ -0,0 +1,24 @@ +--- +source: crates/aiken-project/src/blueprint/validator.rs +description: "Code:\n\nvalidator {\n fn generics(redeemer: a, ctx: Void) {\n True\n }\n}\n" +--- +Schema { + error: Error { + context: FreeTypeVariable, + breadcrumbs: [ + Var { + tipo: RefCell { + value: Generic { + id: 33, + }, + }, + alias: None, + }, + ], + }, + location: 26..37, + source_code: NamedSource { + name: "", + source: "", + , +} diff --git a/crates/aiken-project/src/blueprint/validator.rs b/crates/aiken-project/src/blueprint/validator.rs index 99a8cfc7..db635c18 100644 --- a/crates/aiken-project/src/blueprint/validator.rs +++ b/crates/aiken-project/src/blueprint/validator.rs @@ -1,6 +1,7 @@ use super::{ definitions::Definitions, error::Error, + memo_program::MemoProgram, parameter::Parameter, schema::{Annotated, Schema}, }; @@ -49,27 +50,29 @@ impl Validator { module: &CheckedModule, def: &TypedValidator, ) -> Vec> { - let program = generator.generate(def, &module.name).to_debruijn().unwrap(); - let is_multi_validator = def.other_fun.is_some(); + let mut program = MemoProgram::new(); + let mut validators = vec![Validator::create_validator_blueprint( + generator, modules, module, - &program, - &def.params, + def, &def.fun, is_multi_validator, + &mut program, )]; if let Some(ref other_func) = def.other_fun { validators.push(Validator::create_validator_blueprint( + generator, modules, module, - &program, - &def.params, + def, other_func, is_multi_validator, + &mut program, )); } @@ -77,21 +80,24 @@ impl Validator { } fn create_validator_blueprint( + generator: &mut CodeGenerator, modules: &CheckedModules, module: &CheckedModule, - program: &Program, - params: &[TypedArg], + def: &TypedValidator, func: &TypedFunction, is_multi_validator: bool, + program: &mut MemoProgram, ) -> Result { let mut args = func.arguments.iter().rev(); let (_, redeemer, datum) = (args.next(), args.next().unwrap(), args.next()); let mut definitions = Definitions::new(); - let parameters = params + let parameters = def + .params .iter() .map(|param| { + dbg!(¶m); Annotated::from_type( modules.into(), tipo_or_annotation(module, param), @@ -112,56 +118,58 @@ impl Validator { }) .collect::>()?; + let datum = datum + .map(|datum| { + Annotated::from_type( + modules.into(), + tipo_or_annotation(module, datum), + &mut definitions, + ) + .map_err(|error| Error::Schema { + error, + location: datum.location, + source_code: NamedSource::new( + module.input_path.display().to_string(), + module.code.clone(), + ), + }) + }) + .transpose()? + .map(|schema| Parameter { + title: datum.map(|datum| datum.arg_name.get_label()), + schema, + }); + + let redeemer = Annotated::from_type( + modules.into(), + tipo_or_annotation(module, redeemer), + &mut definitions, + ) + .map_err(|error| Error::Schema { + error, + location: redeemer.location, + source_code: NamedSource::new( + module.input_path.display().to_string(), + module.code.clone(), + ), + }) + .map(|schema| Parameter { + title: Some(redeemer.arg_name.get_label()), + schema: match datum { + Some(..) if is_multi_validator => { + Annotated::as_wrapped_redeemer(&mut definitions, schema, redeemer.tipo.clone()) + } + _ => schema, + }, + })?; + Ok(Validator { title: format!("{}.{}", &module.name, &func.name), description: func.doc.clone(), parameters, - datum: datum - .map(|datum| { - Annotated::from_type( - modules.into(), - tipo_or_annotation(module, datum), - &mut definitions, - ) - .map_err(|error| Error::Schema { - error, - location: datum.location, - source_code: NamedSource::new( - module.input_path.display().to_string(), - module.code.clone(), - ), - }) - }) - .transpose()? - .map(|schema| Parameter { - title: datum.map(|datum| datum.arg_name.get_label()), - schema, - }), - redeemer: Annotated::from_type( - modules.into(), - tipo_or_annotation(module, redeemer), - &mut definitions, - ) - .map_err(|error| Error::Schema { - error, - location: redeemer.location, - source_code: NamedSource::new( - module.input_path.display().to_string(), - module.code.clone(), - ), - }) - .map(|schema| Parameter { - title: Some(redeemer.arg_name.get_label()), - schema: match datum { - Some(..) if is_multi_validator => Annotated::as_wrapped_redeemer( - &mut definitions, - schema, - redeemer.tipo.clone(), - ), - _ => schema, - }, - })?, - program: program.clone(), + datum, + redeemer, + program: program.get(generator, def, &module.name), definitions, }) } @@ -474,6 +482,19 @@ mod tests { ); } + #[test] + fn free_vars() { + assert_validator!( + r#" + validator { + fn generics(redeemer: a, ctx: Void) { + True + } + } + "# + ); + } + #[test] fn list_2_tuples_as_map() { assert_validator!(