From cc4ca4bb5f4fed5f531a2040501fcc6ed53a9ab4 Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 19 Oct 2023 18:19:07 -0500 Subject: [PATCH] fix: Allow two `TypeVariable::Constant(N)` to unify even if their constants are not equal (#3225) --- compiler/noirc_frontend/src/hir_def/types.rs | 11 +++++++++-- .../tests/execution_success/slices/src/main.nr | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/compiler/noirc_frontend/src/hir_def/types.rs b/compiler/noirc_frontend/src/hir_def/types.rs index 73e3a2ca5a5..1f7baa0afaf 100644 --- a/compiler/noirc_frontend/src/hir_def/types.rs +++ b/compiler/noirc_frontend/src/hir_def/types.rs @@ -684,9 +684,16 @@ impl Type { *binding.borrow_mut() = TypeBinding::Bound(clone); Ok(()) } - TypeVariableKind::Constant(_) | TypeVariableKind::IntegerOrField => { - Err(UnificationError) + // The lengths don't match, but neither are set in stone so we can + // just set them both to NotConstant. See issue 2370 + TypeVariableKind::Constant(_) => { + // *length != target_length + drop(borrow); + *var.borrow_mut() = TypeBinding::Bound(Type::NotConstant); + *binding.borrow_mut() = TypeBinding::Bound(Type::NotConstant); + Ok(()) } + TypeVariableKind::IntegerOrField => Err(UnificationError), }, } } diff --git a/tooling/nargo_cli/tests/execution_success/slices/src/main.nr b/tooling/nargo_cli/tests/execution_success/slices/src/main.nr index e027c0f5ea2..8d53e013f96 100644 --- a/tooling/nargo_cli/tests/execution_success/slices/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/slices/src/main.nr @@ -49,6 +49,7 @@ fn main(x : Field, y : pub Field) { regression_2083(); // The parameters to this function must come from witness values (inputs to main) regression_merge_slices(x, y); + regression_2370(); } // Ensure that slices of struct/tuple values work. @@ -301,3 +302,10 @@ fn merge_slices_remove_between_ifs(x: Field, y: Field) -> [Field] { slice } + +// Previously, we'd get a type error when trying to assign an array of a different size to +// an existing array variable. Now, we infer the variable must be a slice. +fn regression_2370() { + let mut slice = []; + slice = [1, 2, 3]; +}