mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix alloc empty struct array error (#14007)
This commit is contained in:
parent
843ce43077
commit
25d8faabf6
@ -300,3 +300,15 @@ fn test_alias_string_contains() {
|
||||
names := [Str('')]
|
||||
assert (Str('') in names) == true
|
||||
}
|
||||
|
||||
struct XYZ {}
|
||||
|
||||
fn test_array_append_empty_struct() {
|
||||
mut names := []XYZ{cap: 2}
|
||||
names << XYZ{}
|
||||
assert (XYZ{} in names) == true
|
||||
|
||||
// test fixed array
|
||||
array := [XYZ{}]
|
||||
assert (XYZ{} in names) == true
|
||||
}
|
||||
|
@ -151,7 +151,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
|
||||
} else {
|
||||
g.write('0, ')
|
||||
}
|
||||
if elem_type.unaliased_sym.kind == .function {
|
||||
if elem_type.unaliased_sym.kind == .function || g.is_empty_struct(elem_type) {
|
||||
g.write('sizeof(voidptr), ')
|
||||
} else {
|
||||
g.write('sizeof($elem_styp), ')
|
||||
@ -217,7 +217,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
|
||||
} else {
|
||||
g.write('0, ')
|
||||
}
|
||||
if elem_type.unaliased_sym.kind == .function {
|
||||
if elem_type.unaliased_sym.kind == .function || g.is_empty_struct(elem_type) {
|
||||
g.write('sizeof(voidptr), ')
|
||||
} else {
|
||||
g.write('sizeof($elem_styp), ')
|
||||
@ -251,6 +251,8 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
|
||||
len := node.exprs.len
|
||||
if elem_type.unaliased_sym.kind == .function {
|
||||
g.write('new_array_from_c_array($len, $len, sizeof(voidptr), _MOV((voidptr[$len]){')
|
||||
} else if g.is_empty_struct(elem_type) {
|
||||
g.write('new_array_from_c_array${noscan}($len, $len, sizeof(voidptr), _MOV(($elem_styp[$len]){')
|
||||
} else {
|
||||
g.write('new_array_from_c_array${noscan}($len, $len, sizeof($elem_styp), _MOV(($elem_styp[$len]){')
|
||||
}
|
||||
|
@ -305,3 +305,18 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fn (mut g Gen) is_empty_struct(t Type) bool {
|
||||
sym := t.unaliased_sym
|
||||
match sym.info {
|
||||
ast.Struct {
|
||||
if sym.info.fields.len > 0 || sym.info.embeds.len > 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user