Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl Interpreter<'_, '_> {
"function_def_set_unconstrained" => {
function_def_set_unconstrained(self, arguments, location)
}
"function_def_visibility" => function_def_visibility(interner, arguments, location),
"module_add_item" => module_add_item(self, arguments, location),
"module_eq" => module_eq(arguments, location),
"module_functions" => module_functions(self, arguments, location),
Expand Down Expand Up @@ -2830,6 +2831,18 @@ fn function_def_set_unconstrained(
Ok(Value::Unit)
}

// fn visibility(self) -> Quoted
fn function_def_visibility(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let self_argument = check_one_argument(arguments, location)?;
let func_id = get_function_def(self_argument)?;
let visibility = interner.function_visibility(func_id);
Ok(visibility_to_quoted(visibility, location))
}

// fn add_item(self, item: Quoted)
fn module_add_item(
interpreter: &mut Interpreter,
Expand Down
9 changes: 9 additions & 0 deletions docs/docs/noir/standard_library/meta/function_def.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ Mutates the function to be unconstrained (if `true` is given) or not (if `false`
This is only valid on functions in the current crate which have not yet been resolved.
This means any functions called at compile-time are invalid targets for this method.

### visibility

#include_code visibility noir_stdlib/src/meta/function_def.nr rust

Returns the function's visibility as a `Quoted` value, which will be one of:
- `quote { }`: the function is private
- `quote { pub }`: the function is `pub`
- `quote { pub(crate) }`: the function is `pub(crate)`

## Trait Implementations

```rust
Expand Down
5 changes: 5 additions & 0 deletions noir_stdlib/src/meta/function_def.nr
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ impl FunctionDefinition {
// docs:start:set_unconstrained
pub comptime fn set_unconstrained(self, value: bool) {}
// docs:end:set_unconstrained

#[builtin(function_def_visibility)]
// docs:start:visibility
pub comptime fn visibility(self) -> Quoted {}
// docs:end:visibility
}

impl crate::hash::Hash for FunctionDefinition {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub fn foo(
#[check_named_attribute]
fn some_test() {}

#[check_visibility]
pub(crate) fn pub_crate_fn() {}

comptime fn function_attr(f: FunctionDefinition) {
// Check FunctionDefinition::parameters
let parameters = f.parameters();
Expand All @@ -48,11 +51,18 @@ comptime fn function_attr(f: FunctionDefinition) {
assert_eq(f.name(), quote { foo });

assert(f.has_named_attribute("function_attr"));

assert_eq(f.visibility(), quote { pub })
}

comptime fn check_named_attribute(f: FunctionDefinition) {
assert(f.has_named_attribute("test"));
assert(f.has_named_attribute("deprecated"));
assert_eq(f.visibility(), quote { })
}

comptime fn check_visibility(f: FunctionDefinition) {
assert_eq(f.visibility(), quote { pub(crate) })
}

#[mutate_add_one]
Expand All @@ -75,6 +85,7 @@ comptime fn mutate_add_one(f: FunctionDefinition) {

fn main() {
assert_eq(add_one(41), 42);
pub_crate_fn();
}

contract some_contract {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading