From 2cc88ec105c2ac91c16010cdddba8f33f64b0b29 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Tue, 26 Mar 2024 09:49:09 -0500 Subject: [PATCH 1/3] Fix slice coercion --- compiler/noirc_frontend/src/hir_def/types.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/compiler/noirc_frontend/src/hir_def/types.rs b/compiler/noirc_frontend/src/hir_def/types.rs index 3c5627f739b..65592407d7d 100644 --- a/compiler/noirc_frontend/src/hir_def/types.rs +++ b/compiler/noirc_frontend/src/hir_def/types.rs @@ -1662,14 +1662,19 @@ fn convert_array_expression_to_slice( let as_slice = HirExpression::Ident(HirIdent::non_trait_method(as_slice_id, location)); let func = interner.push_expr(as_slice); - let arguments = vec![expression]; + // Copy the expression and give it a new ExprId. The old one + // will be mutated in place into a Call expression. + let argument = interner.expression(&expression); + let argument = interner.push_expr(argument); + interner.push_expr_type(argument, array_type.clone()); + interner.push_expr_location(argument, location.span, location.file); + + let arguments = vec![argument]; let call = HirExpression::Call(HirCallExpression { func, arguments, location }); - let call = interner.push_expr(call); + interner.replace_expr(&expression, call); - interner.push_expr_location(call, location.span, location.file); interner.push_expr_location(func, location.span, location.file); - - interner.push_expr_type(call, target_type.clone()); + interner.push_expr_type(expression, target_type.clone()); let func_type = Type::Function(vec![array_type], Box::new(target_type), Box::new(Type::Unit)); interner.push_expr_type(func, func_type); From b2163ec66c87fe81e93d6f25717fe3e305af2258 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Tue, 26 Mar 2024 09:53:53 -0500 Subject: [PATCH 2/3] Add test --- .../slice-coercion/Nargo.toml | 7 +++++++ .../slice-coercion/Prover.toml | 2 ++ .../slice-coercion/src/main.nr | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 test_programs/execution_success/slice-coercion/Nargo.toml create mode 100644 test_programs/execution_success/slice-coercion/Prover.toml create mode 100644 test_programs/execution_success/slice-coercion/src/main.nr diff --git a/test_programs/execution_success/slice-coercion/Nargo.toml b/test_programs/execution_success/slice-coercion/Nargo.toml new file mode 100644 index 00000000000..659677cc560 --- /dev/null +++ b/test_programs/execution_success/slice-coercion/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "slice_coercion" +type = "bin" +authors = [""] +compiler_version = ">=0.25.0" + +[dependencies] diff --git a/test_programs/execution_success/slice-coercion/Prover.toml b/test_programs/execution_success/slice-coercion/Prover.toml new file mode 100644 index 00000000000..09c44a855b0 --- /dev/null +++ b/test_programs/execution_success/slice-coercion/Prover.toml @@ -0,0 +1,2 @@ +first = 3 +expected = 3 diff --git a/test_programs/execution_success/slice-coercion/src/main.nr b/test_programs/execution_success/slice-coercion/src/main.nr new file mode 100644 index 00000000000..a3785e79afa --- /dev/null +++ b/test_programs/execution_success/slice-coercion/src/main.nr @@ -0,0 +1,19 @@ +struct Hasher { + fields: [Field], +} + +impl Hasher { + pub fn new() -> Self { + Self { fields: [] } + } + + pub fn add(&mut self, field: Field) { + self.fields = self.fields.push_back(field); + } +} + +fn main(expected: pub Field, first: Field) { + let mut hasher = Hasher::new(); + hasher.add(first); + assert(hasher.fields[0] == expected); +} From f3e89dc31644803921df2103be684e05f2646e93 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Tue, 26 Mar 2024 10:59:48 -0500 Subject: [PATCH 3/3] Rename test --- .../{slice-coercion => slice_coercion}/Nargo.toml | 0 .../{slice-coercion => slice_coercion}/Prover.toml | 0 .../{slice-coercion => slice_coercion}/src/main.nr | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename test_programs/execution_success/{slice-coercion => slice_coercion}/Nargo.toml (100%) rename test_programs/execution_success/{slice-coercion => slice_coercion}/Prover.toml (100%) rename test_programs/execution_success/{slice-coercion => slice_coercion}/src/main.nr (100%) diff --git a/test_programs/execution_success/slice-coercion/Nargo.toml b/test_programs/execution_success/slice_coercion/Nargo.toml similarity index 100% rename from test_programs/execution_success/slice-coercion/Nargo.toml rename to test_programs/execution_success/slice_coercion/Nargo.toml diff --git a/test_programs/execution_success/slice-coercion/Prover.toml b/test_programs/execution_success/slice_coercion/Prover.toml similarity index 100% rename from test_programs/execution_success/slice-coercion/Prover.toml rename to test_programs/execution_success/slice_coercion/Prover.toml diff --git a/test_programs/execution_success/slice-coercion/src/main.nr b/test_programs/execution_success/slice_coercion/src/main.nr similarity index 100% rename from test_programs/execution_success/slice-coercion/src/main.nr rename to test_programs/execution_success/slice_coercion/src/main.nr