Skip to content

Commit

Permalink
feat: let Module::functions and Module::structs return them in de…
Browse files Browse the repository at this point in the history
…finition order (#6178)

# Description

## Problem

The above functions would return items in different order depending on
the run.

## Summary

## Additional Context



## Documentation

Check one:
- [x] No documentation needed.
- [ ] 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.
  • Loading branch information
asterite authored Sep 30, 2024
1 parent dcd3c52 commit dec9874
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 8 additions & 4 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2458,10 +2458,12 @@ fn module_functions(
let module_id = get_module(self_argument)?;
let module_data = interpreter.elaborator.get_module(module_id);
let func_ids = module_data
.value_definitions()
.definitions()
.definitions()
.iter()
.filter_map(|module_def_id| {
if let ModuleDefId::FunctionId(func_id) = module_def_id {
Some(Value::FunctionDefinition(func_id))
Some(Value::FunctionDefinition(*func_id))
} else {
None
}
Expand All @@ -2482,10 +2484,12 @@ fn module_structs(
let module_id = get_module(self_argument)?;
let module_data = interpreter.elaborator.get_module(module_id);
let struct_ids = module_data
.type_definitions()
.definitions()
.definitions()
.iter()
.filter_map(|module_def_id| {
if let ModuleDefId::TypeId(id) = module_def_id {
Some(Value::StructDefinition(id))
Some(Value::StructDefinition(*id))
} else {
None
}
Expand Down
16 changes: 14 additions & 2 deletions test_programs/compile_success_empty/comptime_module/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ mod foo {
#![some_attribute]
pub fn x() {}
pub fn y() {}
pub fn z() {}

pub struct Struct1 {}
pub struct Struct2 {}
pub struct Struct3 {}
}

contract bar {}
Expand Down Expand Up @@ -75,11 +78,20 @@ fn main() {
let another_module = quote { another_module }.as_module().unwrap();

// Check Module::functions
assert_eq(foo.functions().len(), 2);
let foo_functions = foo.functions();
assert_eq(foo_functions.len(), 3);
assert_eq(foo_functions[0].name(), quote { x });
assert_eq(foo_functions[1].name(), quote { y });
assert_eq(foo_functions[2].name(), quote { z });

assert_eq(bar.functions().len(), 0);

// Check Module::structs
assert_eq(foo.structs().len(), 1);
let foo_structs = foo.structs();
assert_eq(foo_structs.len(), 3);
assert_eq(foo_structs[0].name(), quote { Struct1 });
assert_eq(foo_structs[1].name(), quote { Struct2 });
assert_eq(foo_structs[2].name(), quote { Struct3 });
assert_eq(bar.structs().len(), 0);

// Check Module::name
Expand Down

0 comments on commit dec9874

Please sign in to comment.