1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[1815],{1528(e,n,t){t.r(n),t.d(n,{assets:()=>l,contentTitle:()=>a,default:()=>u,frontMatter:()=>i,metadata:()=>r,toc:()=>c});const r=JSON.parse('{"id":"libraries/standard_library/meta/quoted","title":"Quoted","description":"Token streams produced by `quote { ... }`\u2014parse as expressions, modules, types, and inspect raw tokens.","source":"@site/versioned_docs/version-v1.0.0-rc.1/libraries/standard_library/meta/quoted.md","sourceDirName":"libraries/standard_library/meta","slug":"/libraries/standard_library/meta/quoted","permalink":"/docs/v1.0.0-rc.1/libraries/standard_library/meta/quoted","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-rc.1/libraries/standard_library/meta/quoted.md","tags":[],"version":"v1.0.0-rc.1","frontMatter":{"title":"Quoted","description":"Token streams produced by `quote { ... }`\u2014parse as expressions, modules, types, and inspect raw tokens."},"sidebar":"sidebar","previous":{"title":"UnaryOp and BinaryOp","permalink":"/docs/v1.0.0-rc.1/libraries/standard_library/meta/op"},"next":{"title":"TraitConstraint","permalink":"/docs/v1.0.0-rc.1/libraries/standard_library/meta/trait_constraint"}}');var s=t(74848),o=t(28453);const i={title:"Quoted",description:"Token streams produced by `quote { ... }`\u2014parse as expressions, modules, types, and inspect raw tokens."},a=void 0,l={},c=[{value:"Methods",id:"methods",level:2},{value:"as_expr",id:"as_expr",level:3},{value:"as_module",id:"as_module",level:3},{value:"as_trait_constraint",id:"as_trait_constraint",level:3},{value:"as_type",id:"as_type",level:3},{value:"location",id:"location",level:3},{value:"tokens",id:"tokens",level:3},{value:"Trait Implementations",id:"trait-implementations",level:2},{value:"Security",id:"security",level:2}];function d(e){const n={a:"a",blockquote:"blockquote",code:"code",h2:"h2",h3:"h3",li:"li",p:"p",pre:"pre",sub:"sub",sup:"sup",ul:"ul",...(0,o.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"std::meta::quoted"})," contains methods on the built-in ",(0,s.jsx)(n.code,{children:"Quoted"})," type which represents\nquoted token streams and is the result of the ",(0,s.jsx)(n.code,{children:"quote { ... }"})," expression."]}),"\n",(0,s.jsx)(n.h2,{id:"methods",children:"Methods"}),"\n",(0,s.jsx)(n.h3,{id:"as_expr",children:"as_expr"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="as_expr" showLineNumbers ',children:"pub comptime fn as_expr(self) -> Option<Expr> {}\n"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.sup,{children:(0,s.jsx)(n.sub,{children:(0,s.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/quoted.nr#L6-L8",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/quoted.nr#L6-L8"})})})}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["Parses the quoted token stream as an expression. Returns ",(0,s.jsx)(n.code,{children:"Option::none()"})," if\nthe expression failed to parse."]}),"\n",(0,s.jsx)(n.p,{children:"Example:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="as_expr_example" showLineNumbers ',children:"#[test]\nfn test_expr_as_function_call() {\n comptime {\n let expr = quote { foo(42) }.as_expr().unwrap();\n let function_call = expr.as_function_call().unwrap();\n assert_eq(function_call.function().quoted(), quote { foo });\n\n let args = function_call.arguments();\n assert_eq(args.len(), 1);\n assert_eq(args[0].as_integer().unwrap().value(), 42);\n }\n}\n"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.sup,{children:(0,s.jsx)(n.sub,{children:(0,s.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/comptime_expr/src/main.nr#L227-L240",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/comptime_expr/src/main.nr#L227-L240"})})})}),"\n"]}),"\n",(0,s.jsx)(n.h3,{id:"as_module",children:"as_module"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="as_module" showLineNumbers ',children:"pub comptime fn as_module(self) -> Option<Module> {}\n"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.sup,{children:(0,s.jsx)(n.sub,{children:(0,s.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/quoted.nr#L11-L13",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/quoted.nr#L11-L13"})})})}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["Interprets this token stream as a module path leading to the name of a module.\nReturns ",(0,s.jsx)(n.code,{children:"Option::none()"})," if the module isn't found or this token stream cannot be parsed as a path."]}),"\n",(0,s.jsx)(n.p,{children:"Example:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="as_module_example" showLineNumbers ',children:"mod baz {\n pub mod qux {}\n}\n\n#[test]\nfn as_module_test() {\n comptime {\n let my_mod = quote { baz::qux }.as_module().unwrap();\n assert_eq(my_mod.name(), quote { qux });\n }\n}\n"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.sup,{children:(0,s.jsx)(n.sub,{children:(0,s.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/compile_success_empty/comptime_module/src/main.nr#L100-L112",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/compile_success_empty/comptime_module/src/main.nr#L100-L112"})})})}),"\n"]}),"\n",(0,s.jsx)(n.h3,{id:"as_trait_constraint",children:"as_trait_constraint"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="as_trait_constraint" showLineNumbers ',children:"pub comptime fn as_trait_constraint(self) -> TraitConstraint {}\n"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.sup,{children:(0,s.jsx)(n.sub,{children:(0,s.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/quoted.nr#L16-L18",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/quoted.nr#L16-L18"})})})}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["Interprets this token stream as a trait constraint (without an object type).\nNote that this function panics instead of returning ",(0,s.jsx)(n.code,{children:"Option::none()"})," if the token\nstream does not parse and resolve to a valid trait constraint."]}),"\n",(0,s.jsx)(n.p,{children:"Example:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="implements_example" showLineNumbers ',children:"pub fn function_with_where<T>(_x: T)\nwhere\n T: SomeTrait<i32>,\n{\n comptime {\n let t = quote { T }.as_type();\n let some_trait_i32 = quote { SomeTrait<i32> }.as_trait_constraint();\n assert(t.implements(some_trait_i32));\n\n assert(t.get_trait_impl(some_trait_i32).is_none());\n }\n}\n"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.sup,{children:(0,s.jsx)(n.sub,{children:(0,s.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/compile_success_empty/comptime_type/src/main.nr#L170-L183",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/compile_success_empty/comptime_type/src/main.nr#L170-L183"})})})}),"\n"]}),"\n",(0,s.jsx)(n.h3,{id:"as_type",children:"as_type"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="as_type" showLineNumbers ',children:"pub comptime fn as_type(self) -> Type {}\n"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.sup,{children:(0,s.jsx)(n.sub,{children:(0,s.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/quoted.nr#L21-L23",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/quoted.nr#L21-L23"})})})}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"Interprets this token stream as a resolved type. Panics if the token\nstream doesn't parse to a type or if the type isn't a valid type in scope."}),"\n",(0,s.jsxs)(n.p,{children:["A numeric literal such as ",(0,s.jsx)(n.code,{children:"4"}
1)," is interpreted as the type-level integer ",(0,s.jsx)(n.code,{children:"4"}),", so it\ncan be used to fill a numeric generic slot, such as for an array length."]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="implements_example" showLineNumbers ',children:"pub fn function_with_where<T>(_x: T)\nwhere\n T: SomeTrait<i32>,\n{\n comptime {\n let t = quote { T }.as_type();\n let some_trait_i32 = quote { SomeTrait<i32> }.as_trait_constraint();\n assert(t.implements(some_trait_i32));\n\n assert(t.get_trait_impl(some_trait_i32).is_none());\n }\n}\n"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.sup,{children:(0,s.jsx)(n.sub,{children:(0,s.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/compile_success_empty/comptime_type/src/main.nr#L170-L183",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/compile_success_empty/comptime_type/src/main.nr#L170-L183"})})})}),"\n"]}),"\n",(0,s.jsx)(n.h3,{id:"location",children:"location"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="location" showLineNumbers ',children:"pub comptime fn location(self) -> Option<Location> {}\n"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.sup,{children:(0,s.jsx)(n.sub,{children:(0,s.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/quoted.nr#L26-L28",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/quoted.nr#L26-L28"})})})}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["Returns the source location spanning the tokens of this stream, or ",(0,s.jsx)(n.code,{children:"Option::none()"})," if\nthe stream is empty."]}),"\n",(0,s.jsx)(n.h3,{id:"tokens",children:"tokens"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="tokens" showLineNumbers ',children:"pub comptime fn tokens(self) -> [Quoted] {}\n"})}),"\n",(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.sup,{children:(0,s.jsx)(n.sub,{children:(0,s.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/quoted.nr#L31-L33",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/quoted.nr#L31-L33"})})})}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"Returns a vector of the individual tokens that form this token stream."}),"\n",(0,s.jsx)(n.h2,{id:"trait-implementations",children:"Trait Implementations"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",children:"impl Eq for Quoted\nimpl Hash for Quoted\n"})}),"\n",(0,s.jsx)(n.h2,{id:"security",children:"Security"}),"\n",(0,s.jsxs)(n.p,{children:["A ",(0,s.jsx)(n.code,{children:"Quoted"})," value returned by a comptime function from an untrusted dependency is added, when unquoted,\nto the program's source as if you had written it yourself. It can therefore introduce arbitrary items,\nexpressions, or trait impls into the crate that calls it, including code that the caller did not write\nor would not approve."]}),"\n",(0,s.jsx)(n.p,{children:"When pulling in comptime code from a dependency you do not control:"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["Treat any ",(0,s.jsx)(n.code,{children:"Quoted"})," it returns to you, and any attribute macro it provides, as code you are about\nto vendor into your own crate."]}),"\n",(0,s.jsxs)(n.li,{children:["Use ",(0,s.jsx)(n.code,{children:"nargo expand"})," to view the program after macro expansion, and review the result before\ndeployment. The expanded source is the code that actually compiles into your circuit."]}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["See also the ",(0,s.jsx)(n.a,{href:"/docs/v1.0.0-rc.1/language/comptime#security-considerations",children:"Security considerations"})," section\nof the comptime documentation."]})]})}function u(e={}){const{wrapper:n}={...(0,o.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(d,{...e})}):d(e)}},28453(e,n,t){t.d(n,{R:()=>i,x:()=>a});var r=t(96540);const s={},o=r.createContext(s);function i(e){const n=r.useContext(o);return r.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function a(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:i(e.components),r.createElement(o.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.