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
pub comptime fn as_expr(self) -> Option<Expr> {}
Parses the quoted token stream as an expression. Returns Option::none() if
the expression failed to parse.
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
pub comptime fn as_module(self) -> Option<Module> {}
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:
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
pub comptime fn as_trait_constraint(self) -> TraitConstraint {}
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:
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
pub comptime fn as_type(self) -> Type {}
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.
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
pub comptime fn location(self) -> Option<Location> {}
Returns the source location spanning the tokens of this stream, or Option::none() if
the stream is empty.
tokens
pub comptime fn tokens(self) -> [Quoted] {}
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
Quotedit returns to you, and any attribute macro it provides, as code you are about to vendor into your own crate. - Use
nargo expandto 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.