From 2b6a3e6677a6331129f04cbeb185d5e5eedac806 Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Tue, 22 Aug 2023 10:51:18 +0000 Subject: [PATCH 1/5] feat(attributes): allow for custom attributes --- crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs | 5 ++++- crates/noirc_frontend/src/ast/function.rs | 1 + crates/noirc_frontend/src/lexer/token.rs | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs b/crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs index cc3a7c02a75..1367b1753af 100644 --- a/crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs +++ b/crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs @@ -130,7 +130,10 @@ impl<'a> FunctionContext<'a> { let slice_contents = self.codegen_array(elements, typ[1].clone()); Tree::Branch(vec![slice_length.into(), slice_contents]) } - _ => unreachable!("ICE: array literal type must be an array or a slice, but got {}", array.typ), + _ => unreachable!( + "ICE: array literal type must be an array or a slice, but got {}", + array.typ + ), } } ast::Literal::Integer(value, typ) => { diff --git a/crates/noirc_frontend/src/ast/function.rs b/crates/noirc_frontend/src/ast/function.rs index 2c418dbefd7..377e6aa77ad 100644 --- a/crates/noirc_frontend/src/ast/function.rs +++ b/crates/noirc_frontend/src/ast/function.rs @@ -86,6 +86,7 @@ impl From for NoirFunction { Some(Attribute::Test) => FunctionKind::Normal, Some(Attribute::Oracle(_)) => FunctionKind::Oracle, Some(Attribute::Deprecated(_)) | None => FunctionKind::Normal, + Some(Attribute::Custom(_)) => FunctionKind::Normal, }; NoirFunction { def: fd, kind } diff --git a/crates/noirc_frontend/src/lexer/token.rs b/crates/noirc_frontend/src/lexer/token.rs index 3ef1d2a5dde..cb75c681e8e 100644 --- a/crates/noirc_frontend/src/lexer/token.rs +++ b/crates/noirc_frontend/src/lexer/token.rs @@ -326,6 +326,7 @@ pub enum Attribute { Oracle(String), Deprecated(Option), Test, + Custom(String), } impl fmt::Display for Attribute { @@ -337,6 +338,7 @@ impl fmt::Display for Attribute { Attribute::Test => write!(f, "#[test]"), Attribute::Deprecated(None) => write!(f, "#[deprecated]"), Attribute::Deprecated(Some(ref note)) => write!(f, r#"#[deprecated("{note}")]"#), + Attribute::Custom(ref k) => write!(f, "#[{k}]"), } } } @@ -390,6 +392,10 @@ impl Attribute { Attribute::Deprecated(name.trim_matches('"').to_string().into()) } ["test"] => Attribute::Test, + [name] => { + validate(name)?; + Attribute::Custom(name.to_string()) + } _ => { return Err(LexerErrorKind::MalformedFuncAttribute { span, found: word.to_owned() }) } @@ -429,6 +435,7 @@ impl AsRef for Attribute { Attribute::Oracle(string) => string, Attribute::Deprecated(Some(string)) => string, Attribute::Test | Attribute::Deprecated(None) => "", + Attribute::Custom(string) => string, } } } From 567812e4ca6ea13381fb514f3b8e4f10a7532b24 Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Tue, 22 Aug 2023 10:56:14 +0000 Subject: [PATCH 2/5] test: add lexer test --- crates/noirc_frontend/src/lexer/lexer.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/noirc_frontend/src/lexer/lexer.rs b/crates/noirc_frontend/src/lexer/lexer.rs index 8a98d5bfa3c..12f9a50ef12 100644 --- a/crates/noirc_frontend/src/lexer/lexer.rs +++ b/crates/noirc_frontend/src/lexer/lexer.rs @@ -453,6 +453,15 @@ fn deprecated_attribute_with_note() { assert_eq!(token.token(), &Token::Attribute(Attribute::Deprecated("hello".to_string().into()))); } +#[test] +fn custom_attribute() { + let input = r#"#[custom(hello)]"#; + let mut lexer = Lexer::new(input); + + let token = lexer.next().unwrap().unwrap(); + assert_eq!(token.token(), &Token::Attribute(Attribute::Custom("hello".to_string().into()))); +} + #[test] fn test_custom_gate_syntax() { let input = "#[foreign(sha256)]#[foreign(blake2s)]#[builtin(sum)]"; From 90b93b2e671a43038de7391279de94f82d082ba8 Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:49:20 +0000 Subject: [PATCH 3/5] fix: update parsing to allow for complex patterns --- crates/noirc_frontend/src/lexer/lexer.rs | 5 ++++- crates/noirc_frontend/src/lexer/token.rs | 9 +++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/noirc_frontend/src/lexer/lexer.rs b/crates/noirc_frontend/src/lexer/lexer.rs index 12f9a50ef12..372b3f3e4d1 100644 --- a/crates/noirc_frontend/src/lexer/lexer.rs +++ b/crates/noirc_frontend/src/lexer/lexer.rs @@ -459,7 +459,10 @@ fn custom_attribute() { let mut lexer = Lexer::new(input); let token = lexer.next().unwrap().unwrap(); - assert_eq!(token.token(), &Token::Attribute(Attribute::Custom("hello".to_string().into()))); + assert_eq!( + token.token(), + &Token::Attribute(Attribute::Custom("custom__hello".to_string().into())) + ); } #[test] diff --git a/crates/noirc_frontend/src/lexer/token.rs b/crates/noirc_frontend/src/lexer/token.rs index cb75c681e8e..c4afa74b116 100644 --- a/crates/noirc_frontend/src/lexer/token.rs +++ b/crates/noirc_frontend/src/lexer/token.rs @@ -392,12 +392,9 @@ impl Attribute { Attribute::Deprecated(name.trim_matches('"').to_string().into()) } ["test"] => Attribute::Test, - [name] => { - validate(name)?; - Attribute::Custom(name.to_string()) - } - _ => { - return Err(LexerErrorKind::MalformedFuncAttribute { span, found: word.to_owned() }) + tokens => { + tokens.iter().try_for_each(|token| validate(token))?; + Attribute::Custom(tokens.join("__").to_string()) } }; From 8d0dabf51491c3787d39ed1faac2dfc55ac3801c Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:51:04 +0000 Subject: [PATCH 4/5] chore: lint --- crates/noirc_frontend/src/lexer/token.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/noirc_frontend/src/lexer/token.rs b/crates/noirc_frontend/src/lexer/token.rs index c4afa74b116..f5a72a2feee 100644 --- a/crates/noirc_frontend/src/lexer/token.rs +++ b/crates/noirc_frontend/src/lexer/token.rs @@ -394,7 +394,7 @@ impl Attribute { ["test"] => Attribute::Test, tokens => { tokens.iter().try_for_each(|token| validate(token))?; - Attribute::Custom(tokens.join("__").to_string()) + Attribute::Custom(tokens.join("__")) } }; From fe0d533b6ea65334d0bbef32def52918fc05c8eb Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Tue, 22 Aug 2023 13:21:27 +0000 Subject: [PATCH 5/5] fix: no transformation --- crates/noirc_frontend/src/lexer/lexer.rs | 2 +- crates/noirc_frontend/src/lexer/token.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/noirc_frontend/src/lexer/lexer.rs b/crates/noirc_frontend/src/lexer/lexer.rs index 372b3f3e4d1..d0fa475426a 100644 --- a/crates/noirc_frontend/src/lexer/lexer.rs +++ b/crates/noirc_frontend/src/lexer/lexer.rs @@ -461,7 +461,7 @@ fn custom_attribute() { let token = lexer.next().unwrap().unwrap(); assert_eq!( token.token(), - &Token::Attribute(Attribute::Custom("custom__hello".to_string().into())) + &Token::Attribute(Attribute::Custom("custom(hello)".to_string().into())) ); } diff --git a/crates/noirc_frontend/src/lexer/token.rs b/crates/noirc_frontend/src/lexer/token.rs index f5a72a2feee..6291ac4de12 100644 --- a/crates/noirc_frontend/src/lexer/token.rs +++ b/crates/noirc_frontend/src/lexer/token.rs @@ -394,7 +394,7 @@ impl Attribute { ["test"] => Attribute::Test, tokens => { tokens.iter().try_for_each(|token| validate(token))?; - Attribute::Custom(tokens.join("__")) + Attribute::Custom(word.to_owned()) } };