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: Initialize numeric generics' type to a polymorphic integer when used in an expression #2179

Merged
merged 3 commits into from
Aug 8, 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
14 changes: 14 additions & 0 deletions crates/nargo_cli/tests/execution_success/generics/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ fn main(x: Field, y: Field) {
let two = y;
let nested_generics: Bar<Bar<Field>> = Bar { one, two, other: Bar { one, two, other: 0 } };
assert(nested_generics.other.other == bar1.get_other());

let _ = regression_2055([1, 2, 3]);
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
}

fn regression_2055<LEN>(bytes: [u8; LEN]) -> Field {
let mut f = 0;
let mut b = 1;
let mut len = LEN - 1; // FAILS
for i in 0..LEN {
let j = len - i;
f += (bytes[j] as Field) * b;
b *= 256;
}
f
}
11 changes: 10 additions & 1 deletion crates/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,16 @@ impl<'a> Resolver<'a> {
match self.interner.definition(hir_ident.id).kind {
DefinitionKind::Function(_) => {}
DefinitionKind::Global(_) => {}
DefinitionKind::GenericType(_) => {}
DefinitionKind::GenericType(_) => {
// Initialize numeric generics to a polymorphic integer type in case
// they're used in expressions. We must do this here since the type
// checker does not check definition kinds and otherwise expects
// parameters to already be typed.
if self.interner.id_type(hir_ident.id) == Type::Error {
let typ = Type::polymorphic_integer(self.interner);
self.interner.push_definition_type(hir_ident.id, typ);
}
}
// We ignore the above definition kinds because only local variables can be captured by closures.
DefinitionKind::Local(_) => {
self.resolve_local_variable(hir_ident, var_scope_index);
Expand Down