From b5f27026e2b1bde06b730cdab90a317d2e3a8feb Mon Sep 17 00:00:00 2001 From: rvcas Date: Fri, 29 Mar 2024 11:32:04 -0400 Subject: [PATCH] fix: confusing public validator closes #902 --- crates/aiken-lang/src/format.rs | 4 +- crates/aiken-lang/src/tests/check.rs | 66 +++++++++++++++++++ crates/aiken-lang/src/tests/format.rs | 15 +++++ .../format_double_validator_public.snap | 14 ++++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 crates/aiken-lang/src/tests/snapshots/format_double_validator_public.snap diff --git a/crates/aiken-lang/src/format.rs b/crates/aiken-lang/src/format.rs index 3d7d18cb..9fa8c67b 100644 --- a/crates/aiken-lang/src/format.rs +++ b/crates/aiken-lang/src/format.rs @@ -583,7 +583,7 @@ impl<'comments> Formatter<'comments> { let fun_doc_comments = self.doc_comments(fun.location.start); let first_fn = self .definition_fn( - &false, + &fun.public, &fun.name, &fun.arguments, &fun.return_annotation, @@ -601,7 +601,7 @@ impl<'comments> Formatter<'comments> { let other_fn = self .definition_fn( - &false, + &other.public, &other.name, &other.arguments, &other.return_annotation, diff --git a/crates/aiken-lang/src/tests/check.rs b/crates/aiken-lang/src/tests/check.rs index b51c2057..865f83f0 100644 --- a/crates/aiken-lang/src/tests/check.rs +++ b/crates/aiken-lang/src/tests/check.rs @@ -2208,3 +2208,69 @@ fn allow_discard_for_backpassing_args() { assert_eq!(warnings.len(), 0); } + +#[test] +fn validator_private_type_leak() { + let source_code = r#" + type Datum { + foo: Int, + } + + type Redeemer { + bar: Int, + } + + validator { + pub fn bar(datum: Datum, redeemer: Redeemer, _ctx) { + datum.foo == redeemer.bar + } + } + "#; + + assert!(matches!( + check_validator(parse(source_code)), + Err((_, Error::PrivateTypeLeak { .. })) + )) +} + +#[test] +fn validator_public() { + let source_code = r#" + pub type Datum { + foo: Int, + } + + pub type Redeemer { + bar: Int, + } + + validator { + pub fn bar(datum: Datum, redeemer: Redeemer, _ctx) { + datum.foo == redeemer.bar + } + } + "#; + + assert!(check_validator(parse(source_code)).is_ok()) +} + +#[test] +fn validator_private_everything() { + let source_code = r#" + type Datum { + foo: Int, + } + + type Redeemer { + bar: Int, + } + + validator { + fn bar(datum: Datum, redeemer: Redeemer, _ctx) { + datum.foo == redeemer.bar + } + } + "#; + + assert!(check_validator(parse(source_code)).is_ok()) +} diff --git a/crates/aiken-lang/src/tests/format.rs b/crates/aiken-lang/src/tests/format.rs index d6290c25..1649a7e5 100644 --- a/crates/aiken-lang/src/tests/format.rs +++ b/crates/aiken-lang/src/tests/format.rs @@ -185,6 +185,21 @@ fn format_double_validator() { ); } +#[test] +fn format_double_validator_public() { + assert_format!( + r#" + validator ( param1 : ByteArray ) { + pub fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool { + True + } + /// This is bar + pub fn bar(r: Redeemer, ctx : ScriptContext ) -> Bool { True } + } + "# + ); +} + #[test] fn format_when() { assert_format!( diff --git a/crates/aiken-lang/src/tests/snapshots/format_double_validator_public.snap b/crates/aiken-lang/src/tests/snapshots/format_double_validator_public.snap new file mode 100644 index 00000000..7582c958 --- /dev/null +++ b/crates/aiken-lang/src/tests/snapshots/format_double_validator_public.snap @@ -0,0 +1,14 @@ +--- +source: crates/aiken-lang/src/tests/format.rs +description: "Code:\n\n validator ( param1 : ByteArray ) {\n pub fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {\n True\n }\n /// This is bar\npub fn bar(r: Redeemer, ctx : ScriptContext ) -> Bool { True }\n }\n" +--- +validator(param1: ByteArray) { + pub fn foo(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool { + True + } + + /// This is bar + pub fn bar(r: Redeemer, ctx: ScriptContext) -> Bool { + True + } +}