Skip to content

Commit fc5bb02

Browse files
authored
feat: Add Module::structs (#6017)
# Description ## Problem\* Resolves #6015 ## Summary\* Adds a method to `Module` to retrieve each struct defined in the module ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [x] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
1 parent da32bd8 commit fc5bb02

File tree

4 files changed

+52
-10
lines changed
  • compiler/noirc_frontend/src/hir/comptime/interpreter
  • docs/docs/noir/standard_library/meta
  • noir_stdlib/src/meta
  • test_programs/compile_success_empty/comptime_module/src

4 files changed

+52
-10
lines changed

compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

+25
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
130130
"module_has_named_attribute" => module_has_named_attribute(self, arguments, location),
131131
"module_is_contract" => module_is_contract(self, arguments, location),
132132
"module_name" => module_name(interner, arguments, location),
133+
"module_structs" => module_structs(self, arguments, location),
133134
"modulus_be_bits" => modulus_be_bits(interner, arguments, location),
134135
"modulus_be_bytes" => modulus_be_bytes(interner, arguments, location),
135136
"modulus_le_bits" => modulus_le_bits(interner, arguments, location),
@@ -2294,6 +2295,30 @@ fn module_functions(
22942295
Ok(Value::Slice(func_ids, slice_type))
22952296
}
22962297

2298+
// fn structs(self) -> [StructDefinition]
2299+
fn module_structs(
2300+
interpreter: &Interpreter,
2301+
arguments: Vec<(Value, Location)>,
2302+
location: Location,
2303+
) -> IResult<Value> {
2304+
let self_argument = check_one_argument(arguments, location)?;
2305+
let module_id = get_module(self_argument)?;
2306+
let module_data = interpreter.elaborator.get_module(module_id);
2307+
let struct_ids = module_data
2308+
.type_definitions()
2309+
.filter_map(|module_def_id| {
2310+
if let ModuleDefId::TypeId(id) = module_def_id {
2311+
Some(Value::StructDefinition(id))
2312+
} else {
2313+
None
2314+
}
2315+
})
2316+
.collect();
2317+
2318+
let slice_type = Type::Slice(Box::new(Type::Quoted(QuotedType::StructDefinition)));
2319+
Ok(Value::Slice(struct_ids, slice_type))
2320+
}
2321+
22972322
// fn has_named_attribute(self, name: Quoted) -> bool
22982323
fn module_has_named_attribute(
22992324
interpreter: &Interpreter,

docs/docs/noir/standard_library/meta/module.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,11 @@ Adds a top-level item (a function, a struct, a global, etc.) to the module.
1616
Adding multiple items in one go is also valid if the `Quoted` value has multiple items in it.
1717
Note that the items are type-checked as if they are inside the module they are being added to.
1818

19-
### name
20-
21-
#include_code name noir_stdlib/src/meta/module.nr rust
22-
23-
Returns the name of the module.
24-
2519
### functions
2620

2721
#include_code functions noir_stdlib/src/meta/module.nr rust
2822

29-
Returns each function in the module.
23+
Returns each function defined in the module.
3024

3125
### has_named_attribute
3226

@@ -39,3 +33,15 @@ Returns true if this module has a custom attribute with the given name.
3933
#include_code is_contract noir_stdlib/src/meta/module.nr rust
4034

4135
`true` if this module is a contract module (was declared via `contract foo { ... }`).
36+
37+
### name
38+
39+
#include_code name noir_stdlib/src/meta/module.nr rust
40+
41+
Returns the name of the module.
42+
43+
### structs
44+
45+
#include_code structs noir_stdlib/src/meta/module.nr rust
46+
47+
Returns each struct defined in the module.

noir_stdlib/src/meta/module.nr

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ impl Module {
1010
// docs:end:has_named_attribute
1111

1212
#[builtin(module_is_contract)]
13-
// docs:start:is_contract
13+
// docs:start:is_contract
1414
comptime fn is_contract(self) -> bool {}
1515
// docs:end:is_contract
1616

1717
#[builtin(module_functions)]
18-
// docs:start:functions
18+
// docs:start:functions
1919
comptime fn functions(self) -> [FunctionDefinition] {}
2020
// docs:end:functions
2121

22+
#[builtin(module_structs)]
23+
// docs:start:structs
24+
comptime fn structs(self) -> [StructDefinition] {}
25+
// docs:end:structs
26+
2227
#[builtin(module_name)]
23-
// docs:start:name
28+
// docs:start:name
2429
comptime fn name(self) -> Quoted {}
2530
// docs:end:name
2631
}

test_programs/compile_success_empty/comptime_module/src/main.nr

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ mod foo {
33
#![some_attribute]
44
pub fn x() {}
55
pub fn y() {}
6+
7+
struct Struct1 {}
68
}
79

810
contract bar {}
@@ -74,6 +76,10 @@ fn main() {
7476
assert_eq(foo.functions().len(), 2);
7577
assert_eq(bar.functions().len(), 0);
7678

79+
// Check Module::structs
80+
assert_eq(foo.structs().len(), 1);
81+
assert_eq(bar.structs().len(), 0);
82+
7783
// Check Module::name
7884
assert_eq(foo.name(), quote { foo });
7985
assert_eq(bar.name(), quote { bar });

0 commit comments

Comments
 (0)