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: canonicalize arithmetic generics in array lengths #6494

Closed
Closed
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
46 changes: 46 additions & 0 deletions compiler/noirc_frontend/src/hir_def/types/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@

Type::CheckedCast { from: Box::new(from), to: Box::new(to) }
}
Type::Array(length, element) => Type::Array(
Box::new(length.canonicalize_helper(found_checked_cast, run_simplifications)),
element,
),
Comment on lines +114 to +117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see what this does but can you elaborate why it was added? The intent of canonicalize was to only work on numeric types. If we start trying to find and canonicalize types recursively like this we're missing a bunch of cases, e.g. numerics in struct generics, handling type variables, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this is blocking a sync but I still want to ensure its the right solution before just merging it

other => other,
}
}
Expand Down Expand Up @@ -360,6 +364,48 @@

use crate::hir_def::types::{BinaryTypeOperator, Kind, Type, TypeVariable, TypeVariableId};

#[test]
fn solves_n_plus_one_minus_one() {
let n = Type::TypeVariable(TypeVariable::unbound(TypeVariableId(0), Kind::u32()));
let n_plus_one = Type::InfixExpr(
Box::new(n.clone()),
BinaryTypeOperator::Addition,
Box::new(Type::Constant(FieldElement::one(), Kind::u32())),
);
let n_plus_one_minus_one = Type::InfixExpr(
Box::new(n_plus_one),
BinaryTypeOperator::Subtraction,
Box::new(Type::Constant(FieldElement::one(), Kind::u32())),
);

let canonicalized_typ = n_plus_one_minus_one.canonicalize();

assert_eq!(n, canonicalized_typ);
}

#[test]
fn solves_n_plus_one_minus_one_array_length() {
let n = Type::TypeVariable(TypeVariable::unbound(TypeVariableId(0), Kind::u32()));
let n_plus_one = Type::InfixExpr(
Box::new(n.clone()),
BinaryTypeOperator::Addition,
Box::new(Type::Constant(FieldElement::one(), Kind::u32())),
);
let n_plus_one_minus_one = Type::InfixExpr(
Box::new(n_plus_one),
BinaryTypeOperator::Subtraction,
Box::new(Type::Constant(FieldElement::one(), Kind::u32())),
);

let n_field_array = Type::Array(Box::new(n), Box::new(Type::FieldElement));
let n_plus_one_minus_one_field_array =
Type::Array(Box::new(n_plus_one_minus_one), Box::new(Type::FieldElement));

let canonicalized_typ = n_plus_one_minus_one_field_array.canonicalize();

assert_eq!(n_field_array, canonicalized_typ, "Could not simplify array length");
}

#[test]
fn instantiate_after_canonicalize_smoke_test() {
let field_element_kind = Kind::numeric(Type::FieldElement);
Expand Down Expand Up @@ -441,7 +487,7 @@
// Generate (arbitrary_unsigned_type, generator for that type)
fn arbitrary_unsigned_type_with_generator() -> BoxedStrategy<(Type, BoxedStrategy<FieldElement>)>
{
prop_oneof![

Check warning on line 490 in compiler/noirc_frontend/src/hir_def/types/arithmetic.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (oneof)
strategy::Just((Type::FieldElement, arbitrary_field_element().boxed())),
any::<IntegerBitSize>().prop_map(|bit_size| {
let typ = Type::Integer(Signedness::Unsigned, bit_size);
Expand Down Expand Up @@ -478,7 +524,7 @@
arbitrary_value: BoxedStrategy<FieldElement>,
num_variables: usize,
) -> impl Strategy<Value = Type> {
let leaf = prop_oneof![

Check warning on line 527 in compiler/noirc_frontend/src/hir_def/types/arithmetic.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (oneof)
arbitrary_variable(typ.clone(), num_variables),
arbitrary_value
.prop_map(move |value| Type::Constant(value, Kind::numeric(typ.clone()))),
Expand Down
Loading