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

cgen: fix struct shared field with default init (#15040)

This commit is contained in:
yuyi 2022-07-13 01:04:06 +08:00 committed by GitHub
parent 7a17a29952
commit 19d0d758c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 10 deletions

View File

@ -5360,8 +5360,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
mut has_none_zero := false mut has_none_zero := false
mut init_str := '{' mut init_str := '{'
info := sym.info as ast.Struct info := sym.info as ast.Struct
typ_is_shared_f := typ.has_flag(.shared_f) if sym.language == .v {
if sym.language == .v && !typ_is_shared_f {
for field in info.fields { for field in info.fields {
field_sym := g.table.sym(field.typ) field_sym := g.table.sym(field.typ)
if field.has_default_expr if field.has_default_expr
@ -5392,8 +5391,10 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
} }
} }
} }
typ_is_shared_f := typ.has_flag(.shared_f)
if has_none_zero { if has_none_zero {
init_str += '}' init_str += '}'
if !typ_is_shared_f {
type_name := if info.is_anon { type_name := if info.is_anon {
// No name needed for anon structs, C figures it out on its own. // No name needed for anon structs, C figures it out on its own.
'' ''
@ -5401,10 +5402,11 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
'(${g.typ(typ)})' '(${g.typ(typ)})'
} }
init_str = type_name + init_str init_str = type_name + init_str
}
} else { } else {
init_str += '0}' init_str += '0}'
} }
if typ.has_flag(.shared_f) { if typ_is_shared_f {
styp := '__shared__${g.table.sym(typ).cname}' styp := '__shared__${g.table.sym(typ).cname}'
return '($styp*)__dup${styp}(&($styp){.mtx = {0}, .val = $init_str}, sizeof($styp))' return '($styp*)__dup${styp}(&($styp){.mtx = {0}, .val = $init_str}, sizeof($styp))'
} else { } else {

View File

@ -0,0 +1,23 @@
struct App {
id string
mut:
app_data shared AppData
}
struct AppData {
id string
sub_app_data SubAppData
}
struct SubAppData {
id string
message string = 'Default message'
}
fn test_struct_field_with_default_init() {
mut app := App{}
println(app)
rlock app.app_data {
assert app.app_data.sub_app_data.message == 'Default message'
}
}