Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(globals): Accurately filter literals for resolving globals #2126

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/nargo_cli/tests/test_data/global_consts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion crates/nargo_cli/tests/test_data/strings/src/main.nr
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);

Expand Down
20 changes: 11 additions & 9 deletions crates/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<UnresolvedGlobal>,
) -> (Vec<UnresolvedGlobal>, Vec<UnresolvedGlobal>) {
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(
Expand Down