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:
parent
15cb343065
commit
0232f074a8
@ -230,14 +230,16 @@ fn (mut g Gen) gen_alias_equality_fn(left_type ast.Type) string {
|
|||||||
mut fn_builder := strings.new_builder(512)
|
mut fn_builder := strings.new_builder(512)
|
||||||
fn_builder.writeln('static bool ${ptr_styp}_alias_eq(${ptr_styp} a, ${ptr_styp} b) {')
|
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' }
|
mut 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 right_var := if is_option { '*' + g.read_opt(info.parent_type, 'b') } else { 'b' }
|
||||||
|
|
||||||
sym := g.table.sym(info.parent_type)
|
sym := g.table.sym(info.parent_type)
|
||||||
if sym.kind == .string {
|
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});')
|
fn_builder.writeln('\treturn ((${left_var}).len == (${right_var}).len && (${left_var}).len == 0) || string__eq(${left_var}, ${right_var});')
|
||||||
} else {
|
} else {
|
||||||
fn_builder.writeln('\treturn string__eq(a, b);')
|
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) {')
|
fn_builder.writeln('\t\tif (*(voidptr*)map_get(&b, k, &(voidptr[]){ 0 }) != v) {')
|
||||||
}
|
}
|
||||||
else {
|
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;')
|
fn_builder.writeln('\t\t\treturn false;')
|
||||||
|
@ -3924,7 +3924,7 @@ fn (mut g Gen) unlock_locks() {
|
|||||||
|
|
||||||
fn (mut g Gen) map_init(node ast.MapInit) {
|
fn (mut g Gen) map_init(node ast.MapInit) {
|
||||||
unwrap_key_typ := g.unwrap_generic(node.key_type)
|
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)
|
key_typ_str := g.typ(unwrap_key_typ)
|
||||||
value_typ_str := g.typ(unwrap_val_typ)
|
value_typ_str := g.typ(unwrap_val_typ)
|
||||||
value_sym := g.table.sym(unwrap_val_typ)
|
value_sym := g.table.sym(unwrap_val_typ)
|
||||||
|
18
vlib/v/tests/option_alias_eq_test.v
Normal file
18
vlib/v/tests/option_alias_eq_test.v
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user