From b6da42baf2b9291cb341f865c348233f4edd6018 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Fri, 7 Jun 2024 11:32:05 +0200 Subject: [PATCH] Bump 'is_validator_param' up from 'ArgName' to '...Arg' There's no reasons for this to be a property of only ArgName::Named to begin with. And now, with the extra indirection introduced for arg_name, it may leads to subtle issues when patterns args are used in validators. --- crates/aiken-lang/src/ast.rs | 6 +-- crates/aiken-lang/src/builtins.rs | 13 ++--- crates/aiken-lang/src/expr.rs | 3 +- .../src/parser/definition/function.rs | 9 ++-- .../snapshots/def_invalid_property_test.snap | 4 +- .../snapshots/def_property_test.snap | 2 +- .../def_property_test_annotated_fuzzer.snap | 2 +- .../snapshots/double_validator.snap | 10 ++-- .../definition/snapshots/validator.snap | 6 +-- .../aiken-lang/src/parser/definition/test.rs | 12 ++--- .../src/parser/expr/anonymous_binop.rs | 4 +- .../src/parser/expr/anonymous_function.rs | 2 +- .../snapshots/anonymous_function_basic.snap | 2 +- .../expr/snapshots/first_class_binop.snap | 52 +++++++++---------- .../expr/snapshots/function_invoke.snap | 4 +- crates/aiken-lang/src/tipo/expr.rs | 11 ++-- crates/aiken-lang/src/tipo/infer.rs | 3 +- 17 files changed, 69 insertions(+), 76 deletions(-) diff --git a/crates/aiken-lang/src/ast.rs b/crates/aiken-lang/src/ast.rs index 1e03e402..d83211c1 100644 --- a/crates/aiken-lang/src/ast.rs +++ b/crates/aiken-lang/src/ast.rs @@ -803,6 +803,7 @@ pub struct UntypedArg { pub location: Span, pub annotation: Option, pub doc: Option, + pub is_validator_param: bool, } impl UntypedArg { @@ -818,8 +819,6 @@ impl UntypedArg { label: name.clone(), name, location: self.location, - // TODO: This should likely be moved up inside 'UntypedArg'. - is_validator_param: false, } } } @@ -831,6 +830,7 @@ impl UntypedArg { arg_name: self.arg_name(ix), location: self.location, annotation: self.annotation, + is_validator_param: self.is_validator_param, doc: self.doc, } } @@ -842,6 +842,7 @@ pub struct TypedArg { pub location: Span, pub annotation: Option, pub doc: Option, + pub is_validator_param: bool, pub tipo: Rc, } @@ -928,7 +929,6 @@ pub enum ArgName { name: String, label: String, location: Span, - is_validator_param: bool, }, } diff --git a/crates/aiken-lang/src/builtins.rs b/crates/aiken-lang/src/builtins.rs index 4a2b379e..0975965c 100644 --- a/crates/aiken-lang/src/builtins.rs +++ b/crates/aiken-lang/src/builtins.rs @@ -937,8 +937,8 @@ pub fn prelude_functions(id_gen: &IdGenerator) -> IndexMap IndexMap IndexMap IndexMap IndexMap IndexMap IndexMap impl Parser name} .then(select! {Token::Name {name} => name}) - .map_with_span(move |(label, name), span| ast::ArgName::Named { + .map_with_span(|(label, name), span| ast::ArgName::Named { label, name, location: span, - is_validator_param, }), - select! {Token::Name {name} => name}.map_with_span(move |name, span| ast::ArgName::Named { + select! {Token::Name {name} => name}.map_with_span(|name, span| ast::ArgName::Named { label: name.clone(), name, location: span, - is_validator_param, }), )) .then(just(Token::Colon).ignore_then(annotation()).or_not()) - .map_with_span(|(arg_name, annotation), span| ast::UntypedArg { + .map_with_span(move |(arg_name, annotation), span| ast::UntypedArg { location: span, annotation, doc: None, + is_validator_param, by: ByName(arg_name), }) } diff --git a/crates/aiken-lang/src/parser/definition/snapshots/def_invalid_property_test.snap b/crates/aiken-lang/src/parser/definition/snapshots/def_invalid_property_test.snap index f6416221..b7b65666 100644 --- a/crates/aiken-lang/src/parser/definition/snapshots/def_invalid_property_test.snap +++ b/crates/aiken-lang/src/parser/definition/snapshots/def_invalid_property_test.snap @@ -12,12 +12,12 @@ Test( name: "x", label: "x", location: 9..10, - is_validator_param: false, }, ), location: 9..10, annotation: None, doc: None, + is_validator_param: false, }, via: Var { location: 15..16, @@ -31,12 +31,12 @@ Test( name: "y", label: "y", location: 18..19, - is_validator_param: false, }, ), location: 18..19, annotation: None, doc: None, + is_validator_param: false, }, via: Var { location: 24..25, diff --git a/crates/aiken-lang/src/parser/definition/snapshots/def_property_test.snap b/crates/aiken-lang/src/parser/definition/snapshots/def_property_test.snap index 67658145..f2ba0716 100644 --- a/crates/aiken-lang/src/parser/definition/snapshots/def_property_test.snap +++ b/crates/aiken-lang/src/parser/definition/snapshots/def_property_test.snap @@ -12,12 +12,12 @@ Test( name: "x", label: "x", location: 9..10, - is_validator_param: false, }, ), location: 9..10, annotation: None, doc: None, + is_validator_param: false, }, via: FieldAccess { location: 15..27, diff --git a/crates/aiken-lang/src/parser/definition/snapshots/def_property_test_annotated_fuzzer.snap b/crates/aiken-lang/src/parser/definition/snapshots/def_property_test_annotated_fuzzer.snap index e9f10a97..721edd75 100644 --- a/crates/aiken-lang/src/parser/definition/snapshots/def_property_test_annotated_fuzzer.snap +++ b/crates/aiken-lang/src/parser/definition/snapshots/def_property_test_annotated_fuzzer.snap @@ -12,7 +12,6 @@ Test( name: "x", label: "x", location: 9..10, - is_validator_param: false, }, ), location: 9..15, @@ -25,6 +24,7 @@ Test( }, ), doc: None, + is_validator_param: false, }, via: Call { arguments: [], diff --git a/crates/aiken-lang/src/parser/definition/snapshots/double_validator.snap b/crates/aiken-lang/src/parser/definition/snapshots/double_validator.snap index d577d571..5235bbdb 100644 --- a/crates/aiken-lang/src/parser/definition/snapshots/double_validator.snap +++ b/crates/aiken-lang/src/parser/definition/snapshots/double_validator.snap @@ -14,12 +14,12 @@ Validator( name: "datum", label: "datum", location: 21..26, - is_validator_param: false, }, ), location: 21..26, annotation: None, doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -27,12 +27,12 @@ Validator( name: "rdmr", label: "rdmr", location: 28..32, - is_validator_param: false, }, ), location: 28..32, annotation: None, doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -40,12 +40,12 @@ Validator( name: "ctx", label: "ctx", location: 34..37, - is_validator_param: false, }, ), location: 34..37, annotation: None, doc: None, + is_validator_param: false, }, ], body: Var { @@ -70,12 +70,12 @@ Validator( name: "rdmr", label: "rdmr", location: 64..68, - is_validator_param: false, }, ), location: 64..68, annotation: None, doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -83,12 +83,12 @@ Validator( name: "ctx", label: "ctx", location: 70..73, - is_validator_param: false, }, ), location: 70..73, annotation: None, doc: None, + is_validator_param: false, }, ], body: Var { diff --git a/crates/aiken-lang/src/parser/definition/snapshots/validator.snap b/crates/aiken-lang/src/parser/definition/snapshots/validator.snap index 0a91ff1d..fad9a357 100644 --- a/crates/aiken-lang/src/parser/definition/snapshots/validator.snap +++ b/crates/aiken-lang/src/parser/definition/snapshots/validator.snap @@ -14,12 +14,12 @@ Validator( name: "datum", label: "datum", location: 21..26, - is_validator_param: false, }, ), location: 21..26, annotation: None, doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -27,12 +27,12 @@ Validator( name: "rdmr", label: "rdmr", location: 28..32, - is_validator_param: false, }, ), location: 28..32, annotation: None, doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -40,12 +40,12 @@ Validator( name: "ctx", label: "ctx", location: 34..37, - is_validator_param: false, }, ), location: 34..37, annotation: None, doc: None, + is_validator_param: false, }, ], body: Var { diff --git a/crates/aiken-lang/src/parser/definition/test.rs b/crates/aiken-lang/src/parser/definition/test.rs index 61123ca3..7e2cbec1 100644 --- a/crates/aiken-lang/src/parser/definition/test.rs +++ b/crates/aiken-lang/src/parser/definition/test.rs @@ -60,13 +60,10 @@ pub fn via() -> impl Parser { location: span, } }), - select! {Token::Name {name} => name}.map_with_span(move |name, location| { - ast::ArgName::Named { - label: name.clone(), - name, - location, - is_validator_param: false, - } + select! {Token::Name {name} => name}.map_with_span(|name, location| ast::ArgName::Named { + label: name.clone(), + name, + location, }), )) .then(just(Token::Colon).ignore_then(annotation()).or_not()) @@ -79,6 +76,7 @@ pub fn via() -> impl Parser { annotation, location, doc: None, + is_validator_param: false, }, via, }) diff --git a/crates/aiken-lang/src/parser/expr/anonymous_binop.rs b/crates/aiken-lang/src/parser/expr/anonymous_binop.rs index 146e1cba..f4835eda 100644 --- a/crates/aiken-lang/src/parser/expr/anonymous_binop.rs +++ b/crates/aiken-lang/src/parser/expr/anonymous_binop.rs @@ -45,8 +45,8 @@ pub fn parser() -> impl Parser { name: "left".to_string(), label: "left".to_string(), location, - is_validator_param: false, }), + is_validator_param: false, annotation: arg_annotation.clone(), doc: None, location, @@ -56,8 +56,8 @@ pub fn parser() -> impl Parser { name: "right".to_string(), label: "right".to_string(), location, - is_validator_param: false, }), + is_validator_param: false, annotation: arg_annotation, doc: None, location, diff --git a/crates/aiken-lang/src/parser/expr/anonymous_function.rs b/crates/aiken-lang/src/parser/expr/anonymous_function.rs index 1963a584..2d092ce4 100644 --- a/crates/aiken-lang/src/parser/expr/anonymous_function.rs +++ b/crates/aiken-lang/src/parser/expr/anonymous_function.rs @@ -42,11 +42,11 @@ pub fn params() -> impl Parser { label: name.clone(), name, location: span, - is_validator_param: false, }), )) .then(just(Token::Colon).ignore_then(annotation()).or_not()) .map_with_span(|(arg_name, annotation), span| ast::UntypedArg { + is_validator_param: false, location: span, annotation, doc: None, diff --git a/crates/aiken-lang/src/parser/expr/snapshots/anonymous_function_basic.snap b/crates/aiken-lang/src/parser/expr/snapshots/anonymous_function_basic.snap index 95e04ce0..e67b39f1 100644 --- a/crates/aiken-lang/src/parser/expr/snapshots/anonymous_function_basic.snap +++ b/crates/aiken-lang/src/parser/expr/snapshots/anonymous_function_basic.snap @@ -12,7 +12,6 @@ Fn { name: "a", label: "a", location: 4..5, - is_validator_param: false, }, ), location: 4..10, @@ -25,6 +24,7 @@ Fn { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { diff --git a/crates/aiken-lang/src/parser/expr/snapshots/first_class_binop.snap b/crates/aiken-lang/src/parser/expr/snapshots/first_class_binop.snap index fb216f72..8f90a37f 100644 --- a/crates/aiken-lang/src/parser/expr/snapshots/first_class_binop.snap +++ b/crates/aiken-lang/src/parser/expr/snapshots/first_class_binop.snap @@ -30,7 +30,6 @@ Sequence { name: "left", label: "left", location: 16..17, - is_validator_param: false, }, ), location: 16..17, @@ -43,6 +42,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -50,7 +50,6 @@ Sequence { name: "right", label: "right", location: 16..17, - is_validator_param: false, }, ), location: 16..17, @@ -63,6 +62,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -127,7 +127,6 @@ Sequence { name: "left", label: "left", location: 38..40, - is_validator_param: false, }, ), location: 38..40, @@ -140,6 +139,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -147,7 +147,6 @@ Sequence { name: "right", label: "right", location: 38..40, - is_validator_param: false, }, ), location: 38..40, @@ -160,6 +159,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -224,7 +224,6 @@ Sequence { name: "left", label: "left", location: 61..62, - is_validator_param: false, }, ), location: 61..62, @@ -237,6 +236,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -244,7 +244,6 @@ Sequence { name: "right", label: "right", location: 61..62, - is_validator_param: false, }, ), location: 61..62, @@ -257,6 +256,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -321,7 +321,6 @@ Sequence { name: "left", label: "left", location: 83..85, - is_validator_param: false, }, ), location: 83..85, @@ -334,6 +333,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -341,7 +341,6 @@ Sequence { name: "right", label: "right", location: 83..85, - is_validator_param: false, }, ), location: 83..85, @@ -354,6 +353,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -418,12 +418,12 @@ Sequence { name: "left", label: "left", location: 106..108, - is_validator_param: false, }, ), location: 106..108, annotation: None, doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -431,12 +431,12 @@ Sequence { name: "right", label: "right", location: 106..108, - is_validator_param: false, }, ), location: 106..108, annotation: None, doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -501,12 +501,12 @@ Sequence { name: "left", label: "left", location: 129..131, - is_validator_param: false, }, ), location: 129..131, annotation: None, doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -514,12 +514,12 @@ Sequence { name: "right", label: "right", location: 129..131, - is_validator_param: false, }, ), location: 129..131, annotation: None, doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -584,7 +584,6 @@ Sequence { name: "left", label: "left", location: 152..154, - is_validator_param: false, }, ), location: 152..154, @@ -597,6 +596,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -604,7 +604,6 @@ Sequence { name: "right", label: "right", location: 152..154, - is_validator_param: false, }, ), location: 152..154, @@ -617,6 +616,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -681,7 +681,6 @@ Sequence { name: "left", label: "left", location: 175..177, - is_validator_param: false, }, ), location: 175..177, @@ -694,6 +693,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -701,7 +701,6 @@ Sequence { name: "right", label: "right", location: 175..177, - is_validator_param: false, }, ), location: 175..177, @@ -714,6 +713,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -778,7 +778,6 @@ Sequence { name: "left", label: "left", location: 198..199, - is_validator_param: false, }, ), location: 198..199, @@ -791,6 +790,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -798,7 +798,6 @@ Sequence { name: "right", label: "right", location: 198..199, - is_validator_param: false, }, ), location: 198..199, @@ -811,6 +810,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -875,7 +875,6 @@ Sequence { name: "left", label: "left", location: 220..221, - is_validator_param: false, }, ), location: 220..221, @@ -888,6 +887,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -895,7 +895,6 @@ Sequence { name: "right", label: "right", location: 220..221, - is_validator_param: false, }, ), location: 220..221, @@ -908,6 +907,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -972,7 +972,6 @@ Sequence { name: "left", label: "left", location: 242..243, - is_validator_param: false, }, ), location: 242..243, @@ -985,6 +984,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -992,7 +992,6 @@ Sequence { name: "right", label: "right", location: 242..243, - is_validator_param: false, }, ), location: 242..243, @@ -1005,6 +1004,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -1069,7 +1069,6 @@ Sequence { name: "left", label: "left", location: 264..265, - is_validator_param: false, }, ), location: 264..265, @@ -1082,6 +1081,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -1089,7 +1089,6 @@ Sequence { name: "right", label: "right", location: 264..265, - is_validator_param: false, }, ), location: 264..265, @@ -1102,6 +1101,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { @@ -1166,7 +1166,6 @@ Sequence { name: "left", label: "left", location: 286..287, - is_validator_param: false, }, ), location: 286..287, @@ -1179,6 +1178,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, UntypedArg { by: ByName( @@ -1186,7 +1186,6 @@ Sequence { name: "right", label: "right", location: 286..287, - is_validator_param: false, }, ), location: 286..287, @@ -1199,6 +1198,7 @@ Sequence { }, ), doc: None, + is_validator_param: false, }, ], body: BinOp { diff --git a/crates/aiken-lang/src/parser/expr/snapshots/function_invoke.snap b/crates/aiken-lang/src/parser/expr/snapshots/function_invoke.snap index 77ab2d30..d3dc4cec 100644 --- a/crates/aiken-lang/src/parser/expr/snapshots/function_invoke.snap +++ b/crates/aiken-lang/src/parser/expr/snapshots/function_invoke.snap @@ -53,12 +53,12 @@ Sequence { name: "_capture__0", label: "_capture__0", location: 0..0, - is_validator_param: false, }, ), location: 0..0, annotation: None, doc: None, + is_validator_param: false, }, ], body: Call { @@ -84,12 +84,12 @@ Sequence { name: "y", label: "y", location: 52..53, - is_validator_param: false, }, ), location: 52..53, annotation: None, doc: None, + is_validator_param: false, }, ], body: BinOp { diff --git a/crates/aiken-lang/src/tipo/expr.rs b/crates/aiken-lang/src/tipo/expr.rs index 920022d3..3a29539a 100644 --- a/crates/aiken-lang/src/tipo/expr.rs +++ b/crates/aiken-lang/src/tipo/expr.rs @@ -1085,6 +1085,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> { annotation, location, doc, + is_validator_param, } = untyped_arg; let tipo = annotation @@ -1106,6 +1107,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> { location, annotation, tipo, + is_validator_param, doc, }) } @@ -1737,12 +1739,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> { for arg in &args { match &arg.arg_name { - ArgName::Named { - name, - is_validator_param, - location, - .. - } if !is_validator_param => { + ArgName::Named { name, location, .. } if !arg.is_validator_param => { if let Some(duplicate_location) = argument_names.insert(name, location) { return Err(Error::DuplicateArgument { location: *location, @@ -1969,7 +1966,6 @@ impl<'a, 'b> ExprTyper<'a, 'b> { label: name.clone(), name, location: var_location, - is_validator_param: false, }; names.push((name, assignment_pattern_location, annotation)); @@ -1993,7 +1989,6 @@ impl<'a, 'b> ExprTyper<'a, 'b> { label: name.clone(), name: name.clone(), location: pattern.location(), - is_validator_param: false, }; let pattern_is_var = pattern.is_var(); diff --git a/crates/aiken-lang/src/tipo/infer.rs b/crates/aiken-lang/src/tipo/infer.rs index 0309e148..075aa774 100644 --- a/crates/aiken-lang/src/tipo/infer.rs +++ b/crates/aiken-lang/src/tipo/infer.rs @@ -206,10 +206,9 @@ fn infer_definition( match &arg.arg_name(ix) { ArgName::Named { name, - is_validator_param, label: _, location: _, - } if *is_validator_param => { + } if arg.is_validator_param => { environment.insert_variable( name.to_string(), ValueConstructorVariant::LocalVariable {