From 1f3a513a9a120cdf3bf8be4d82002775799a95da Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 29 May 2025 10:52:32 -0300 Subject: [PATCH 1/4] Rename serialize to serialize_1 --- .../{serialize => serialize_1}/Nargo.toml | 2 +- .../{serialize => serialize_1}/src/main.nr | 0 .../serialize_1/execute__tests__expanded.snap | 67 +++++++++++++++++++ ..._tests__force_brillig_false_inliner_0.snap | 0 4 files changed, 68 insertions(+), 1 deletion(-) rename test_programs/compile_success_empty/{serialize => serialize_1}/Nargo.toml (80%) rename test_programs/compile_success_empty/{serialize => serialize_1}/src/main.nr (100%) create mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_1/execute__tests__expanded.snap rename tooling/nargo_cli/tests/snapshots/compile_success_empty/{serialize => serialize_1}/execute__tests__force_brillig_false_inliner_0.snap (100%) diff --git a/test_programs/compile_success_empty/serialize/Nargo.toml b/test_programs/compile_success_empty/serialize_1/Nargo.toml similarity index 80% rename from test_programs/compile_success_empty/serialize/Nargo.toml rename to test_programs/compile_success_empty/serialize_1/Nargo.toml index 2cf87765b8a..62bbf337b11 100644 --- a/test_programs/compile_success_empty/serialize/Nargo.toml +++ b/test_programs/compile_success_empty/serialize_1/Nargo.toml @@ -1,5 +1,5 @@ [package] -name = "serialize" +name = "serialize_1" type = "bin" authors = [""] compiler_version = ">=0.32.0" diff --git a/test_programs/compile_success_empty/serialize/src/main.nr b/test_programs/compile_success_empty/serialize_1/src/main.nr similarity index 100% rename from test_programs/compile_success_empty/serialize/src/main.nr rename to test_programs/compile_success_empty/serialize_1/src/main.nr diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_1/execute__tests__expanded.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_1/execute__tests__expanded.snap new file mode 100644 index 00000000000..297b3c52860 --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_1/execute__tests__expanded.snap @@ -0,0 +1,67 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: expanded_code +--- +trait Serialize { + let Size: u32; + + fn serialize(self) -> [Field; Size]; +} + +impl Serialize for (A, B) where A: Serialize, B: Serialize { + type Size = ::Size + ::Size; + + fn serialize(self) -> [Field; ::Size + ::Size] where A: Serialize, B: Serialize { + let mut array: [Field; ::Size + ::Size] = std::mem::zeroed(); + let a: [Field; ::Size] = Serialize::serialize(self.0); + let b: [Field; ::Size] = Serialize::serialize(self.1); + for i in 0..a.len() { + array[i] = a[i]; + }; + for i in 0..b.len() { + { + let i_3793: u32 = i + a.len(); + array[i_3793] = b[i]; + } + }; + array + } +} + +impl Serialize for [T; N] where T: Serialize { + type Size = N * ::Size; + + fn serialize(self) -> [Field; N * ::Size] where T: Serialize { + let mut array: [Field; N * ::Size] = std::mem::zeroed(); + let mut array_i: Field = 0; + { + let ___i0: Self = self; + for ___i1 in 0..___i0.len() { + let elem: T = ___i0[___i1]; + { + let elem_fields: [Field; ::Size] = Serialize::serialize(elem); + for i in 0..elem_fields.len() { + array[array_i] = elem_fields[i]; + array_i = array_i + 1; + } + } + } + }; + array + } +} + +impl Serialize for Field { + let Size: u32 = 1; + + fn serialize(self) -> [Self; 1] { + [self] + } +} + +fn main() { + let x: ((Field, [Field; 3]), [Field; 4]) = ((1, [2, 3, 4]), [5, 6, 7, 8]); + assert(x.serialize().len() == 8); +} + +// Warning: the generated code has syntax errors diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize/execute__tests__force_brillig_false_inliner_0.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_1/execute__tests__force_brillig_false_inliner_0.snap similarity index 100% rename from tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize/execute__tests__force_brillig_false_inliner_0.snap rename to tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_1/execute__tests__force_brillig_false_inliner_0.snap From 2a6a489bc44dd47165d924877abe662c62e33a4d Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 29 May 2025 11:20:13 -0300 Subject: [PATCH 2/4] fix: merge expr bindings with instantiations bindings during monomorphization --- .../src/monomorphization/mod.rs | 11 ++- .../serialize_2/Nargo.toml | 7 ++ .../serialize_2/src/main.nr | 64 ++++++++++++++++++ .../serialize_3/Nargo.toml | 7 ++ .../serialize_3/src/main.nr | 27 ++++++++ .../serialize_4/Nargo.toml | 7 ++ .../serialize_4/src/main.nr | 42 ++++++++++++ .../serialize_5/Nargo.toml | 7 ++ .../serialize_5/src/main.nr | 64 ++++++++++++++++++ tooling/nargo_cli/build.rs | 12 +++- .../serialize_2/execute__tests__expanded.snap | 67 +++++++++++++++++++ ..._tests__force_brillig_false_inliner_0.snap | 26 +++++++ .../serialize_3/execute__tests__expanded.snap | 34 ++++++++++ ..._tests__force_brillig_false_inliner_0.snap | 26 +++++++ .../serialize_4/execute__tests__expanded.snap | 41 ++++++++++++ ..._tests__force_brillig_false_inliner_0.snap | 26 +++++++ .../execute__tests__expanded.snap | 4 +- ..._tests__force_brillig_false_inliner_0.snap | 26 +++++++ 18 files changed, 492 insertions(+), 6 deletions(-) create mode 100644 test_programs/compile_success_empty/serialize_2/Nargo.toml create mode 100644 test_programs/compile_success_empty/serialize_2/src/main.nr create mode 100644 test_programs/compile_success_empty/serialize_3/Nargo.toml create mode 100644 test_programs/compile_success_empty/serialize_3/src/main.nr create mode 100644 test_programs/compile_success_empty/serialize_4/Nargo.toml create mode 100644 test_programs/compile_success_empty/serialize_4/src/main.nr create mode 100644 test_programs/compile_success_empty/serialize_5/Nargo.toml create mode 100644 test_programs/compile_success_empty/serialize_5/src/main.nr create mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_2/execute__tests__expanded.snap create mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_2/execute__tests__force_brillig_false_inliner_0.snap create mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_3/execute__tests__expanded.snap create mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_3/execute__tests__force_brillig_false_inliner_0.snap create mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_4/execute__tests__expanded.snap create mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_4/execute__tests__force_brillig_false_inliner_0.snap rename tooling/nargo_cli/tests/snapshots/compile_success_empty/{serialize_1 => serialize_5}/execute__tests__expanded.snap (94%) create mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__force_brillig_false_inliner_0.snap diff --git a/compiler/noirc_frontend/src/monomorphization/mod.rs b/compiler/noirc_frontend/src/monomorphization/mod.rs index 01c6c273d12..e8e72375c07 100644 --- a/compiler/noirc_frontend/src/monomorphization/mod.rs +++ b/compiler/noirc_frontend/src/monomorphization/mod.rs @@ -2511,7 +2511,7 @@ pub fn perform_impl_bindings( } pub fn resolve_trait_method( - interner: &NodeInterner, + interner: &mut NodeInterner, method: TraitMethodId, expr_id: ExprId, ) -> Result { @@ -2531,7 +2531,14 @@ pub fn resolve_trait_method( &trait_generics.ordered, &trait_generics.named, ) { - Ok((TraitImplKind::Normal(impl_id), _instantiation_bindings)) => impl_id, + Ok((TraitImplKind::Normal(impl_id), instantiation_bindings)) => { + // Insert any additional instantiation bindings into this expression's instantiation bindings. + // This is similar to what's done in `verify_trait_constraint` in the frontend. + let mut bindings = interner.get_instantiation_bindings(expr_id).clone(); + bindings.extend(instantiation_bindings); + interner.store_instantiation_bindings(expr_id, bindings); + impl_id + } Ok((TraitImplKind::Assumed { .. }, _instantiation_bindings)) => { return Err(InterpreterError::NoImpl { location }); } diff --git a/test_programs/compile_success_empty/serialize_2/Nargo.toml b/test_programs/compile_success_empty/serialize_2/Nargo.toml new file mode 100644 index 00000000000..0534529f5d4 --- /dev/null +++ b/test_programs/compile_success_empty/serialize_2/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "serialize_2" +type = "bin" +authors = [""] +compiler_version = ">=0.32.0" + +[dependencies] diff --git a/test_programs/compile_success_empty/serialize_2/src/main.nr b/test_programs/compile_success_empty/serialize_2/src/main.nr new file mode 100644 index 00000000000..5eec603d83e --- /dev/null +++ b/test_programs/compile_success_empty/serialize_2/src/main.nr @@ -0,0 +1,64 @@ +trait Serialize { + let Size: u32; + + // Note that Rust disallows referencing constants here! + fn serialize(self) -> [Field; Self::Size]; +} + +impl Serialize for (A, B) +where + A: Serialize, + B: Serialize, +{ + let Size = ::Size + ::Size; + + fn serialize(self: Self) -> [Field; Self::Size] { + let mut array: [Field; Self::Size] = std::mem::zeroed(); + let a = self.0.serialize(); + let b = self.1.serialize(); + + for i in 0..a.len() { + array[i] = a[i]; + } + for i in 0..b.len() { + array[i + a.len()] = b[i]; + } + array + } +} + +impl Serialize for [T; N] +where + T: Serialize, +{ + let Size = ::Size * N; + + fn serialize(self: Self) -> [Field; Self::Size] { + let mut array: [Field; Self::Size] = std::mem::zeroed(); + let mut array_i = 0; + + for elem in self { + let elem_fields = elem.serialize(); + + for i in 0..elem_fields.len() { + array[array_i] = elem_fields[i]; + array_i += 1; + } + } + + array + } +} + +impl Serialize for Field { + let Size: u32 = 1; + + fn serialize(self) -> [Field; Self::Size] { + [self] + } +} + +fn main() { + let x = (((1, [2, 3, 4]), [5, 6, 7, 8]), 9); + assert_eq(x.serialize().len(), 9); +} diff --git a/test_programs/compile_success_empty/serialize_3/Nargo.toml b/test_programs/compile_success_empty/serialize_3/Nargo.toml new file mode 100644 index 00000000000..33dca9c1a65 --- /dev/null +++ b/test_programs/compile_success_empty/serialize_3/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "serialize_3" +type = "bin" +authors = [""] +compiler_version = ">=0.32.0" + +[dependencies] diff --git a/test_programs/compile_success_empty/serialize_3/src/main.nr b/test_programs/compile_success_empty/serialize_3/src/main.nr new file mode 100644 index 00000000000..c1be0103e09 --- /dev/null +++ b/test_programs/compile_success_empty/serialize_3/src/main.nr @@ -0,0 +1,27 @@ +trait Serialize { + let Size: u32; + + fn serialize(self); +} + +impl Serialize for Field { + let Size: u32 = 1; + + fn serialize(self) {} +} + +impl Serialize for (A,) +where + A: Serialize, +{ + let Size = ::Size; + + fn serialize(self: Self) { + self.0.serialize(); + } +} + +fn main() { + let x = ((1,),); + x.serialize(); +} \ No newline at end of file diff --git a/test_programs/compile_success_empty/serialize_4/Nargo.toml b/test_programs/compile_success_empty/serialize_4/Nargo.toml new file mode 100644 index 00000000000..d6f36ccf4ae --- /dev/null +++ b/test_programs/compile_success_empty/serialize_4/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "serialize_4" +type = "bin" +authors = [""] +compiler_version = ">=0.32.0" + +[dependencies] diff --git a/test_programs/compile_success_empty/serialize_4/src/main.nr b/test_programs/compile_success_empty/serialize_4/src/main.nr new file mode 100644 index 00000000000..7929077d1e8 --- /dev/null +++ b/test_programs/compile_success_empty/serialize_4/src/main.nr @@ -0,0 +1,42 @@ +trait Serialize { + let Size: u32; + + fn serialize(self) -> [Field; Self::Size]; +} + +impl Serialize for (A, B) +where + A: Serialize, + B: Serialize, +{ + let Size = ::Size + ::Size; + + fn serialize(self: Self) -> [Field; Self::Size] { + let _ = self.0.serialize(); + [0; Self::Size] + } +} + +impl Serialize for [T; N] +where + T: Serialize, +{ + let Size = ::Size * N; + + fn serialize(self: Self) -> [Field; Self::Size] { + [0; Self::Size] + } +} + +impl Serialize for Field { + let Size: u32 = 1; + + fn serialize(self) -> [Field; Self::Size] { + [self] + } +} + +fn main() { + let x = (((1, [2, 3, 4]), [5, 6, 7, 8]), 9); + assert_eq(x.serialize().len(), 9); +} \ No newline at end of file diff --git a/test_programs/compile_success_empty/serialize_5/Nargo.toml b/test_programs/compile_success_empty/serialize_5/Nargo.toml new file mode 100644 index 00000000000..e3fcae3f369 --- /dev/null +++ b/test_programs/compile_success_empty/serialize_5/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "serialize_5" +type = "bin" +authors = [""] +compiler_version = ">=0.32.0" + +[dependencies] diff --git a/test_programs/compile_success_empty/serialize_5/src/main.nr b/test_programs/compile_success_empty/serialize_5/src/main.nr new file mode 100644 index 00000000000..050e4400019 --- /dev/null +++ b/test_programs/compile_success_empty/serialize_5/src/main.nr @@ -0,0 +1,64 @@ +trait Serialize { + let Size: u32; + + // Note that Rust disallows referencing constants here! + fn serialize(self) -> [Field; Self::Size]; +} + +impl Serialize for (A, B) +where + A: Serialize, + B: Serialize, +{ + let Size = ::Size + ::Size; + + fn serialize(self: Self) -> [Field; Self::Size] { + let mut array: [Field; Self::Size] = std::mem::zeroed(); + let a = self.0.serialize(); + let b = self.1.serialize(); + + for i in 0..a.len() { + array[i] = a[i]; + } + for i in 0..b.len() { + array[i + a.len()] = b[i]; + } + array + } +} + +impl Serialize for [T; N] +where + T: Serialize, +{ + let Size = ::Size * N; + + fn serialize(self: Self) -> [Field; Self::Size] { + let mut array: [Field; Self::Size] = std::mem::zeroed(); + let mut array_i = 0; + + for elem in self { + let elem_fields = elem.serialize(); + + for i in 0..elem_fields.len() { + array[array_i] = elem_fields[i]; + array_i += 1; + } + } + + array + } +} + +impl Serialize for Field { + let Size: u32 = 1; + + fn serialize(self) -> [Field; Self::Size] { + [self] + } +} + +fn main() { + let x = (1, [2, 3, 4]); + assert_eq(x.serialize().len(), 4); +} \ No newline at end of file diff --git a/tooling/nargo_cli/build.rs b/tooling/nargo_cli/build.rs index 63a79a9fe51..a0ea47be704 100644 --- a/tooling/nargo_cli/build.rs +++ b/tooling/nargo_cli/build.rs @@ -145,7 +145,7 @@ const TESTS_WITHOUT_STDOUT_CHECK: [&str; 0] = []; /// These tests are ignored because of existing bugs in `nargo expand`. /// As the bugs are fixed these tests should be removed from this list. /// (some are ignored on purpose for the same reason as `IGNORED_NARGO_EXPAND_EXECUTION_TESTS`) -const IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_EMPTY_TESTS: [&str; 17] = [ +const IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_EMPTY_TESTS: [&str; 21] = [ // bug "associated_type_bounds", // bug @@ -163,7 +163,15 @@ const IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_EMPTY_TESTS: [&str; 17] = [ // bug "regression_7038_4", // bug - "serialize", + "serialize_1", + // bug + "serialize_2", + // bug + "serialize_3", + // bug + "serialize_4", + // bug + "serialize_5", // bug "trait_allowed_item_name_matches", // bug diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_2/execute__tests__expanded.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_2/execute__tests__expanded.snap new file mode 100644 index 00000000000..ba5ebb3d93c --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_2/execute__tests__expanded.snap @@ -0,0 +1,67 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: expanded_code +--- +trait Serialize { + let Size: u32; + + fn serialize(self) -> [Field; Size]; +} + +impl Serialize for (A, B) where A: Serialize, B: Serialize { + type Size = ::Size + ::Size; + + fn serialize(self) -> [Field; ::Size + ::Size] where A: Serialize, B: Serialize { + let mut array: [Field; ::Size + ::Size] = std::mem::zeroed(); + let a: [Field; ::Size] = Serialize::serialize(self.0); + let b: [Field; ::Size] = Serialize::serialize(self.1); + for i in 0..a.len() { + array[i] = a[i]; + }; + for i in 0..b.len() { + { + let i_3793: u32 = i + a.len(); + array[i_3793] = b[i]; + } + }; + array + } +} + +impl Serialize for [T; N] where T: Serialize { + type Size = N * ::Size; + + fn serialize(self) -> [Field; N * ::Size] where T: Serialize { + let mut array: [Field; N * ::Size] = std::mem::zeroed(); + let mut array_i: Field = 0; + { + let ___i0: Self = self; + for ___i1 in 0..___i0.len() { + let elem: T = ___i0[___i1]; + { + let elem_fields: [Field; ::Size] = Serialize::serialize(elem); + for i in 0..elem_fields.len() { + array[array_i] = elem_fields[i]; + array_i = array_i + 1; + } + } + } + }; + array + } +} + +impl Serialize for Field { + let Size: u32 = 1; + + fn serialize(self) -> [Self; 1] { + [self] + } +} + +fn main() { + let x: (((Field, [Field; 3]), [Field; 4]), Field) = (((1, [2, 3, 4]), [5, 6, 7, 8]), 9); + assert(x.serialize().len() == 9); +} + +// Warning: the generated code has syntax errors diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_2/execute__tests__force_brillig_false_inliner_0.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_2/execute__tests__force_brillig_false_inliner_0.snap new file mode 100644 index 00000000000..7de940827c8 --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_2/execute__tests__force_brillig_false_inliner_0.snap @@ -0,0 +1,26 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: artifact +--- +{ + "noir_version": "[noir_version]", + "hash": "[hash]", + "abi": { + "parameters": [], + "return_type": null, + "error_types": {} + }, + "bytecode": [ + "func 0", + "current witness index : _0", + "private parameters indices : []", + "public parameters indices : []", + "return value indices : []" + ], + "debug_symbols": "XY5BCsQwCEXv4rqLWfcqw1BsaosgJtikMITefWyYQOlK/3/6tcJCc9km1jXuML4rzMYivE0SA2aO6m49B+hyykbkFty4byU00gyjFpEBDpTShvaE2mpGc/oagHTx6oErC13d+XGBge158UBjnIX+ci0abjR/Uyf942Qx0FKMrqTGPPsH", + "file_map": {}, + "names": [ + "main" + ], + "brillig_names": [] +} diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_3/execute__tests__expanded.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_3/execute__tests__expanded.snap new file mode 100644 index 00000000000..7d4d0ef29c9 --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_3/execute__tests__expanded.snap @@ -0,0 +1,34 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: expanded_code +--- +trait Serialize { + let Size: u32; + + fn serialize(self); +} + +impl Serialize for Field { + let Size: u32 = 1; + + fn serialize(self) {} +} + +impl Serialize for (A,) +where + A: Serialize, +{ + type Size = ::Size; + + fn serialize(self) + where + A: Serialize, + { + Serialize::serialize(self.0); + } +} + +fn main() { + let x: ((Field,),) = ((1,),); + x.serialize(); +} diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_3/execute__tests__force_brillig_false_inliner_0.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_3/execute__tests__force_brillig_false_inliner_0.snap new file mode 100644 index 00000000000..7de940827c8 --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_3/execute__tests__force_brillig_false_inliner_0.snap @@ -0,0 +1,26 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: artifact +--- +{ + "noir_version": "[noir_version]", + "hash": "[hash]", + "abi": { + "parameters": [], + "return_type": null, + "error_types": {} + }, + "bytecode": [ + "func 0", + "current witness index : _0", + "private parameters indices : []", + "public parameters indices : []", + "return value indices : []" + ], + "debug_symbols": "XY5BCsQwCEXv4rqLWfcqw1BsaosgJtikMITefWyYQOlK/3/6tcJCc9km1jXuML4rzMYivE0SA2aO6m49B+hyykbkFty4byU00gyjFpEBDpTShvaE2mpGc/oagHTx6oErC13d+XGBge158UBjnIX+ci0abjR/Uyf942Qx0FKMrqTGPPsH", + "file_map": {}, + "names": [ + "main" + ], + "brillig_names": [] +} diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_4/execute__tests__expanded.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_4/execute__tests__expanded.snap new file mode 100644 index 00000000000..0040ddafe24 --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_4/execute__tests__expanded.snap @@ -0,0 +1,41 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: expanded_code +--- +trait Serialize { + let Size: u32; + + fn serialize(self) -> [Field; Size]; +} + +impl Serialize for (A, B) where A: Serialize, B: Serialize { + type Size = ::Size + ::Size; + + fn serialize(self) -> [Field; ::Size + ::Size] where A: Serialize, B: Serialize { + let _: [Field; ::Size] = Serialize::serialize(self.0); + [0; ::Size + ::Size] + } +} + +impl Serialize for [T; N] where T: Serialize { + type Size = N * ::Size; + + fn serialize(self) -> [Field; N * ::Size] where T: Serialize { + [0; N * ::Size] + } +} + +impl Serialize for Field { + let Size: u32 = 1; + + fn serialize(self) -> [Self; 1] { + [self] + } +} + +fn main() { + let x: (((Field, [Field; 3]), [Field; 4]), Field) = (((1, [2, 3, 4]), [5, 6, 7, 8]), 9); + assert(x.serialize().len() == 9); +} + +// Warning: the generated code has syntax errors diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_4/execute__tests__force_brillig_false_inliner_0.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_4/execute__tests__force_brillig_false_inliner_0.snap new file mode 100644 index 00000000000..7de940827c8 --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_4/execute__tests__force_brillig_false_inliner_0.snap @@ -0,0 +1,26 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: artifact +--- +{ + "noir_version": "[noir_version]", + "hash": "[hash]", + "abi": { + "parameters": [], + "return_type": null, + "error_types": {} + }, + "bytecode": [ + "func 0", + "current witness index : _0", + "private parameters indices : []", + "public parameters indices : []", + "return value indices : []" + ], + "debug_symbols": "XY5BCsQwCEXv4rqLWfcqw1BsaosgJtikMITefWyYQOlK/3/6tcJCc9km1jXuML4rzMYivE0SA2aO6m49B+hyykbkFty4byU00gyjFpEBDpTShvaE2mpGc/oagHTx6oErC13d+XGBge158UBjnIX+ci0abjR/Uyf942Qx0FKMrqTGPPsH", + "file_map": {}, + "names": [ + "main" + ], + "brillig_names": [] +} diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_1/execute__tests__expanded.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__expanded.snap similarity index 94% rename from tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_1/execute__tests__expanded.snap rename to tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__expanded.snap index 297b3c52860..a39af5292e2 100644 --- a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_1/execute__tests__expanded.snap +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__expanded.snap @@ -60,8 +60,8 @@ impl Serialize for Field { } fn main() { - let x: ((Field, [Field; 3]), [Field; 4]) = ((1, [2, 3, 4]), [5, 6, 7, 8]); - assert(x.serialize().len() == 8); + let x: (Field, [Field; 3]) = (1, [2, 3, 4]); + assert(x.serialize().len() == 4); } // Warning: the generated code has syntax errors diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__force_brillig_false_inliner_0.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__force_brillig_false_inliner_0.snap new file mode 100644 index 00000000000..7de940827c8 --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__force_brillig_false_inliner_0.snap @@ -0,0 +1,26 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: artifact +--- +{ + "noir_version": "[noir_version]", + "hash": "[hash]", + "abi": { + "parameters": [], + "return_type": null, + "error_types": {} + }, + "bytecode": [ + "func 0", + "current witness index : _0", + "private parameters indices : []", + "public parameters indices : []", + "return value indices : []" + ], + "debug_symbols": "XY5BCsQwCEXv4rqLWfcqw1BsaosgJtikMITefWyYQOlK/3/6tcJCc9km1jXuML4rzMYivE0SA2aO6m49B+hyykbkFty4byU00gyjFpEBDpTShvaE2mpGc/oagHTx6oErC13d+XGBge158UBjnIX+ci0abjR/Uyf942Qx0FKMrqTGPPsH", + "file_map": {}, + "names": [ + "main" + ], + "brillig_names": [] +} From c8d8d1502efed98b1913908edd757959fb5b3c60 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 29 May 2025 11:37:33 -0300 Subject: [PATCH 3/4] nargo fmt --- test_programs/compile_success_empty/serialize_3/src/main.nr | 2 +- test_programs/compile_success_empty/serialize_4/src/main.nr | 2 +- test_programs/compile_success_empty/serialize_5/src/main.nr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test_programs/compile_success_empty/serialize_3/src/main.nr b/test_programs/compile_success_empty/serialize_3/src/main.nr index c1be0103e09..32aff8dfc2e 100644 --- a/test_programs/compile_success_empty/serialize_3/src/main.nr +++ b/test_programs/compile_success_empty/serialize_3/src/main.nr @@ -24,4 +24,4 @@ where fn main() { let x = ((1,),); x.serialize(); -} \ No newline at end of file +} diff --git a/test_programs/compile_success_empty/serialize_4/src/main.nr b/test_programs/compile_success_empty/serialize_4/src/main.nr index 7929077d1e8..b7b1df24f22 100644 --- a/test_programs/compile_success_empty/serialize_4/src/main.nr +++ b/test_programs/compile_success_empty/serialize_4/src/main.nr @@ -39,4 +39,4 @@ impl Serialize for Field { fn main() { let x = (((1, [2, 3, 4]), [5, 6, 7, 8]), 9); assert_eq(x.serialize().len(), 9); -} \ No newline at end of file +} diff --git a/test_programs/compile_success_empty/serialize_5/src/main.nr b/test_programs/compile_success_empty/serialize_5/src/main.nr index 050e4400019..41879392c4f 100644 --- a/test_programs/compile_success_empty/serialize_5/src/main.nr +++ b/test_programs/compile_success_empty/serialize_5/src/main.nr @@ -61,4 +61,4 @@ impl Serialize for Field { fn main() { let x = (1, [2, 3, 4]); assert_eq(x.serialize().len(), 4); -} \ No newline at end of file +} From a5ad581ae48b0282229ce70e7bc5ad98c0cee9fc Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 29 May 2025 12:51:45 -0300 Subject: [PATCH 4/4] Remove extra serialize test --- .../serialize_1/src/main.nr | 4 +- .../serialize_2/src/main.nr | 59 +++------------- .../serialize_3/src/main.nr | 37 +++++++--- .../serialize_4/src/main.nr | 32 +++++++-- .../serialize_5/Nargo.toml | 7 -- .../serialize_5/src/main.nr | 64 ------------------ tooling/nargo_cli/build.rs | 4 +- .../serialize_5/execute__tests__expanded.snap | 67 ------------------- ..._tests__force_brillig_false_inliner_0.snap | 26 ------- 9 files changed, 67 insertions(+), 233 deletions(-) delete mode 100644 test_programs/compile_success_empty/serialize_5/Nargo.toml delete mode 100644 test_programs/compile_success_empty/serialize_5/src/main.nr delete mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__expanded.snap delete mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__force_brillig_false_inliner_0.snap diff --git a/test_programs/compile_success_empty/serialize_1/src/main.nr b/test_programs/compile_success_empty/serialize_1/src/main.nr index a11fdf570d0..5eec603d83e 100644 --- a/test_programs/compile_success_empty/serialize_1/src/main.nr +++ b/test_programs/compile_success_empty/serialize_1/src/main.nr @@ -59,6 +59,6 @@ impl Serialize for Field { } fn main() { - let x = ((1, [2, 3, 4]), [5, 6, 7, 8]); - assert_eq(x.serialize().len(), 8); + let x = (((1, [2, 3, 4]), [5, 6, 7, 8]), 9); + assert_eq(x.serialize().len(), 9); } diff --git a/test_programs/compile_success_empty/serialize_2/src/main.nr b/test_programs/compile_success_empty/serialize_2/src/main.nr index 5eec603d83e..32aff8dfc2e 100644 --- a/test_programs/compile_success_empty/serialize_2/src/main.nr +++ b/test_programs/compile_success_empty/serialize_2/src/main.nr @@ -1,64 +1,27 @@ trait Serialize { let Size: u32; - // Note that Rust disallows referencing constants here! - fn serialize(self) -> [Field; Self::Size]; + fn serialize(self); } -impl Serialize for (A, B) -where - A: Serialize, - B: Serialize, -{ - let Size = ::Size + ::Size; - - fn serialize(self: Self) -> [Field; Self::Size] { - let mut array: [Field; Self::Size] = std::mem::zeroed(); - let a = self.0.serialize(); - let b = self.1.serialize(); +impl Serialize for Field { + let Size: u32 = 1; - for i in 0..a.len() { - array[i] = a[i]; - } - for i in 0..b.len() { - array[i + a.len()] = b[i]; - } - array - } + fn serialize(self) {} } -impl Serialize for [T; N] +impl Serialize for (A,) where - T: Serialize, + A: Serialize, { - let Size = ::Size * N; - - fn serialize(self: Self) -> [Field; Self::Size] { - let mut array: [Field; Self::Size] = std::mem::zeroed(); - let mut array_i = 0; - - for elem in self { - let elem_fields = elem.serialize(); - - for i in 0..elem_fields.len() { - array[array_i] = elem_fields[i]; - array_i += 1; - } - } - - array - } -} - -impl Serialize for Field { - let Size: u32 = 1; + let Size = ::Size; - fn serialize(self) -> [Field; Self::Size] { - [self] + fn serialize(self: Self) { + self.0.serialize(); } } fn main() { - let x = (((1, [2, 3, 4]), [5, 6, 7, 8]), 9); - assert_eq(x.serialize().len(), 9); + let x = ((1,),); + x.serialize(); } diff --git a/test_programs/compile_success_empty/serialize_3/src/main.nr b/test_programs/compile_success_empty/serialize_3/src/main.nr index 32aff8dfc2e..b7b1df24f22 100644 --- a/test_programs/compile_success_empty/serialize_3/src/main.nr +++ b/test_programs/compile_success_empty/serialize_3/src/main.nr @@ -1,27 +1,42 @@ trait Serialize { let Size: u32; - fn serialize(self); + fn serialize(self) -> [Field; Self::Size]; } -impl Serialize for Field { - let Size: u32 = 1; +impl Serialize for (A, B) +where + A: Serialize, + B: Serialize, +{ + let Size = ::Size + ::Size; - fn serialize(self) {} + fn serialize(self: Self) -> [Field; Self::Size] { + let _ = self.0.serialize(); + [0; Self::Size] + } } -impl Serialize for (A,) +impl Serialize for [T; N] where - A: Serialize, + T: Serialize, { - let Size = ::Size; + let Size = ::Size * N; + + fn serialize(self: Self) -> [Field; Self::Size] { + [0; Self::Size] + } +} + +impl Serialize for Field { + let Size: u32 = 1; - fn serialize(self: Self) { - self.0.serialize(); + fn serialize(self) -> [Field; Self::Size] { + [self] } } fn main() { - let x = ((1,),); - x.serialize(); + let x = (((1, [2, 3, 4]), [5, 6, 7, 8]), 9); + assert_eq(x.serialize().len(), 9); } diff --git a/test_programs/compile_success_empty/serialize_4/src/main.nr b/test_programs/compile_success_empty/serialize_4/src/main.nr index b7b1df24f22..41879392c4f 100644 --- a/test_programs/compile_success_empty/serialize_4/src/main.nr +++ b/test_programs/compile_success_empty/serialize_4/src/main.nr @@ -1,6 +1,7 @@ trait Serialize { let Size: u32; + // Note that Rust disallows referencing constants here! fn serialize(self) -> [Field; Self::Size]; } @@ -12,8 +13,17 @@ where let Size = ::Size + ::Size; fn serialize(self: Self) -> [Field; Self::Size] { - let _ = self.0.serialize(); - [0; Self::Size] + let mut array: [Field; Self::Size] = std::mem::zeroed(); + let a = self.0.serialize(); + let b = self.1.serialize(); + + for i in 0..a.len() { + array[i] = a[i]; + } + for i in 0..b.len() { + array[i + a.len()] = b[i]; + } + array } } @@ -24,7 +34,19 @@ where let Size = ::Size * N; fn serialize(self: Self) -> [Field; Self::Size] { - [0; Self::Size] + let mut array: [Field; Self::Size] = std::mem::zeroed(); + let mut array_i = 0; + + for elem in self { + let elem_fields = elem.serialize(); + + for i in 0..elem_fields.len() { + array[array_i] = elem_fields[i]; + array_i += 1; + } + } + + array } } @@ -37,6 +59,6 @@ impl Serialize for Field { } fn main() { - let x = (((1, [2, 3, 4]), [5, 6, 7, 8]), 9); - assert_eq(x.serialize().len(), 9); + let x = (1, [2, 3, 4]); + assert_eq(x.serialize().len(), 4); } diff --git a/test_programs/compile_success_empty/serialize_5/Nargo.toml b/test_programs/compile_success_empty/serialize_5/Nargo.toml deleted file mode 100644 index e3fcae3f369..00000000000 --- a/test_programs/compile_success_empty/serialize_5/Nargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "serialize_5" -type = "bin" -authors = [""] -compiler_version = ">=0.32.0" - -[dependencies] diff --git a/test_programs/compile_success_empty/serialize_5/src/main.nr b/test_programs/compile_success_empty/serialize_5/src/main.nr deleted file mode 100644 index 41879392c4f..00000000000 --- a/test_programs/compile_success_empty/serialize_5/src/main.nr +++ /dev/null @@ -1,64 +0,0 @@ -trait Serialize { - let Size: u32; - - // Note that Rust disallows referencing constants here! - fn serialize(self) -> [Field; Self::Size]; -} - -impl Serialize for (A, B) -where - A: Serialize, - B: Serialize, -{ - let Size = ::Size + ::Size; - - fn serialize(self: Self) -> [Field; Self::Size] { - let mut array: [Field; Self::Size] = std::mem::zeroed(); - let a = self.0.serialize(); - let b = self.1.serialize(); - - for i in 0..a.len() { - array[i] = a[i]; - } - for i in 0..b.len() { - array[i + a.len()] = b[i]; - } - array - } -} - -impl Serialize for [T; N] -where - T: Serialize, -{ - let Size = ::Size * N; - - fn serialize(self: Self) -> [Field; Self::Size] { - let mut array: [Field; Self::Size] = std::mem::zeroed(); - let mut array_i = 0; - - for elem in self { - let elem_fields = elem.serialize(); - - for i in 0..elem_fields.len() { - array[array_i] = elem_fields[i]; - array_i += 1; - } - } - - array - } -} - -impl Serialize for Field { - let Size: u32 = 1; - - fn serialize(self) -> [Field; Self::Size] { - [self] - } -} - -fn main() { - let x = (1, [2, 3, 4]); - assert_eq(x.serialize().len(), 4); -} diff --git a/tooling/nargo_cli/build.rs b/tooling/nargo_cli/build.rs index a0ea47be704..8d0ecc2a815 100644 --- a/tooling/nargo_cli/build.rs +++ b/tooling/nargo_cli/build.rs @@ -145,7 +145,7 @@ const TESTS_WITHOUT_STDOUT_CHECK: [&str; 0] = []; /// These tests are ignored because of existing bugs in `nargo expand`. /// As the bugs are fixed these tests should be removed from this list. /// (some are ignored on purpose for the same reason as `IGNORED_NARGO_EXPAND_EXECUTION_TESTS`) -const IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_EMPTY_TESTS: [&str; 21] = [ +const IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_EMPTY_TESTS: [&str; 20] = [ // bug "associated_type_bounds", // bug @@ -171,8 +171,6 @@ const IGNORED_NARGO_EXPAND_COMPILE_SUCCESS_EMPTY_TESTS: [&str; 21] = [ // bug "serialize_4", // bug - "serialize_5", - // bug "trait_allowed_item_name_matches", // bug "trait_default_implementation", diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__expanded.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__expanded.snap deleted file mode 100644 index a39af5292e2..00000000000 --- a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__expanded.snap +++ /dev/null @@ -1,67 +0,0 @@ ---- -source: tooling/nargo_cli/tests/execute.rs -expression: expanded_code ---- -trait Serialize { - let Size: u32; - - fn serialize(self) -> [Field; Size]; -} - -impl Serialize for (A, B) where A: Serialize, B: Serialize { - type Size = ::Size + ::Size; - - fn serialize(self) -> [Field; ::Size + ::Size] where A: Serialize, B: Serialize { - let mut array: [Field; ::Size + ::Size] = std::mem::zeroed(); - let a: [Field; ::Size] = Serialize::serialize(self.0); - let b: [Field; ::Size] = Serialize::serialize(self.1); - for i in 0..a.len() { - array[i] = a[i]; - }; - for i in 0..b.len() { - { - let i_3793: u32 = i + a.len(); - array[i_3793] = b[i]; - } - }; - array - } -} - -impl Serialize for [T; N] where T: Serialize { - type Size = N * ::Size; - - fn serialize(self) -> [Field; N * ::Size] where T: Serialize { - let mut array: [Field; N * ::Size] = std::mem::zeroed(); - let mut array_i: Field = 0; - { - let ___i0: Self = self; - for ___i1 in 0..___i0.len() { - let elem: T = ___i0[___i1]; - { - let elem_fields: [Field; ::Size] = Serialize::serialize(elem); - for i in 0..elem_fields.len() { - array[array_i] = elem_fields[i]; - array_i = array_i + 1; - } - } - } - }; - array - } -} - -impl Serialize for Field { - let Size: u32 = 1; - - fn serialize(self) -> [Self; 1] { - [self] - } -} - -fn main() { - let x: (Field, [Field; 3]) = (1, [2, 3, 4]); - assert(x.serialize().len() == 4); -} - -// Warning: the generated code has syntax errors diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__force_brillig_false_inliner_0.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__force_brillig_false_inliner_0.snap deleted file mode 100644 index 7de940827c8..00000000000 --- a/tooling/nargo_cli/tests/snapshots/compile_success_empty/serialize_5/execute__tests__force_brillig_false_inliner_0.snap +++ /dev/null @@ -1,26 +0,0 @@ ---- -source: tooling/nargo_cli/tests/execute.rs -expression: artifact ---- -{ - "noir_version": "[noir_version]", - "hash": "[hash]", - "abi": { - "parameters": [], - "return_type": null, - "error_types": {} - }, - "bytecode": [ - "func 0", - "current witness index : _0", - "private parameters indices : []", - "public parameters indices : []", - "return value indices : []" - ], - "debug_symbols": "XY5BCsQwCEXv4rqLWfcqw1BsaosgJtikMITefWyYQOlK/3/6tcJCc9km1jXuML4rzMYivE0SA2aO6m49B+hyykbkFty4byU00gyjFpEBDpTShvaE2mpGc/oagHTx6oErC13d+XGBge158UBjnIX+ci0abjR/Uyf942Qx0FKMrqTGPPsH", - "file_map": {}, - "names": [ - "main" - ], - "brillig_names": [] -}