1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix alias eq method + map init with option (#18483)

This commit is contained in:
Felipe Pena 2023-06-20 05:52:59 -03:00 committed by GitHub
parent 15cb343065
commit 0232f074a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View File

@ -230,14 +230,16 @@ fn (mut g Gen) gen_alias_equality_fn(left_type ast.Type) string {
mut fn_builder := strings.new_builder(512)
fn_builder.writeln('static bool ${ptr_styp}_alias_eq(${ptr_styp} a, ${ptr_styp} b) {')
is_option := info.parent_type.has_flag(.option) || left.typ.has_flag(.option)
is_option := left.typ.has_flag(.option)
left_var := if is_option { '*' + g.read_opt(info.parent_type, 'a') } else { 'a' }
right_var := if is_option { '*' + g.read_opt(info.parent_type, 'b') } else { 'b' }
mut left_var := if is_option { '*' + g.read_opt(info.parent_type, 'a') } else { 'a' }
mut right_var := if is_option { '*' + g.read_opt(info.parent_type, 'b') } else { 'b' }
sym := g.table.sym(info.parent_type)
if sym.kind == .string {
if is_option {
if info.parent_type.has_flag(.option) {
left_var = '*' + g.read_opt(info.parent_type, 'a')
right_var = '*' + g.read_opt(info.parent_type, 'b')
fn_builder.writeln('\treturn ((${left_var}).len == (${right_var}).len && (${left_var}).len == 0) || string__eq(${left_var}, ${right_var});')
} else {
fn_builder.writeln('\treturn string__eq(a, b);')
@ -462,7 +464,11 @@ fn (mut g Gen) gen_map_equality_fn(left_type ast.Type) string {
fn_builder.writeln('\t\tif (*(voidptr*)map_get(&b, k, &(voidptr[]){ 0 }) != v) {')
}
else {
fn_builder.writeln('\t\tif (*(${ptr_value_styp}*)map_get(&b, k, &(${ptr_value_styp}[]){ 0 }) != v) {')
if value.typ.has_flag(.option) {
fn_builder.writeln('\t\tif (memcmp(v.data, ((${ptr_value_styp}*)map_get(&b, k, &(${ptr_value_styp}[]){ 0 }))->data, sizeof(${g.base_type(value.typ)})) != 0) {')
} else {
fn_builder.writeln('\t\tif (*(${ptr_value_styp}*)map_get(&b, k, &(${ptr_value_styp}[]){ 0 }) != v) {')
}
}
}
fn_builder.writeln('\t\t\treturn false;')

View File

@ -3924,7 +3924,7 @@ fn (mut g Gen) unlock_locks() {
fn (mut g Gen) map_init(node ast.MapInit) {
unwrap_key_typ := g.unwrap_generic(node.key_type)
unwrap_val_typ := g.unwrap_generic(node.value_type).clear_flags(.option, .result)
unwrap_val_typ := g.unwrap_generic(node.value_type).clear_flags(.result)
key_typ_str := g.typ(unwrap_key_typ)
value_typ_str := g.typ(unwrap_val_typ)
value_sym := g.table.sym(unwrap_val_typ)

View File

@ -0,0 +1,18 @@
struct MyStruct {
field int
}
pub type MyOpt = ?MyStruct
fn empty() map[string]MyOpt {
return {}
}
fn test_empty() {
expected := {
'key': ?MyOpt(none)
}
assert dump(expected) == {
'key': ?MyOpt(none)
}
}