cgen: fix struct field with default optional value (fix #11119) (#22167)

This commit is contained in:
yuyi 2024-09-07 00:54:14 +08:00 committed by GitHub
parent e1847cac9c
commit 023fd9234d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 0 deletions

View File

@ -415,6 +415,14 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
g.struct_init(default_init) g.struct_init(default_init)
} }
return true return true
} else if sym.language == .v && !field.typ.is_ptr() && sym.mod != 'builtin'
&& !sym.info.is_empty_struct() {
default_init := ast.StructInit{
typ: field.typ
}
g.write('.${field_name} = ')
g.struct_init(default_init)
return true
} }
} }
} }

View File

@ -0,0 +1,27 @@
import regex
struct RegexCache {
mut:
tag_script_start regex.RE = regex.regex_opt(r'^script.*>') or { panic(err) }
}
pub struct Tokenizer {
mut:
regex_cache RegexCache = RegexCache{}
}
fn new_parser() &Parser {
mut parser := &Parser{}
return parser
}
pub struct Parser {
mut:
tnzr Tokenizer
}
fn test_struct_field_default_value_optional() {
p := new_parser()
println(p)
assert true
}