diff --git a/crates/nargo_cli/tests/test_data/global_consts/src/main.nr b/crates/nargo_cli/tests/test_data/global_consts/src/main.nr index 9bcca2b8071..2ed6e4593dd 100644 --- a/crates/nargo_cli/tests/test_data/global_consts/src/main.nr +++ b/crates/nargo_cli/tests/test_data/global_consts/src/main.nr @@ -12,12 +12,19 @@ struct Dummy { y: [Field; foo::MAGIC_NUMBER] } +struct Test { + v: Field, +} +global VALS: [Test; 1] = [Test { v: 100 }]; +global NESTED = [VALS, VALS]; + fn main(a: [Field; M + N - N], b: [Field; 30 + N / 2], c : pub [Field; foo::MAGIC_NUMBER], d: [Field; foo::bar::N]) { let test_struct = Dummy { x: d, y: c }; for i in 0..foo::MAGIC_NUMBER { assert(c[i] == foo::MAGIC_NUMBER); assert(test_struct.y[i] == foo::MAGIC_NUMBER); + assert(test_struct.y[i] != NESTED[1][0].v); } assert(N != M); diff --git a/crates/nargo_cli/tests/test_data/strings/src/main.nr b/crates/nargo_cli/tests/test_data/strings/src/main.nr index bee2370201c..edf5fff55b4 100644 --- a/crates/nargo_cli/tests/test_data/strings/src/main.nr +++ b/crates/nargo_cli/tests/test_data/strings/src/main.nr @@ -1,10 +1,13 @@ use dep::std; +// Test global string literals +global HELLO_WORLD = "hello world"; + fn main(message : pub str<11>, y : Field, hex_as_string : str<4>, hex_as_field : Field) { let mut bad_message = "hello world"; assert(message == "hello world"); - bad_message = "helld world"; + assert(message == HELLO_WORLD); let x = 10; let z = x * 5; std::println(10); @@ -16,6 +19,7 @@ fn main(message : pub str<11>, y : Field, hex_as_string : str<4>, hex_as_field : assert(y == 5); // Change to y != 5 to see how the later print statements are not called std::println(array); + bad_message = "helld world"; std::println(bad_message); assert(message != bad_message); diff --git a/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs b/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs index e974961a405..76fbea289be 100644 --- a/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -13,7 +13,7 @@ use crate::hir::Context; use crate::node_interner::{FuncId, NodeInterner, StmtId, StructId, TypeAliasId}; use crate::{ ExpressionKind, Generics, Ident, LetStatement, NoirFunction, NoirStruct, NoirTypeAlias, - ParsedModule, Shared, Type, TypeBinding, UnresolvedGenerics, UnresolvedType, + ParsedModule, Shared, Type, TypeBinding, UnresolvedGenerics, UnresolvedType, Literal, }; use fm::FileId; use iter_extended::vecmap; @@ -161,10 +161,10 @@ impl DefCollector { // // Additionally, we must resolve integer globals before structs since structs may refer to // the values of integer globals as numeric generics. - let (integer_globals, other_globals) = - filter_integer_globals(def_collector.collected_globals); + let (literal_globals, other_globals) = + filter_literal_globals(def_collector.collected_globals); - let mut file_global_ids = resolve_globals(context, integer_globals, crate_id, errors); + let mut file_global_ids = resolve_globals(context, literal_globals, crate_id, errors); resolve_type_aliases(context, def_collector.collected_type_aliases, crate_id, errors); @@ -274,13 +274,15 @@ where } /// Separate the globals Vec into two. The first element in the tuple will be the -/// integer literal globals, and the second will be all other globals. -fn filter_integer_globals( +/// literal globals, except for arrays, and the second will be all other globals. +/// We exclude array literals as they can contain complex types +fn filter_literal_globals( globals: Vec, ) -> (Vec, Vec) { - globals - .into_iter() - .partition(|global| matches!(&global.stmt_def.expression.kind, ExpressionKind::Literal(_))) + globals.into_iter().partition(|global| match &global.stmt_def.expression.kind { + ExpressionKind::Literal(literal) => !matches!(literal, Literal::Array(_)), + _ => false, + }) } fn resolve_globals(