mirror of
https://github.com/vlang/v.git
synced 2025-09-11 16:36:20 -04:00
parser, checker, cgen: fix anon struct with default expr (#19257)
This commit is contained in:
parent
13fd5493d3
commit
5848610453
@ -93,6 +93,10 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// check anon struct declaration
|
||||||
|
if field.anon_struct_decl.fields.len > 0 {
|
||||||
|
c.struct_decl(mut field.anon_struct_decl)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.expected_type = old_expected_type
|
c.expected_type = old_expected_type
|
||||||
util.timing_measure_cumulative('Checker.struct setting default_expr_typ')
|
util.timing_measure_cumulative('Checker.struct setting default_expr_typ')
|
||||||
|
@ -333,7 +333,7 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if has_option_field {
|
if has_option_field || field.anon_struct_decl.fields.len > 0 {
|
||||||
default_init := ast.StructInit{
|
default_init := ast.StructInit{
|
||||||
typ: field.typ
|
typ: field.typ
|
||||||
}
|
}
|
||||||
@ -379,7 +379,6 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
|
|||||||
tmp_var)
|
tmp_var)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
g.expr(field.default_expr)
|
g.expr(field.default_expr)
|
||||||
} else if field.typ.has_flag(.option) {
|
} else if field.typ.has_flag(.option) {
|
||||||
tmp_var := g.new_tmp_var()
|
tmp_var := g.new_tmp_var()
|
||||||
|
@ -314,7 +314,6 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
|
|||||||
is_deprecated: is_field_deprecated
|
is_deprecated: is_field_deprecated
|
||||||
anon_struct_decl: p.anon_struct_decl
|
anon_struct_decl: p.anon_struct_decl
|
||||||
}
|
}
|
||||||
p.anon_struct_decl = ast.StructDecl{}
|
|
||||||
}
|
}
|
||||||
// save embeds as table fields too, it will be used in generation phase
|
// save embeds as table fields too, it will be used in generation phase
|
||||||
fields << ast.StructField{
|
fields << ast.StructField{
|
||||||
@ -333,7 +332,9 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
|
|||||||
is_global: is_field_global
|
is_global: is_field_global
|
||||||
is_volatile: is_field_volatile
|
is_volatile: is_field_volatile
|
||||||
is_deprecated: is_field_deprecated
|
is_deprecated: is_field_deprecated
|
||||||
|
anon_struct_decl: p.anon_struct_decl
|
||||||
}
|
}
|
||||||
|
p.anon_struct_decl = ast.StructDecl{}
|
||||||
p.attrs = prev_attrs
|
p.attrs = prev_attrs
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
15
vlib/v/tests/anon_struct_with_default_expr_test.v
Normal file
15
vlib/v/tests/anon_struct_with_default_expr_test.v
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
struct Bar {
|
||||||
|
anon struct {
|
||||||
|
foofoo Foo = Foo{'foofoo'}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Foo {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_anon_struct_with_default_expr() {
|
||||||
|
bar := Bar{}
|
||||||
|
println(bar.anon.foofoo.name)
|
||||||
|
assert bar.anon.foofoo.name == 'foofoo'
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user