Skip to content

Commit

Permalink
fix: Allow non-integer globals to reference struct methods (#4490)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #1440

## Summary\*

This was a fairly simple change - non-integer globals used to be
resolved before struct methods were even collected. I've moved this to
after methods are collected. Now global resolution is the first step of
resolving in general.

Integer globals are still resolved early since structs may refer to them
in numeric generics.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** 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
jfecher authored Mar 12, 2024
1 parent 604ee8f commit 00d6494
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
10 changes: 5 additions & 5 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,6 @@ impl DefCollector {
// Must resolve structs before we resolve globals.
errors.extend(resolve_structs(context, def_collector.collected_types, crate_id));

// We must wait to resolve non-integer globals until after we resolve structs since struct
// globals will need to reference the struct type they're initialized to to ensure they are valid.
resolved_globals.extend(resolve_globals(context, other_globals, crate_id));
errors.extend(resolved_globals.errors);

// Bind trait impls to their trait. Collect trait functions, that have a
// default implementation, which hasn't been overridden.
errors.extend(collect_trait_impls(
Expand All @@ -349,6 +344,11 @@ impl DefCollector {
// over trait methods if there are name conflicts.
errors.extend(collect_impls(context, crate_id, &def_collector.collected_impls));

// We must wait to resolve non-integer globals until after we resolve structs since struct
// globals will need to reference the struct type they're initialized to to ensure they are valid.
resolved_globals.extend(resolve_globals(context, other_globals, crate_id));
errors.extend(resolved_globals.errors);

// Resolve each function in the crate. This is now possible since imports have been resolved
let mut functions = Vec::new();
functions.extend(resolve_free_functions(
Expand Down
17 changes: 17 additions & 0 deletions test_programs/execution_success/global_consts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,20 @@ mod my_submodule {
x
}
}

struct Foo {
a: Field,
}

struct Bar {}

impl Bar {
fn get_a() -> Field {
1
}
}

// Regression for #1440
global foo = Foo {
a: Bar::get_a(),
};

0 comments on commit 00d6494

Please sign in to comment.