From df8c4ed4a67d5925bc28905ee12e1bc83f280128 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Wed, 6 Mar 2024 10:09:13 -0600 Subject: [PATCH 1/2] Fix global resolution order --- .../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 7f36af5b30e..0ff0c22c16a 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -331,11 +331,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( @@ -353,6 +348,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..7f2a0300c8e 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(), +}; From b166343f7fe049dc49ea35ffe698ee7368d09a3f Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Mon, 11 Mar 2024 08:14:42 -0500 Subject: [PATCH 2/2] Fix test formatting --- test_programs/execution_success/global_consts/src/main.nr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test_programs/execution_success/global_consts/src/main.nr b/test_programs/execution_success/global_consts/src/main.nr index 7f2a0300c8e..52ffe3e823b 100644 --- a/test_programs/execution_success/global_consts/src/main.nr +++ b/test_programs/execution_success/global_consts/src/main.nr @@ -115,12 +115,12 @@ struct Foo { struct Bar {} impl Bar { - fn get_a() -> Field { - 1 - } + fn get_a() -> Field { + 1 + } } // Regression for #1440 global foo = Foo { - a: Bar::get_a(), + a: Bar::get_a(), };