From 00d6494ae70b10e1872d96fb4e57ecb0b5f01787 Mon Sep 17 00:00:00 2001 From: jfecher Date: Tue, 12 Mar 2024 17:41:33 -0500 Subject: [PATCH] fix: Allow non-integer globals to reference struct methods (#4490) # 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. --- .../src/hir/def_collector/dc_crate.rs | 10 +++++----- .../execution_success/global_consts/src/main.nr | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs index 62febc89899..a5dfc738824 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -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( @@ -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( diff --git a/test_programs/execution_success/global_consts/src/main.nr b/test_programs/execution_success/global_consts/src/main.nr index 3c8ecc67a0c..52ffe3e823b 100644 --- a/test_programs/execution_success/global_consts/src/main.nr +++ b/test_programs/execution_success/global_consts/src/main.nr @@ -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(), +};