Skip to content

Commit

Permalink
feat(attributes): enable custom attributes (#2395)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Aug 22, 2023
1 parent d173a4e commit 179611b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
5 changes: 4 additions & 1 deletion crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/ast/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl From<FunctionDefinition> 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 }
Expand Down
12 changes: 12 additions & 0 deletions crates/noirc_frontend/src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ 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("custom(hello)".to_string().into()))
);
}

#[test]
fn test_custom_gate_syntax() {
let input = "#[foreign(sha256)]#[foreign(blake2s)]#[builtin(sum)]";
Expand Down
8 changes: 6 additions & 2 deletions crates/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ pub enum Attribute {
Oracle(String),
Deprecated(Option<String>),
Test,
Custom(String),
}

impl fmt::Display for Attribute {
Expand All @@ -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}]"),
}
}
}
Expand Down Expand Up @@ -390,8 +392,9 @@ impl Attribute {
Attribute::Deprecated(name.trim_matches('"').to_string().into())
}
["test"] => Attribute::Test,
_ => {
return Err(LexerErrorKind::MalformedFuncAttribute { span, found: word.to_owned() })
tokens => {
tokens.iter().try_for_each(|token| validate(token))?;
Attribute::Custom(word.to_owned())
}
};

Expand Down Expand Up @@ -429,6 +432,7 @@ impl AsRef<str> for Attribute {
Attribute::Oracle(string) => string,
Attribute::Deprecated(Some(string)) => string,
Attribute::Test | Attribute::Deprecated(None) => "",
Attribute::Custom(string) => string,
}
}
}
Expand Down

0 comments on commit 179611b

Please sign in to comment.