Skip to content

Commit 694c781

Browse files
authored
cgen: fix auto_eq for option eq operator overload (#20795)
1 parent ce99c31 commit 694c781

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

vlib/v/gen/c/auto_eq_methods.v

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,13 @@ fn (mut g Gen) gen_struct_equality_fn(left_type ast.Type) string {
188188

189189
// overloaded
190190
if left.sym.has_method('==') {
191-
fn_builder.writeln('\treturn ${fn_name}__eq(a, b);')
191+
if left.typ.has_flag(.option) {
192+
opt_ptr_styp := g.typ(left.typ.set_nr_muls(0).clear_flag(.option))
193+
opt_fn_name := opt_ptr_styp.replace('struct ', '')
194+
fn_builder.writeln('\treturn (a.state == b.state && b.state == 2) || ${opt_fn_name}__eq(*(${opt_ptr_styp}*)a.data, *(${opt_ptr_styp}*)b.data);')
195+
} else {
196+
fn_builder.writeln('\treturn ${fn_name}__eq(a, b);')
197+
}
192198
fn_builder.writeln('}')
193199
return fn_name
194200
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import time
2+
3+
struct SubTestTimeOptional[T] {
4+
iis string
5+
ext T
6+
}
7+
8+
pub fn (s1 SubTestTimeOptional[T]) == (s2 SubTestTimeOptional[T]) bool {
9+
return s1.iis == s2.iis
10+
}
11+
12+
struct TestTimeOptional {
13+
exp ?time.Time
14+
}
15+
16+
fn now_optional[T]() SubTestTimeOptional[T] {
17+
return SubTestTimeOptional[TestTimeOptional]{
18+
iis: 'Vtest'
19+
ext: TestTimeOptional{
20+
exp: time.now()
21+
}
22+
}
23+
}
24+
25+
fn now_delay_optional[T]() SubTestTimeOptional[T] {
26+
return SubTestTimeOptional[T]{
27+
iis: 'Vtest'
28+
ext: TestTimeOptional{
29+
exp: time.now().add_seconds(5)
30+
}
31+
}
32+
}
33+
34+
fn test_main() {
35+
mut t1 := now_optional[TestTimeOptional]()
36+
mut t2 := now_delay_optional[TestTimeOptional]()
37+
assert t1 != t2
38+
}

0 commit comments

Comments
 (0)