Skip to content

Commit

Permalink
fix: ensure all fixed array elements initialized (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
JalonSolov authored Jul 29, 2024
1 parent 9d4365e commit b0c8374
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
8 changes: 3 additions & 5 deletions ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ struct Ident {
struct TypeOrIdent {
node_type_str string @[json: '_type']
name string @[json: 'Name']
len Expr @[json: 'Len']
elt Ident @[json: 'Elt']
// len BasicLit @[json: 'Len']
len Expr @[json: 'Len']
}

struct BasicLit {
Expand All @@ -179,9 +178,8 @@ struct SelectorExpr {
// Foo{bar:baz}
// []bool{}
struct CompositeLit {
typ TypeOrIdent @[json: 'Type']

elts []Expr @[json: 'Elts']
typ TypeOrIdent @[json: 'Type']
elts []Expr @[json: 'Elts']
}

/*
Expand Down
14 changes: 14 additions & 0 deletions struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ fn (mut app App) composite_lit(c CompositeLit) {
app.gen(',')
}
}
if have_len {
elt_name := go2v_type(c.typ.elt.name)
diff := len_val.int() - c.elts.len
if diff > 0 {
for _ in 0 .. diff {
app.gen(',')
match elt_name {
'int' { app.gen('0') }
'string' { app.gen("''") }
else { app.gen('unknown element type??') }
}
}
}
}
app.gen(']')
if is_fixed {
app.gen('!')
Expand Down
4 changes: 2 additions & 2 deletions tests/array_fixed_size/array_fixed_size.vv
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ struct Ok {

fn main() {
mut full := [4, 5, 23, 55]!
mut one_missing := [4, 5, 23, 55]!
mut missing := ['John', 'Paul', 'George', 'Ringo']!
mut one_missing := [4, 5, 23, 55, 0]!
mut missing := ['John', 'Paul', 'George', 'Ringo', '', '']!
mut missing_empty := [2]int{}
mut abc := [1, 2, 3]!
mut bytes := [u8(1), 2, 3]!
Expand Down

0 comments on commit b0c8374

Please sign in to comment.