PageSourceSearch

https://noir-lang.org/docs/assets/js/c41e919f.d68b8c5e.js

js noir-lang.org collected 2026-10-03 22:51:22 UTC 14,088 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[1407],{64659(e,n,t){t.r(n),t.d(n,{assets:()=>a,contentTitle:()=>o,default:()=>u,frontMatter:()=>l,metadata:()=>r,toc:()=>d});const r=JSON.parse('{"id":"libraries/standard_library/meta/module","title":"Module","description":"Work with modules in `comptime`\u2014query names, list functions/structs, detect contracts, and add new items.","source":"@site/processed-docs/libraries/standard_library/meta/module.md","sourceDirName":"libraries/standard_library/meta","slug":"/libraries/standard_library/meta/module","permalink":"/docs/dev/libraries/standard_library/meta/module","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/docs/libraries/standard_library/meta/module.md","tags":[],"version":"current","frontMatter":{"title":"Module","description":"Work with modules in `comptime`\u2014query names, list functions/structs, detect contracts, and add new items."},"sidebar":"sidebar","previous":{"title":"Location","permalink":"/docs/dev/libraries/standard_library/meta/location"},"next":{"title":"UnaryOp and BinaryOp","permalink":"/docs/dev/libraries/standard_library/meta/op"}}');var s=t(74848),i=t(28453);const l={title:"Module",description:"Work with modules in `comptime`\u2014query names, list functions/structs, detect contracts, and add new items."},o=void 0,a={},d=[{value:"Methods",id:"methods",level:2},{value:"child_modules",id:"child_modules",level:3},{value:"functions",id:"functions",level:3},{value:"has_named_attribute",id:"has_named_attribute",level:3},{value:"named_attribute_args",id:"named_attribute_args",level:3},{value:"has_builtin_attribute",id:"has_builtin_attribute",level:3},{value:"is_contract",id:"is_contract",level:3},{value:"location",id:"location",level:3},{value:"name",id:"name",level:3},{value:"parent",id:"parent",level:3},{value:"structs",id:"structs",level:3},{value:"Trait Implementations",id:"trait-implementations",level:2}];function c(e){const n={a:"a",blockquote:"blockquote",code:"code",h2:"h2",h3:"h3",p:"p",pre:"pre",sub:"sub",sup:"sup",...(0,i.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"std::meta::module"})," contains methods on the built-in ",(0,s.jsx)(n.code,{children:"Module"})," type which represents a module in the source program.\nNote that this type represents a module generally, it isn't limited to only ",(0,s.jsx)(n.code,{children:"mod my_submodule { ... }"}),"\ndeclarations in the source program."]}),"\n",(0,s.jsx)(n.h2,{id:"methods",children:"Methods"}),"\n",(0,s.jsx)(n.h3,{id:"child_modules",children:"child_modules"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="child_modules" showLineNumbers ',children:"pub comptime fn child_modules(self) -> [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/module.nr#L40-L42",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/module.nr#L40-L42"})})})}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"Returns all the child modules of the current module."}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="child_modules_example" showLineNumbers ',children:"mod my_module {\n    pub mod child1 {}\n    pub mod child2 {}\n    pub mod child3 {\n        pub mod nested_child {}\n    }\n}\n\n#[test]\nfn child_modules_test() {\n    comptime {\n        let my_module = quote [ my_module ].as_module().unwrap();\n        let children = my_module.child_modules().map(Module::name);\n\n        // The order children are returned in is left unspecified.\n        assert_eq(children, [quote [ child1 ], quote [ child2 ], quote [ child3 ]].as_vector());\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#L135-L154",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/compile_success_empty/comptime_module/src/main.nr#L135-L154"})})})}),"\n"]}),"\n",(0,s.jsx)(n.h3,{id:"functions",children:"functions"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="functions" showLineNumbers ',children:"pub comptime fn functions(self) ->
1 [FunctionDefinition] {}\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/module.nr#L30-L32",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/module.nr#L30-L32"})})})}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"Returns each function defined in the module."}),"\n",(0,s.jsx)(n.h3,{id:"has_named_attribute",children:"has_named_attribute"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="has_named_attribute" showLineNumbers ',children:"pub comptime fn has_named_attribute<let N: u32>(self, name: str<N>) -> bool {}\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/module.nr#L5-L7",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/module.nr#L5-L7"})})})}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"Returns true if this module has a custom attribute with the given name."}),"\n",(0,s.jsxs)(n.p,{children:["This matches both built-in attributes and user-written attributes (tags and\napplied comptime macros). Use ",(0,s.jsx)(n.code,{children:"has_builtin_attribute"})," if you need to match\nonly the built-in attribute of the given name."]}),"\n",(0,s.jsx)(n.h3,{id:"named_attribute_args",children:"named_attribute_args"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="named_attribute_args" showLineNumbers ',children:"pub comptime fn named_attribute_args<let N: u32>(self, name: str<N>) -> [[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/module.nr#L10-L12",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/module.nr#L10-L12"})})})}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["Returns the arguments of each occurrence of the attribute with the given name, as token\nstreams. The outer slice has one entry per occurrence \u2014 empty when the attribute is absent,\nso this is a superset of ",(0,s.jsx)(n.code,{children:"has_named_attribute"})," \u2014 and each inner slice holds that occurrence's\nargument expressions, which can be spliced into generated code with ",(0,s.jsx)(n.code,{children:"quote"}),"."]}),"\n",(0,s.jsx)(n.h3,{id:"has_builtin_attribute",children:"has_builtin_attribute"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="has_builtin_attribute" showLineNumbers ',children:"pub comptime fn has_builtin_attribute<let N: u32>(self, name: str<N>) -> bool {}\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/module.nr#L15-L17",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/module.nr#L15-L17"})})})}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"Returns true if this module has a built-in attribute with the given name."}),"\n",(0,s.jsxs)(n.p,{children:["Unlike ",(0,s.jsx)(n.code,{children:"has_named_attribute"}),", this ignores user-written tag attributes and\napplied comptime macros."]}),"\n",(0,s.jsx)(n.h3,{id:"is_contract",children:"is_contract"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="is_contract" showLineNumbers ',children:"pub comptime fn is_contract(self) -> bool {}\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/module.nr#L20-L22",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/module.nr#L20-L22"})})})}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"true"})," if this module is a contract module (was declared via ",(0,s.jsx)(n.code,{children:"contract foo { ... }"}),")."]}),"\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) -> 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/module.nr#L25-L27",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/module.nr#L25-L27"})})})}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["Returns the source ",(0,s.jsx)(n.a,{href:"/docs/dev/libraries/standard_library/meta/location",children:(0,s.jsx)(n.code,{children:"Location"})})," where this module is declared.\nThis can be passed to ",(0,s.jsx)(n.code,{children:"std::meta::error"})," or ",(0,s.jsx)(n.code,{children:"std::meta::warn"})," to attach a diagnostic to the module."]}),"\n",(0,s.jsx)(n.h3,{id:"name",children:"name"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="name" showLineNumbers ',children:"pub comptime fn name(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/module.nr#L45-L47",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/module.nr#L45-L47"})})})}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"Returns the name of the module.\nThe top-level module in each crate has no name and is thus empty."}),"\n",(0,s.jsx)(n.h3,{id:"parent",children:"parent"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="parent" showLineNumbers ',children:"pub comptime fn parent(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/module.nr#L50-L52",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/module.nr#L50-L52"})})})}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"Returns the parent module of the given module, if any."}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="parent_example" showLineNumbers ',children:"mod module1 {\n    pub mod module2 {}\n}\n\n#[test]\nfn parent_test() {\n    comptime {\n        let my_module2 = quote [ module1::module2 ].as_module().unwrap();\n        assert_eq(my_module2.name(), quote [ module2 ]);\n\n        let my_module1 = my_module2.parent().unwrap();\n        assert_eq(my_module1.name(), quote [ module1 ]);\n\n        // The top-level module in each crate has no name\n        let top_level_module = my_module1.parent().unwrap();\n        assert_eq(top_level_module.name(), quote []);\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#L114-L133",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/compile_success_empty/comptime_module/src/main.nr#L114-L133"})})})}),"\n"]}),"\n",(0,s.jsx)(n.h3,{id:"structs",children:"structs"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-rust",metastring:'title="structs" showLineNumbers ',children:"pub comptime fn structs(self) -> [TypeDefinition] {}\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/module.nr#L35-L37",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/module.nr#L35-L37"})})})}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"Returns each struct defined in the module."}),"\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 Module\nimpl Hash for Module\n"})}),"\n",(0,s.jsx)(n.p,{children:"Note that each module is assigned a unique ID internally and this is what is used for\nequality and hashing. So even modules with identical names and contents may not\nbe equal in this sense if they were originally different items in the source program."})]})}function u(e={}){const{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(c,{...e})}):c(e)}},28453(e,n,t){t.d(n,{R:()=>l,x:()=>o});var r=t(96540);const s={},i=r.createContext(s);function l(e){const n=r.useContext(i);return r.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function o(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:l(e.components),r.createElement(i.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.