Skip to content

Commit 023fd92

Browse files
authored
cgen: fix struct field with default optional value (fix #11119) (#22167)
1 parent e1847ca commit 023fd92

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

vlib/v/gen/c/struct.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
415415
g.struct_init(default_init)
416416
}
417417
return true
418+
} else if sym.language == .v && !field.typ.is_ptr() && sym.mod != 'builtin'
419+
&& !sym.info.is_empty_struct() {
420+
default_init := ast.StructInit{
421+
typ: field.typ
422+
}
423+
g.write('.${field_name} = ')
424+
g.struct_init(default_init)
425+
return true
418426
}
419427
}
420428
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import regex
2+
3+
struct RegexCache {
4+
mut:
5+
tag_script_start regex.RE = regex.regex_opt(r'^script.*>') or { panic(err) }
6+
}
7+
8+
pub struct Tokenizer {
9+
mut:
10+
regex_cache RegexCache = RegexCache{}
11+
}
12+
13+
fn new_parser() &Parser {
14+
mut parser := &Parser{}
15+
return parser
16+
}
17+
18+
pub struct Parser {
19+
mut:
20+
tnzr Tokenizer
21+
}
22+
23+
fn test_struct_field_default_value_optional() {
24+
p := new_parser()
25+
println(p)
26+
assert true
27+
}

0 commit comments

Comments
 (0)