Skip to main content
Version: dev

Expr

std::meta::expr contains methods on the built-in Expr type for quoted, syntactically valid expressions.

Methods

as_array

as_array
pub comptime fn as_array(self) -> Option<[Expr]> {}

Source code: noir_stdlib/src/meta/expr.nr#L10-L12

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

as_assert

as_assert
pub comptime fn as_assert(self) -> Option<(Expr, Option<Expr>)> {}

Source code: noir_stdlib/src/meta/expr.nr#L16-L18

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

as_assert_eq

as_assert_eq
pub comptime fn as_assert_eq(self) -> Option<(Expr, Expr, Option<Expr>)> {}

Source code: noir_stdlib/src/meta/expr.nr#L23-L25

If this expression is an assert_eq, this returns the left-hand-side and right-hand-side expressions, together with the optional message.

as_assign

as_assign
pub comptime fn as_assign(self) -> Option<(Expr, Expr)> {}

Source code: noir_stdlib/src/meta/expr.nr#L30-L32

If this expression is an assignment, this returns a tuple with the left hand side and right hand side in order.

as_binary_op

as_binary_op
pub comptime fn as_binary_op(self) -> Option<(Expr, BinaryOp, Expr)> {}

Source code: noir_stdlib/src/meta/expr.nr#L37-L39

If this expression is a binary operator operation <lhs> <op> <rhs>, return the left-hand side, operator, and the right-hand side of the operation.

as_block

as_block
pub comptime fn as_block(self) -> Option<[Expr]> {}

Source code: noir_stdlib/src/meta/expr.nr#L44-L46

If this expression is a block { stmt1; stmt2; ...; stmtN }, return a slice containing each statement.

as_bool

as_bool
pub comptime fn as_bool(self) -> Option<bool> {}

Source code: noir_stdlib/src/meta/expr.nr#L50-L52

If this expression is a boolean literal, return that literal.

as_cast

as_cast
#[builtin(expr_as_cast)]
pub comptime fn as_cast(self) -> Option<(Expr, UnresolvedType)> {}

Source code: noir_stdlib/src/meta/expr.nr#L56-L59

If this expression is a cast expression (expr as type), returns the casted expression and the type to cast to.

as_comptime

as_comptime
pub comptime fn as_comptime(self) -> Option<[Expr]> {}

Source code: noir_stdlib/src/meta/expr.nr#L64-L66

If this expression is a comptime { stmt1; stmt2; ...; stmtN } block, return each statement in the block.

as_constructor

as_constructor
pub comptime fn as_constructor(self) -> Option<(UnresolvedType, [(Quoted, Expr)])> {}

Source code: noir_stdlib/src/meta/expr.nr#L71-L73

If this expression is a constructor Type { field1: expr1, ..., fieldN: exprN }, return the type and the fields.

as_for

as_for
pub comptime fn as_for(self) -> Option<(Quoted, Expr, Expr)> {}

Source code: noir_stdlib/src/meta/expr.nr#L78-L80

If this expression is a for statement over a single expression, return the identifier, the expression and the for loop body.

as_for_range

as_for
pub comptime fn as_for(self) -> Option<(Quoted, Expr, Expr)> {}

Source code: noir_stdlib/src/meta/expr.nr#L78-L80

If this expression is a for statement over a range, return the identifier, the range start, the range end and the for loop body.

as_function_call

as_function_call
pub comptime fn as_function_call(self) -> Option<(Expr, [Expr])> {}

Source code: noir_stdlib/src/meta/expr.nr#L92-L94

If this expression is a function call foo(arg1, ..., argN), return the function and a slice of each argument.

as_if

as_if
pub comptime fn as_if(self) -> Option<(Expr, Expr, Option<Expr>)> {}

Source code: noir_stdlib/src/meta/expr.nr#L100-L102

If this expression is an if condition { then_branch } else { else_branch }, return the condition, then branch, and else branch. If there is no else branch, None is returned for that branch instead.

as_index

as_index
pub comptime fn as_index(self) -> Option<(Expr, Expr)> {}

Source code: noir_stdlib/src/meta/expr.nr#L107-L109

If this expression is an index into an array array[index], return the array and the index.

as_integer

as_integer
pub comptime fn as_integer(self) -> Option<(Field, bool)> {}

Source code: noir_stdlib/src/meta/expr.nr#L114-L116

If this expression is an integer literal, return the integer as a field as well as whether the integer is negative (true) or not (false).

as_lambda

as_lambda
pub comptime fn as_lambda(
self,
) -> Option<([(Expr, Option<UnresolvedType>)], Option<UnresolvedType>, Expr)> {}

Source code: noir_stdlib/src/meta/expr.nr#L120-L124

If this expression is a lambda, returns the parameters, return type and body.

as_let

as_let
pub comptime fn as_let(self) -> Option<(Expr, Option<UnresolvedType>, Expr)> {}

Source code: noir_stdlib/src/meta/expr.nr#L129-L131

If this expression is a let statement, returns the let pattern as an Expr, the optional type annotation, and the assigned expression.

as_member_access

as_member_access
pub comptime fn as_member_access(self) -> Option<(Expr, Quoted)> {}

Source code: noir_stdlib/src/meta/expr.nr#L136-L138

If this expression is a member access foo.bar, return the struct/tuple expression and the field. The field will be represented as a quoted value.

as_method_call

as_method_call
pub comptime fn as_method_call(self) -> Option<(Expr, Quoted, [UnresolvedType], [Expr])> {}

Source code: noir_stdlib/src/meta/expr.nr#L143-L145

If this expression is a method call foo.bar::<generic1, ..., genericM>(arg1, ..., argN), return the receiver, method name, a slice of each generic argument, and a slice of each argument.

as_repeated_element_array

as_repeated_element_array
pub comptime fn as_repeated_element_array(self) -> Option<(Expr, Expr)> {}

Source code: noir_stdlib/src/meta/expr.nr#L150-L152

If this expression is a repeated element array [elem; length], return the repeated element and the length expressions.

as_repeated_element_slice

as_repeated_element_slice
pub comptime fn as_repeated_element_slice(self) -> Option<(Expr, Expr)> {}

Source code: noir_stdlib/src/meta/expr.nr#L157-L159

If this expression is a repeated element slice [elem; length], return the repeated element and the length expressions.

as_slice

as_slice
pub comptime fn as_slice(self) -> Option<[Expr]> {}

Source code: noir_stdlib/src/meta/expr.nr#L164-L166

If this expression is a slice literal &[elem1, ..., elemN], return each element of the slice.

as_tuple

as_tuple
pub comptime fn as_tuple(self) -> Option<[Expr]> {}

Source code: noir_stdlib/src/meta/expr.nr#L171-L173

If this expression is a tuple (field1, ..., fieldN), return each element of the tuple.

as_unary_op

as_unary_op
pub comptime fn as_unary_op(self) -> Option<(UnaryOp, Expr)> {}

Source code: noir_stdlib/src/meta/expr.nr#L178-L180

If this expression is a unary operation <op> <rhs>, return the unary operator as well as the right-hand side expression.

as_unsafe

as_unsafe
pub comptime fn as_unsafe(self) -> Option<[Expr]> {}

Source code: noir_stdlib/src/meta/expr.nr#L185-L187

If this expression is an unsafe { stmt1; ...; stmtN } block, return each statement inside in a slice.

has_semicolon

has_semicolon
pub comptime fn has_semicolon(self) -> bool {}

Source code: noir_stdlib/src/meta/expr.nr#L206-L208

true if this expression is trailed by a semicolon. E.g.

comptime {
let expr1 = quote { 1 + 2 }.as_expr().unwrap();
let expr2 = quote { 1 + 2; }.as_expr().unwrap();

assert(expr1.as_binary_op().is_some());
assert(expr2.as_binary_op().is_some());

assert(!expr1.has_semicolon());
assert(expr2.has_semicolon());
}

is_break

is_break
pub comptime fn is_break(self) -> bool {}

Source code: noir_stdlib/src/meta/expr.nr#L212-L214

true if this expression is break.

is_continue

is_continue
pub comptime fn is_continue(self) -> bool {}

Source code: noir_stdlib/src/meta/expr.nr#L218-L220

true if this expression is continue.

modify

modify
pub comptime fn modify<Env>(self, f: fn[Env](Expr) -> Option<Expr>) -> Expr {

Source code: noir_stdlib/src/meta/expr.nr#L229-L231

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 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

quoted
pub comptime fn quoted(self) -> Quoted {

Source code: noir_stdlib/src/meta/expr.nr#L266-L268

Returns this expression as a Quoted value. It's the same as quote { $self }.

resolve

resolve
pub comptime fn resolve(self, in_function: Option<FunctionDefinition>) -> TypedExpr {}

Source code: noir_stdlib/src/meta/expr.nr#L282-L284

Resolves and type-checks this expression and returns the result as a TypedExpr.

The in_function argument specifies where the expression is resolved:

  • If it's none, the expression is resolved in the function where resolve was called
  • If it's some, the expression is resolved in the given function

If any names used by this expression are not in scope or if there are any type errors, this will give compiler errors as if the expression was written directly into the current comptime function.