cgen: fix auto_eq for option eq operator overload (#20795)

This commit is contained in:
Felipe Pena 2024-02-13 07:11:07 -03:00 committed by GitHub
parent ce99c31058
commit 694c781f57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View File

@ -188,7 +188,13 @@ fn (mut g Gen) gen_struct_equality_fn(left_type ast.Type) string {
// overloaded
if left.sym.has_method('==') {
fn_builder.writeln('\treturn ${fn_name}__eq(a, b);')
if left.typ.has_flag(.option) {
opt_ptr_styp := g.typ(left.typ.set_nr_muls(0).clear_flag(.option))
opt_fn_name := opt_ptr_styp.replace('struct ', '')
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);')
} else {
fn_builder.writeln('\treturn ${fn_name}__eq(a, b);')
}
fn_builder.writeln('}')
return fn_name
}

View File

@ -0,0 +1,38 @@
import time
struct SubTestTimeOptional[T] {
iis string
ext T
}
pub fn (s1 SubTestTimeOptional[T]) == (s2 SubTestTimeOptional[T]) bool {
return s1.iis == s2.iis
}
struct TestTimeOptional {
exp ?time.Time
}
fn now_optional[T]() SubTestTimeOptional[T] {
return SubTestTimeOptional[TestTimeOptional]{
iis: 'Vtest'
ext: TestTimeOptional{
exp: time.now()
}
}
}
fn now_delay_optional[T]() SubTestTimeOptional[T] {
return SubTestTimeOptional[T]{
iis: 'Vtest'
ext: TestTimeOptional{
exp: time.now().add_seconds(5)
}
}
}
fn test_main() {
mut t1 := now_optional[TestTimeOptional]()
mut t2 := now_delay_optional[TestTimeOptional]()
assert t1 != t2
}