Skip to content

Commit

Permalink
cgen: improve uninitialized option struct field declaration (#20129)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Dec 10, 2023
1 parent e6bb6df commit 4938b35
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
6 changes: 2 additions & 4 deletions vlib/v/gen/c/struct.v
Expand Up @@ -378,8 +378,7 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
}

if field.default_expr is ast.None {
tmp_var := g.new_tmp_var()
g.expr_with_tmp_var(ast.None{}, ast.none_type, field.typ, tmp_var)
g.gen_option_error(field.typ, ast.None{})
return true
} else if field.typ.has_flag(.option) {
tmp_var := g.new_tmp_var()
Expand All @@ -394,8 +393,7 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
}
g.expr(field.default_expr)
} else if field.typ.has_flag(.option) {
tmp_var := g.new_tmp_var()
g.expr_with_tmp_var(ast.None{}, ast.none_type, field.typ, tmp_var)
g.gen_option_error(field.typ, ast.None{})
return true
} else if sym.info is ast.ArrayFixed {
g.write('{')
Expand Down
34 changes: 34 additions & 0 deletions vlib/v/tests/option_struct_field_init_test.v
@@ -0,0 +1,34 @@
pub struct Client {
pub:
token string
intents int
mut:
ready bool
sequence ?int
}

enum Intents {
foo
}

pub type IntentsOrInt = Intents | int

@[params]
pub struct BotConfig {
pub:
intents IntentsOrInt
}

pub fn bot(token string, config BotConfig) Client {
return Client{
token: 'Bot ${token}'
intents: match config.intents {
Intents { int(config.intents) }
int { config.intents }
}
}
}

fn test_main() {
assert true
}

0 comments on commit 4938b35

Please sign in to comment.