Skip to content

Commit

Permalink
fix: Initialize numeric generics' type to a polymorphic integer when …
Browse files Browse the repository at this point in the history
…used in an expression (#2179)

* Fix 2055

* Add regression test
  • Loading branch information
jfecher authored Aug 8, 2023
1 parent 32d52d3 commit c74b228
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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]);
}

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

0 comments on commit c74b228

Please sign in to comment.