From e500e1777ee2de537f5ca1e9dfc4bff9ddae14ed Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 6 Sep 2024 16:12:03 -0300 Subject: [PATCH] fix: let `derive(Eq)` work for empty structs --- noir_stdlib/src/cmp.nr | 8 +++++++- test_programs/execution_success/derive/src/main.nr | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/noir_stdlib/src/cmp.nr b/noir_stdlib/src/cmp.nr index b7f473429a7..521604a4e20 100644 --- a/noir_stdlib/src/cmp.nr +++ b/noir_stdlib/src/cmp.nr @@ -11,7 +11,13 @@ trait Eq { comptime fn derive_eq(s: StructDefinition) -> Quoted { let signature = quote { fn eq(_self: Self, _other: Self) -> bool }; let for_each_field = |name| quote { (_self.$name == _other.$name) }; - let body = |fields| fields; + let body = |fields| { + if s.fields().len() == 0 { + quote { true } + } else { + fields + } + }; crate::meta::make_trait_impl(s, quote { Eq }, signature, for_each_field, quote { & }, body) } // docs:end:derive_eq diff --git a/test_programs/execution_success/derive/src/main.nr b/test_programs/execution_success/derive/src/main.nr index 5ec2fb32a79..b32612831d7 100644 --- a/test_programs/execution_success/derive/src/main.nr +++ b/test_programs/execution_success/derive/src/main.nr @@ -35,6 +35,9 @@ struct MyOtherOtherStruct { x: T, } +#[derive(Eq, Default, Hash, Ord)] +struct EmptyStruct { } + fn main() { let s = MyStruct { my_field: 1 }; s.do_nothing(); @@ -53,6 +56,9 @@ fn main() { let mut hasher = TestHasher { result: 0 }; o1.hash(&mut hasher); assert_eq(hasher.finish(), 12 + 24 + 54); + + let empty = EmptyStruct {}; + assert_eq(empty, empty); } struct TestHasher {