mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix struct optional field zero init (#16814)
This commit is contained in:
parent
04cb6ae65f
commit
1c151fbec9
@ -314,6 +314,10 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
|
||||
}
|
||||
|
||||
g.expr(field.default_expr)
|
||||
} else if field.typ.has_flag(.optional) {
|
||||
tmp_var := g.new_tmp_var()
|
||||
g.expr_with_tmp_var(ast.None{}, ast.none_type, field.typ, tmp_var)
|
||||
return true
|
||||
} else {
|
||||
g.write(g.type_default(field.typ))
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
0
|
||||
0
|
||||
Option(0)
|
||||
Option(0)
|
||||
Option(error: none)
|
||||
Option(error: none)
|
||||
Option(error: none)
|
||||
Option(error: none)
|
||||
1
|
||||
|
21
vlib/v/tests/struct_optional_field_zero_test.v
Normal file
21
vlib/v/tests/struct_optional_field_zero_test.v
Normal file
@ -0,0 +1,21 @@
|
||||
struct Foo {
|
||||
foo ?string
|
||||
}
|
||||
|
||||
fn test_struct_optional_field_zero() {
|
||||
a := Foo{}
|
||||
if foo := a.foo {
|
||||
println(foo)
|
||||
assert false
|
||||
} else {
|
||||
assert true
|
||||
}
|
||||
|
||||
b := Foo{'hello'}
|
||||
if foo := b.foo {
|
||||
println(foo)
|
||||
assert true
|
||||
} else {
|
||||
assert false
|
||||
}
|
||||
}
|
@ -60,21 +60,21 @@ fn test_types() {
|
||||
|
||||
fn test_optional_types() {
|
||||
assert json.encode(StructTypeOptional[string]{ val: none }) == '{}'
|
||||
assert json.encode(StructTypeOptional[string]{}) == '{"val":""}'
|
||||
assert json.encode(StructTypeOptional[string]{}) == '{}'
|
||||
assert json.encode(StructTypeOptional[string]{ val: '' }) == '{"val":""}'
|
||||
assert json.encode(StructTypeOptional[string]{ val: 'a' }) == '{"val":"a"}'
|
||||
|
||||
assert json.encode(StructTypeOptional[bool]{ val: none }) == '{}'
|
||||
assert json.encode(StructTypeOptional[bool]{}) == '{"val":false}'
|
||||
assert json.encode(StructTypeOptional[bool]{}) == '{}'
|
||||
assert json.encode(StructTypeOptional[bool]{ val: false }) == '{"val":false}'
|
||||
assert json.encode(StructTypeOptional[bool]{ val: true }) == '{"val":true}'
|
||||
|
||||
assert json.encode(StructTypeOptional[int]{ val: none }) == '{}'
|
||||
assert json.encode(StructTypeOptional[int]{}) == '{"val":0}'
|
||||
assert json.encode(StructTypeOptional[int]{}) == '{}'
|
||||
assert json.encode(StructTypeOptional[int]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(StructTypeOptional[int]{ val: 1 }) == '{"val":1}'
|
||||
|
||||
assert json.encode(StructTypeOptional[time.Time]{}) == '{"val":"0000-00-00T00:00:00.000Z"}'
|
||||
assert json.encode(StructTypeOptional[time.Time]{}) == '{}'
|
||||
assert json.encode(StructTypeOptional[time.Time]{ val: fixed_time }) == '{"val":"2022-03-11T13:54:25.000Z"}'
|
||||
}
|
||||
|
||||
@ -123,7 +123,7 @@ fn test_array() {
|
||||
|
||||
fn test_optional_array() {
|
||||
assert json.encode(StructTypeOptional[[]int]{ val: none }) == '{}'
|
||||
assert json.encode(StructTypeOptional[[]int]{}) == '{"val":[]}'
|
||||
assert json.encode(StructTypeOptional[[]int]{}) == '{}'
|
||||
assert json.encode(StructTypeOptional[[]int]{ val: [] }) == '{"val":[]}'
|
||||
assert json.encode(StructTypeOptional[[]int]{ val: [0] }) == '{"val":[0]}'
|
||||
assert json.encode(StructTypeOptional[[]int]{ val: [1] }) == '{"val":[1]}'
|
||||
|
Loading…
Reference in New Issue
Block a user