1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: make the default values for all enums, be their first listed entry, even if it was enum MyEnum { first = 999 second}, not just the defaul 0 (#18388)

This commit is contained in:
Felipe Pena 2023-06-11 15:57:36 -03:00 committed by GitHub
parent fc6726b2b1
commit 54eabcb242
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 0 deletions

View File

@ -32,6 +32,7 @@ pub mut:
//
// See https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00 for details.
pub enum SameSite {
same_site_not_set
same_site_default_mode = 1
same_site_lax_mode
same_site_strict_mode
@ -162,6 +163,7 @@ pub fn (c &Cookie) str() string {
b.write_string('; Secure')
}
match c.same_site {
.same_site_not_set {}
.same_site_default_mode {
b.write_string('; SameSite')
}

View File

@ -6254,6 +6254,18 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
return init_str
}
}
.enum_ {
// returns the enum's first value
if enum_decl := g.table.enum_decls[sym.name] {
return if enum_decl.fields[0].expr is ast.EmptyExpr {
'0'
} else {
g.expr_string(enum_decl.fields[0].expr)
}
} else {
return '0'
}
}
else {
return '0'
}

View File

@ -0,0 +1,28 @@
enum TestEnum {
two = 1
one
}
struct Test {
a TestEnum
b TestEnum = .one
}
fn test_main() {
assert dump(Test{}) == Test{
a: .two
b: .one
}
assert dump(Test{}) == Test{}
assert dump(Test{
a: .one
}) == Test{
a: .one
}
assert dump(Test{
b: .two
}) == Test{
b: .two
}
}