Skip to main content
Version: dev

Quoted

std::meta::quoted contains methods on the built-in Quoted type which represents quoted token streams and is the result of the quote { ... } expression.

Methods

as_expr

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

Source code: noir_stdlib/src/meta/quoted.nr#L6-L8

Parses the quoted token stream as an expression. Returns Option::none() if the expression failed to parse.

Example:

as_expr_example
#[test]
fn test_expr_as_function_call() {
comptime {
let expr = quote { foo(42) }.as_expr().unwrap();
let (_function, args) = expr.as_function_call().unwrap();
assert_eq(args.len(), 1);
assert_eq(args[0].as_integer().unwrap(), 42);
}
}

Source code: test_programs/noir_test_success/comptime_expr/src/main.nr#L213-L223

as_module

as_module
pub comptime fn as_module(self) -> Option<Module> {}

Source code: noir_stdlib/src/meta/quoted.nr#L11-L13

Interprets this token stream as a module path leading to the name of a module. Returns Option::none() if the module isn't found or this token stream cannot be parsed as a path.

Example:

as_module_example
mod baz {
pub mod qux {}
}

#[test]
fn as_module_test() {
comptime {
let my_mod = quote { baz::qux }.as_module().unwrap();
assert_eq(my_mod.name(), quote { qux });
}
}

Source code: test_programs/compile_success_empty/comptime_module/src/main.nr#L100-L112

as_trait_constraint

as_trait_constraint
pub comptime fn as_trait_constraint(self) -> TraitConstraint {}

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

Interprets this token stream as a trait constraint (without an object type). Note that this function panics instead of returning Option::none() if the token stream does not parse and resolve to a valid trait constraint.

Example:

implements_example
pub fn function_with_where<T>(_x: T)
where
T: SomeTrait<i32>,
{
comptime {
let t = quote { T }.as_type();
let some_trait_i32 = quote { SomeTrait<i32> }.as_trait_constraint();
assert(t.implements(some_trait_i32));

assert(t.get_trait_impl(some_trait_i32).is_none());
}
}

Source code: test_programs/compile_success_empty/comptime_type/src/main.nr#L170-L183

as_type

as_type
pub comptime fn as_type(self) -> Type {}

Source code: noir_stdlib/src/meta/quoted.nr#L21-L23

Interprets this token stream as a resolved type. Panics if the token stream doesn't parse to a type or if the type isn't a valid type in scope.

implements_example
pub fn function_with_where<T>(_x: T)
where
T: SomeTrait<i32>,
{
comptime {
let t = quote { T }.as_type();
let some_trait_i32 = quote { SomeTrait<i32> }.as_trait_constraint();
assert(t.implements(some_trait_i32));

assert(t.get_trait_impl(some_trait_i32).is_none());
}
}

Source code: test_programs/compile_success_empty/comptime_type/src/main.nr#L170-L183

location

location
pub comptime fn location(self) -> Option<Location> {}

Source code: noir_stdlib/src/meta/quoted.nr#L26-L28

Returns the source location spanning the tokens of this stream, or Option::none() if the stream is empty.

tokens

tokens
pub comptime fn tokens(self) -> [Quoted] {}

Source code: noir_stdlib/src/meta/quoted.nr#L31-L33

Returns a vector of the individual tokens that form this token stream.

Trait Implementations

impl Eq for Quoted
impl Hash for Quoted

Security

A Quoted value returned by a comptime function from an untrusted dependency is added, when unquoted, to the program's source as if you had written it yourself. It can therefore introduce arbitrary items, expressions, or trait impls into the crate that calls it, including code that the caller did not write or would not approve.

When pulling in comptime code from a dependency you do not control:

  • Treat any Quoted it returns to you, and any attribute macro it provides, as code you are about to vendor into your own crate.
  • Use nargo expand to view the program after macro expansion, and review the result before deployment. The expanded source is the code that actually compiles into your circuit.

See also the Security considerations section of the comptime documentation.