From bfab24c8f6d36efb9e948d49a5be175ecb7ee352 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Fri, 27 Oct 2023 14:30:58 -0500 Subject: [PATCH 1/3] Add test --- .../impl_with_where_clause/Nargo.toml | 7 +++++ .../impl_with_where_clause/src/main.nr | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/Nargo.toml create mode 100644 tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/src/main.nr diff --git a/tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/Nargo.toml b/tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/Nargo.toml new file mode 100644 index 00000000000..9da56eebf35 --- /dev/null +++ b/tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "trait_generics" +type = "bin" +authors = [""] +compiler_version = "0.10.5" + +[dependencies] diff --git a/tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/src/main.nr b/tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/src/main.nr new file mode 100644 index 00000000000..be9fe0b110d --- /dev/null +++ b/tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/src/main.nr @@ -0,0 +1,29 @@ + +fn main() { + let array: [Field; 3] = [1, 2, 3]; + assert(array.eq(array)); + + // Ensure this still works if we have to infer the type of the integer literals + let array = [1, 2, 3]; + assert(array.eq(array)); +} + +trait Eq { + fn eq(self, other: Self) -> bool; +} + +impl Eq for [T; N] where T: Eq { + fn eq(self, other: Self) -> bool { + let mut ret = true; + for i in 0 .. self.len() { + ret &= self[i].eq(other[i]); + } + ret + } +} + +impl Eq for Field { + fn eq(self, other: Field) -> bool { + self == other + } +} From b60b9d32af118cb52b3f09d530651b90e016b275 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Fri, 27 Oct 2023 14:37:34 -0500 Subject: [PATCH 2/3] Allow where clauses on trait impls --- compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs index 17c85abbd8b..32f39ce4952 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -145,12 +145,13 @@ impl<'a> ModCollector<'a> { for trait_impl in impls { let trait_name = trait_impl.trait_name.clone(); - let unresolved_functions = + let mut unresolved_functions = self.collect_trait_impl_function_overrides(context, &trait_impl, krate); let module = ModuleId { krate, local_id: self.module_id }; - for (_, func_id, noir_function) in &unresolved_functions.functions { + for (_, func_id, noir_function) in &mut unresolved_functions.functions { + noir_function.def.where_clause.append(&mut trait_impl.where_clause.clone()); context.def_interner.push_function(*func_id, &noir_function.def, module); } From f6072fe6a7b980db364486edf4077095920201b6 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Fri, 27 Oct 2023 14:41:09 -0500 Subject: [PATCH 3/3] Fix test name --- .../compile_success_empty/impl_with_where_clause/Nargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/Nargo.toml b/tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/Nargo.toml index 9da56eebf35..070af7d82c2 100644 --- a/tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/Nargo.toml +++ b/tooling/nargo_cli/tests/compile_success_empty/impl_with_where_clause/Nargo.toml @@ -1,5 +1,5 @@ [package] -name = "trait_generics" +name = "impl_with_where_clause" type = "bin" authors = [""] compiler_version = "0.10.5"