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: add Expr::as_assert #5857

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use crate::{
ast::{
ArrayLiteral, BlockExpression, Expression, ExpressionKind, FunctionKind,
ArrayLiteral, BlockExpression, ConstrainKind, Expression, ExpressionKind, FunctionKind,
FunctionReturnType, IntegerBitSize, LValue, Literal, Statement, StatementKind, UnaryOp,
UnresolvedType, UnresolvedTypeData, Visibility,
},
Expand Down Expand Up @@ -54,6 +54,7 @@
"assert_constant" => Ok(Value::Bool(true)),
"as_slice" => as_slice(interner, arguments, location),
"expr_as_array" => expr_as_array(interner, arguments, return_type, location),
"expr_as_assert" => expr_as_assert(interner, arguments, return_type, location),
"expr_as_assign" => expr_as_assign(interner, arguments, return_type, location),
"expr_as_binary_op" => expr_as_binary_op(interner, arguments, return_type, location),
"expr_as_block" => expr_as_block(interner, arguments, return_type, location),
Expand Down Expand Up @@ -425,7 +426,7 @@
let argument = check_one_argument(arguments, location)?;
let typ = parse(argument, parser::parse_type(), "a type")?;
let typ =
interpreter.elaborate_item(interpreter.current_function, |elab| elab.resolve_type(typ));

Check warning on line 429 in compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elab)

Check warning on line 429 in compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elab)
Ok(Value::Type(typ))
}

Expand Down Expand Up @@ -863,6 +864,38 @@
})
}

// fn as_assert(self) -> Option<(Expr, Option<Expr>)>
fn expr_as_assert(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
expr_as(interner, arguments, return_type.clone(), location, |expr| {
if let ExprValue::Statement(StatementKind::Constrain(constrain)) = expr {
if constrain.2 == ConstrainKind::Assert {
let predicate = Value::expression(constrain.0.kind);

let option_type = extract_option_generic_type(return_type);
let Type::Tuple(mut tuple_types) = option_type else {
panic!("Expected the return type option generic arg to be a tuple");
};
assert_eq!(tuple_types.len(), 2);

let option_type = tuple_types.pop().unwrap();
let message = constrain.1.map(|message| Value::expression(message.kind));
let message = option(option_type, message).ok()?;

Some(Value::Tuple(vec![predicate, message]))
} else {
None
}
} else {
None
}
})
}

// fn as_assign(self) -> Option<(Expr, Expr)>
fn expr_as_assign(
interner: &NodeInterner,
Expand Down
12 changes: 9 additions & 3 deletions docs/docs/noir/standard_library/meta/expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ title: Expr

If this expression is an array, this returns a slice of each element in the array.

### as_assert

#include_code as_assert noir_stdlib/src/meta/expr.nr rust

If this expression is an assert, this returns the assert expression and the optional message.

### as_assign

#include_code as_assign noir_stdlib/src/meta/expr.nr rust
Expand Down Expand Up @@ -162,16 +168,16 @@ comptime {

`true` if this expression is `continue`.

### mutate
### modify

#include_code mutate noir_stdlib/src/meta/expr.nr rust
#include_code modify noir_stdlib/src/meta/expr.nr rust

Applies a mapping function to this expression and to all of its sub-expressions.
`f` will be applied to each sub-expression first, then applied to the expression itself.

This happens recursively for every expression within `self`.

For example, calling `mutate` on `(&[1], &[2, 3])` with an `f` that returns `Option::some`
For example, calling `modify` on `(&[1], &[2, 3])` with an `f` that returns `Option::some`
for expressions that are integers, doubling them, would return `(&[2], &[4, 6])`.

### quoted
Expand Down
Loading
Loading