PageSourceSearch

https://noir-lang.org/docs/assets/js/b942ed18.6fda8edc.js

js noir-lang.org collected 2026-10-03 22:54:56 UTC 18,203 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[3898],{68369(e,n,t){t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>a,default:()=>u,frontMatter:()=>o,metadata:()=>i,toc:()=>l});const i=JSON.parse('{"id":"libraries/standard_library/meta/function_def","title":"FunctionDefinition","description":"Inspect and mutate function definitions in `comptime`\u2014read signatures, body, attributes, and adjust parameters or return types.","source":"@site/versioned_docs/version-v1.0.0-rc.0/libraries/standard_library/meta/function_def.md","sourceDirName":"libraries/standard_library/meta","slug":"/libraries/standard_library/meta/function_def","permalink":"/docs/v1.0.0-rc.0/libraries/standard_library/meta/function_def","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-rc.0/libraries/standard_library/meta/function_def.md","tags":[],"version":"v1.0.0-rc.0","frontMatter":{"title":"FunctionDefinition","description":"Inspect and mutate function definitions in `comptime`\u2014read signatures, body, attributes, and adjust parameters or return types."},"sidebar":"sidebar","previous":{"title":"Expr","permalink":"/docs/v1.0.0-rc.0/libraries/standard_library/meta/expr"},"next":{"title":"ItemVisibility","permalink":"/docs/v1.0.0-rc.0/libraries/standard_library/meta/item_visibility"}}');var r=t(74848),s=t(28453);const o={title:"FunctionDefinition",description:"Inspect and mutate function definitions in `comptime`\u2014read signatures, body, attributes, and adjust parameters or return types."},a=void 0,c={},l=[{value:"Methods",id:"methods",level:2},{value:"as_typed_expr",id:"as_typed_expr",level:3},{value:"body",id:"body",level:3},{value:"disable",id:"disable",level:3},{value:"Security",id:"security",level:4},{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_unconstrained",id:"is_unconstrained",level:3},{value:"location",id:"location",level:3},{value:"module",id:"module",level:3},{value:"name",id:"name",level:3},{value:"parameters",id:"parameters",level:3},{value:"return_type",id:"return_type",level:3},{value:"visibility",id:"visibility",level:3},{value:"Trait Implementations",id:"trait-implementations",level:2}];function d(e){const n={a:"a",blockquote:"blockquote",code:"code",h2:"h2",h3:"h3",h4:"h4",li:"li",p:"p",pre:"pre",sub:"sub",sup:"sup",ul:"ul",...(0,s.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"std::meta::function_def"})," contains methods on the built-in ",(0,r.jsx)(n.code,{children:"FunctionDefinition"})," type representing\na function definition in the source program."]}),"\n",(0,r.jsx)(n.h2,{id:"methods",children:"Methods"}),"\n",(0,r.jsx)(n.h3,{id:"as_typed_expr",children:"as_typed_expr"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="as_typed_expr" showLineNumbers ',children:"pub comptime fn as_typed_expr(self) -> TypedExpr {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L24-L26",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L24-L26"})})})}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["Returns this function as a ",(0,r.jsx)(n.code,{children:"TypedExpr"}),", which can be unquoted. For example:"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:"let typed_expr = some_function.as_typed_expr();\nlet _ = quote { $typed_expr(1, 2, 3); };\n"})}),"\n",(0,r.jsx)(n.h3,{id:"body",children:"body"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="body" showLineNumbers ',children:"pub comptime fn body(self) -> Expr {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L29-L31",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L29-L31"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns the body of the function as an expression. This is only valid\non functions in the current crate which have not yet been resolved.\nThis means any functions called at compile-time are invalid targets for this method."}),"\n",(0,r.jsx)(n.h3,{id:"disable",children:"disable"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="disable" showLineNumbers ',children:"pub comptime fn disable(self, error_message: CtString) {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L34-L36",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L34-L36"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Disables calling the given function, issuing an error with the provided message if it is ever called."}),"\n",(0,r.jsxs)(n.p,{children:["If an attribute generates a new version of a function that is meant to be called instead, calling ",(0,r.jsx)(n.code,{children:"disable"}),"\non the old function can be helpful to point a user to the new function to call."]}),"\n",(0,r.jsxs)(n.p,{children:["If the disabled function is part of a contract, this function will also remove it from the contract\ninterface as if ",(0,r.jsx)(n.code,{children:"#[contract_library_method]"})," was used."]}),"\n",(0,r.jsx)(n.p,{children:"This method requires the given function to yet be elaborated by the compiler - otherwise it is possible\nit is already called elsewhere without an error."}),"\n",(0,r.jsx)(n.h4,{id:"security",children:"Security"}),"\n",(0,r.jsxs)(n.p,{children:["Before switching over to use these newly generated functions, users should run ",(0,r.jsx)(n.code,{children:"nargo expand"})," on any\nuntrusted dependencies to ensure their bodies are as expected."]}),"\n",(0,r.jsxs)(n.p,{children:["Because a dependency's attribute macro can call ",(0,r.jsx)(n.code,{children:"disable"})," on any function it can name, an untrusted or\nmalicious depen
1dency may use ",(0,r.jsx)(n.code,{children:"disable"})," to silently turn off functionality in your program \u2014 for example,\ndisabling a contract entry point so that it can no longer be invoked. A disabled function does not fail\nat compile time: it fails only when something tries to call it, producing the error message supplied to\n",(0,r.jsx)(n.code,{children:"disable"}),". This makes it possible to ship a program in which an endpoint or feature has been\nunintentionally taken offline, and gives opportunities for malicious DoS behaviors"]}),"\n",(0,r.jsx)(n.p,{children:"Before deploying a program that pulls in third-party comptime code, you should:"}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:["Exercise every entry point of the program (and any externally reachable function) at least once\nduring testing. A ",(0,r.jsx)(n.code,{children:"disable"}),"d function errors loudly the first time it is called, so even a minimal\nsmoke test of each endpoint is enough to surface this class of issue."]}),"\n",(0,r.jsxs)(n.li,{children:["Run ",(0,r.jsx)(n.code,{children:"nargo expand"})," on any untrusted dependencies and review the output for ",(0,r.jsx)(n.code,{children:"disable"})," calls on\nfunctions you did not expect to be disabled."]}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["See also the ",(0,r.jsx)(n.a,{href:"/docs/v1.0.0-rc.0/language/comptime#security-considerations",children:"Security considerations"})," section of\nthe comptime documentation."]}),"\n",(0,r.jsx)(n.h3,{id:"has_named_attribute",children:"has_named_attribute"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.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,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L39-L41",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L39-L41"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns true if this function has a custom attribute with the given name."}),"\n",(0,r.jsxs)(n.p,{children:["This matches both built-in attributes (such as ",(0,r.jsx)(n.code,{children:"deprecated"}),", ",(0,r.jsx)(n.code,{children:"export"}),") and\nuser-written attributes (tags like ",(0,r.jsx)(n.code,{children:"#['my_tag]"})," and applied comptime macros).\nUse ",(0,r.jsx)(n.code,{children:"has_builtin_attribute"})," if you need to match only the built-in attribute\nof the given name."]}),"\n",(0,r.jsx)(n.h3,{id:"named_attribute_args",children:"named_attribute_args"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.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,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L44-L46",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L44-L46"})})})}),"\n"]}),"\n",(0,r.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,r.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,r.jsx)(n.code,{children:"quote"}),"."]}),"\n",(0,r.jsx)(n.h3,{id:"has_builtin_attribute",children:"has_builtin_attribute"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.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,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L49-L51",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L49-L51"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns true if this function has a built-in attribute with the given name."}),"\n",(0,r.jsxs)(n.p,{children:["Unlike ",(0,r.jsx)(n.code,{children:"has_named_attribute"}),", this ignores user-written tag attributes and\napplied comptime macros, so a user attribute that happens to share an\nidentifier with a built-in does not produce a false positive."]}),"\n",(0,r.jsx)(n.h3,{id:"is_unconstrained",children:"is_unconstrained"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="is_unconstrained" showLineNumbers ',children:"pub comptime fn is_unconstrained(self) -> bool {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L54-L56",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L54-L56"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns true if this function is unconstrained."}),"\n",(0,r.jsx)(n.h3,{id:"location",children:"location"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="location" showLineNumbers ',children:"pub comptime fn location(self) -> Location {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L59-L61",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L59-L61"})})})}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["Returns the source ",(0,r.jsx)(n.a,{href:"/docs/v1.0.0-rc.0/libraries/standard_library/meta/location",children:(0,r.jsx)(n.code,{children:"Location"})})," where the function is defined.\nThis can be passed to ",(0,r.jsx)(n.code,{children:"std::meta::error"})," or ",(0,r.jsx)(n.code,{children:"std::meta::warn"})," to attach a diagnostic to the function."]}),"\n",(0,r.jsx)(n.h3,{id:"module",children:"module"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="module" showLineNumbers ',children:"pub comptime fn module(self) -> Module {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L64-L66",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L64-L66"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns the module where the function is defined."}),"\n",(0,r.jsx)(n.h3,{id:"name",children:"name"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="name" showLineNumbers ',children:"pub comptime fn name(self) -> Quoted {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L69-L71",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L69-L71"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns the name of the function."}),"\n",(0,r.jsx)(n.h3,{id:"parameters",children:"parameters"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="parameters" showLineNumbers ',children:"pub comptime fn parameters(self) ->
1 [FunctionParameter] {\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L74-L76",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L74-L76"})})})}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["Returns the function's declared parameters in source order as ",(0,r.jsx)(n.code,{children:"FunctionParameter"})," values.\nUse ",(0,r.jsx)(n.code,{children:"name()"})," to get a parameter's declared name as a ",(0,r.jsx)(n.code,{children:"Quoted"})," identifier and ",(0,r.jsx)(n.code,{children:"typ()"})," to get\nits ",(0,r.jsx)(n.code,{children:"Type"}),"."]}),"\n",(0,r.jsx)(n.h3,{id:"return_type",children:"return_type"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="return_type" showLineNumbers ',children:"pub comptime fn return_type(self) -> Type {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L86-L88",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L86-L88"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"The return type of the function."}),"\n",(0,r.jsx)(n.h3,{id:"visibility",children:"visibility"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="visibility" showLineNumbers ',children:"pub comptime fn visibility(self) -> ItemVisibility {\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/function_def.nr#L91-L93",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/function_def.nr#L91-L93"})})})}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["Returns the function's ",(0,r.jsx)(n.a,{href:"/docs/v1.0.0-rc.0/libraries/standard_library/meta/item_visibility",children:(0,r.jsx)(n.code,{children:"ItemVisibility"})}),". Use its predicate methods to\ninspect the visibility, or ",(0,r.jsx)(n.code,{children:"as_quoted()"})," when generating code."]}),"\n",(0,r.jsx)(n.h2,{id:"trait-implementations",children:"Trait Implementations"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:"impl Eq for FunctionDefinition\nimpl Hash for FunctionDefinition\n"})}),"\n",(0,r.jsx)(n.p,{children:"Note that each function is assigned a unique ID internally and this is what is used for\nequality and hashing. So even functions with identical signatures and bodies may not\nbe equal in this sense if they were originally different items in the source program."})]})}function u(e={}){const{wrapper:n}={...(0,s.R)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(d,{...e})}):d(e)}},28453(e,n,t){t.d(n,{R:()=>o,x:()=>a});var i=t(96540);const r={},s=i.createContext(r);function o(e){const n=i.useContext(s);return i.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(r):e.components||r:o(e.components),i.createElement(s.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.