Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(noir): added assert keyword #1227

Merged
merged 9 commits into from
Apr 26, 2023
5 changes: 5 additions & 0 deletions crates/nargo_cli/tests/test_data/assert/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
1 change: 1 addition & 0 deletions crates/nargo_cli/tests/test_data/assert/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "1"
3 changes: 3 additions & 0 deletions crates/nargo_cli/tests/test_data/assert/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main(x: Field) {
assert x == 1;
}
8 changes: 8 additions & 0 deletions crates/noirc_frontend/src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ fn test_basic_language_syntax() {
x * y;
};
constrain mul(five, ten) == 50;
assert ten + five == 15;
";

let expected = vec![
Expand Down Expand Up @@ -601,6 +602,13 @@ fn test_basic_language_syntax() {
Token::Equal,
Token::Int(50_i128.into()),
Token::Semicolon,
Token::Keyword(Keyword::Assert),
Token::Ident("ten".to_string()),
Token::Plus,
Token::Ident("five".to_string()),
Token::Equal,
Token::Int(15_i128.into()),
Token::Semicolon,
Token::EOF,
];
let mut lexer = Lexer::new(input);
Expand Down
3 changes: 3 additions & 0 deletions crates/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ impl AsRef<str> for Attribute {
#[cfg_attr(test, derive(strum_macros::EnumIter))]
pub enum Keyword {
As,
Assert,
Bool,
Char,
CompTime,
Expand Down Expand Up @@ -447,6 +448,7 @@ impl fmt::Display for Keyword {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Keyword::As => write!(f, "as"),
Keyword::Assert => write!(f, "assert"),
Keyword::Bool => write!(f, "bool"),
Keyword::Char => write!(f, "char"),
Keyword::CompTime => write!(f, "comptime"),
Expand Down Expand Up @@ -483,6 +485,7 @@ impl Keyword {
pub(crate) fn lookup_keyword(word: &str) -> Option<Token> {
let keyword = match word {
"as" => Keyword::As,
"assert" => Keyword::Assert,
"bool" => Keyword::Bool,
"char" => Keyword::Char,
"comptime" => Keyword::CompTime,
Expand Down
6 changes: 5 additions & 1 deletion crates/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ fn constrain<'a, P>(expr_parser: P) -> impl NoirParser<Statement> + 'a
where
P: ExprParser + 'a,
{
ignore_then_commit(keyword(Keyword::Constrain).labelled("statement"), expr_parser)
ignore_then_commit(choice((keyword(Keyword::Assert), keyword(Keyword::Constrain))).labelled("statement"), expr_parser)
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
.map(|expr| Statement::Constrain(ConstrainStatement(expr)))
}

Expand Down Expand Up @@ -1176,6 +1176,8 @@ mod test {
#[test]
fn parse_constrain() {
parse_with(constrain(expression()), "constrain x == y").unwrap();
// Assert is syntax sugar for constrain
parse_with(constrain(expression()), "assert x == y").unwrap();

// Currently we disallow constrain statements where the outer infix operator
// produces a value. This would require an implicit `==` which
Expand Down Expand Up @@ -1468,7 +1470,9 @@ mod test {
("let", 3, "let $error: unspecified = Error"),
("foo = one two three", 1, "foo = plain::one"),
("constrain", 1, "constrain Error"),
("assert", 1, "constrain Error"),
("constrain x ==", 1, "constrain (plain::x == Error)"),
("assert x ==", 1, "constrain (plain::x == Error)"),
];

let show_errors = |v| vecmap(v, ToString::to_string).join("\n");
Expand Down