diff --git a/.noir-sync-commit b/.noir-sync-commit index 07427291ef5..10716ce4950 100644 --- a/.noir-sync-commit +++ b/.noir-sync-commit @@ -1 +1 @@ -070d7e71d6587679721437dd5b996478bd17ed85 +9c99a97ca8f42bee23cf97ebd724fdc51e647c60 diff --git a/noir/noir-repo/compiler/noirc_frontend/src/elaborator/mod.rs b/noir/noir-repo/compiler/noirc_frontend/src/elaborator/mod.rs index 9403e12496f..964b143b981 100644 --- a/noir/noir-repo/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/noir/noir-repo/compiler/noirc_frontend/src/elaborator/mod.rs @@ -165,6 +165,11 @@ pub struct Elaborator<'context> { /// Each element of the Vec represents a scope with every scope together making /// up all currently visible definitions. The first scope is always the global scope. comptime_scopes: Vec>, + + /// These are the globals that have yet to be elaborated. + /// This map is used to lazily evaluate these globals if they're encountered before + /// they are elaborated (e.g. in a function's type or another global's RHS). + unresolved_globals: BTreeMap, } impl<'context> Elaborator<'context> { @@ -192,6 +197,7 @@ impl<'context> Elaborator<'context> { trait_constraints: Vec::new(), current_trait_impl: None, comptime_scopes: vec![HashMap::default()], + unresolved_globals: BTreeMap::new(), } } @@ -208,6 +214,9 @@ impl<'context> Elaborator<'context> { // Additionally, we must resolve integer globals before structs since structs may refer to // the values of integer globals as numeric generics. let (literal_globals, non_literal_globals) = filter_literal_globals(items.globals); + for global in non_literal_globals { + this.unresolved_globals.insert(global.global_id, global); + } for global in literal_globals { this.elaborate_global(global); @@ -242,7 +251,7 @@ impl<'context> Elaborator<'context> { // We must wait to resolve non-literal 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. - for global in non_literal_globals { + while let Some((_, global)) = this.unresolved_globals.pop_first() { this.elaborate_global(global); } diff --git a/noir/noir-repo/compiler/noirc_frontend/src/elaborator/patterns.rs b/noir/noir-repo/compiler/noirc_frontend/src/elaborator/patterns.rs index 411caeba7b8..e337726b579 100644 --- a/noir/noir-repo/compiler/noirc_frontend/src/elaborator/patterns.rs +++ b/noir/noir-repo/compiler/noirc_frontend/src/elaborator/patterns.rs @@ -429,6 +429,9 @@ impl<'context> Elaborator<'context> { } } DefinitionKind::Global(global_id) => { + if let Some(global) = self.unresolved_globals.remove(&global_id) { + self.elaborate_global(global); + } if let Some(current_item) = self.current_item { self.interner.add_global_dependency(current_item, global_id); } diff --git a/noir/noir-repo/compiler/noirc_frontend/src/elaborator/types.rs b/noir/noir-repo/compiler/noirc_frontend/src/elaborator/types.rs index 159ad1c7493..1b611d8f71f 100644 --- a/noir/noir-repo/compiler/noirc_frontend/src/elaborator/types.rs +++ b/noir/noir-repo/compiler/noirc_frontend/src/elaborator/types.rs @@ -399,11 +399,16 @@ impl<'context> Elaborator<'context> { .or_else(|| self.resolve_trait_method_by_named_generic(path)) } - fn eval_global_as_array_length(&mut self, global: GlobalId, path: &Path) -> u32 { - let Some(stmt) = self.interner.get_global_let_statement(global) else { - let path = path.clone(); - self.push_err(ResolverError::NoSuchNumericTypeVariable { path }); - return 0; + fn eval_global_as_array_length(&mut self, global_id: GlobalId, path: &Path) -> u32 { + let Some(stmt) = self.interner.get_global_let_statement(global_id) else { + if let Some(global) = self.unresolved_globals.remove(&global_id) { + self.elaborate_global(global); + return self.eval_global_as_array_length(global_id, path); + } else { + let path = path.clone(); + self.push_err(ResolverError::NoSuchNumericTypeVariable { path }); + return 0; + } }; let length = stmt.expression; diff --git a/noir/noir-repo/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs b/noir/noir-repo/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs index 096bab2b47e..9b47a104a40 100644 --- a/noir/noir-repo/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/noir/noir-repo/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -139,7 +139,7 @@ pub struct UnresolvedTypeAlias { pub type_alias_def: NoirTypeAlias, } -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct UnresolvedGlobal { pub file_id: FileId, pub module_id: LocalModuleId, diff --git a/noir/noir-repo/compiler/noirc_frontend/src/node_interner.rs b/noir/noir-repo/compiler/noirc_frontend/src/node_interner.rs index 40ca9ce04e0..327900af641 100644 --- a/noir/noir-repo/compiler/noirc_frontend/src/node_interner.rs +++ b/noir/noir-repo/compiler/noirc_frontend/src/node_interner.rs @@ -279,7 +279,7 @@ impl DefinitionId { } /// An ID for a global value -#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy, PartialOrd, Ord)] pub struct GlobalId(usize); impl GlobalId {