Skip to content

Commit 17812a7

Browse files
authored
cgen: fix type_default for option type, when the default expr is none (fix #23318) (#23320)
1 parent 8cdb507 commit 17812a7

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module main
2+
3+
import json
4+
5+
struct Test {
6+
id ?string = none
7+
}
8+
9+
fn test_main() {
10+
assert json.encode(Test{}) == '{}'
11+
}

vlib/v/gen/c/cgen.v

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,14 @@ fn (mut g Gen) expr_string(expr ast.Expr) string {
12361236
return g.out.cut_to(pos).trim_space()
12371237
}
12381238

1239+
fn (mut g Gen) expr_string_opt(typ ast.Type, expr ast.Expr) string {
1240+
expr_str := g.expr_string(expr)
1241+
if expr is ast.None {
1242+
return '(${g.styp(typ)}){.state=2, .err=${expr_str}, .data={EMPTY_STRUCT_INITIALIZATION}}'
1243+
}
1244+
return expr_str
1245+
}
1246+
12391247
fn (mut g Gen) expr_string_with_cast(expr ast.Expr, typ ast.Type, exp ast.Type) string {
12401248
pos := g.out.len
12411249
// pos2 := g.out_parallel[g.out_idx].len
@@ -7034,7 +7042,7 @@ fn (mut g Gen) type_default_impl(typ_ ast.Type, decode_sumtype bool) string {
70347042
}
70357043
}
70367044
} else {
7037-
default_str := g.expr_string(field.default_expr)
7045+
default_str := g.expr_string_opt(field.typ, field.default_expr)
70387046
if default_str.count('\n') > 1 {
70397047
g.type_default_vars.writeln(default_str.all_before_last('\n'))
70407048
expr_str = default_str.all_after_last('\n')

vlib/v/gen/c/json.v

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
725725
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${dec_name}(jsonroot_${tmp});')
726726
if field.has_default_expr {
727727
dec.writeln('\t} else {')
728-
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string(field.default_expr)};')
728+
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
729+
field.default_expr)};')
729730
}
730731
dec.writeln('\t}')
731732
} else if field_sym.kind == .enum {
@@ -759,7 +760,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
759760
}
760761
if field.has_default_expr {
761762
dec.writeln('\t} else {')
762-
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string(field.default_expr)};')
763+
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
764+
field.default_expr)};')
763765
}
764766
dec.writeln('\t}')
765767
} else if field_sym.name == 'time.Time' {
@@ -771,7 +773,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
771773
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = time__unix(json__decode_u64(jsonroot_${tmp}));')
772774
if field.has_default_expr {
773775
dec.writeln('\t} else {')
774-
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string(field.default_expr)};')
776+
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
777+
field.default_expr)};')
775778
}
776779
dec.writeln('\t}')
777780
} else if field_sym.kind == .alias {
@@ -792,7 +795,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
792795
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${parent_dec_name} (jsonroot_${tmp});')
793796
if field.has_default_expr {
794797
dec.writeln('\t} else {')
795-
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string(field.default_expr)};')
798+
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
799+
field.default_expr)};')
796800
}
797801
dec.writeln('\t}')
798802
} else {
@@ -803,7 +807,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
803807
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = *(${field_type}*) ${tmp}.data;')
804808
if field.has_default_expr {
805809
dec.writeln('\t} else {')
806-
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string(field.default_expr)};')
810+
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
811+
field.default_expr)};')
807812
}
808813
dec.writeln('\t}')
809814
}
@@ -842,7 +847,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
842847
}
843848
if field.has_default_expr {
844849
dec.writeln('\t} else {')
845-
default_str := g.expr_string(field.default_expr)
850+
default_str := g.expr_string_opt(field.typ, field.default_expr)
846851
lines := default_str.count('\n')
847852
if lines > 1 {
848853
dec.writeln(default_str.all_before_last('\n'))

0 commit comments

Comments
 (0)