Skip to content

Commit

Permalink
cgen: fix array append map value with or expr (fix #22674) (#22678)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Oct 28, 2024
1 parent 7bd2bef commit 08c2019
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions vlib/v/gen/c/index.v
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
zero := g.type_default(info.value_type)
g.write('${zero} })))')
}
} else if g.inside_map_postfix || g.inside_map_infix || g.inside_map_index
|| g.inside_array_index
|| (g.is_assign_lhs && !g.is_arraymap_set && get_and_set_types) {
} else if !gen_or && (g.inside_map_postfix || g.inside_map_infix
|| g.inside_map_index || g.inside_array_index || (g.is_assign_lhs && !g.is_arraymap_set
&& get_and_set_types)) {
zero := g.type_default(info.value_type)
if node.is_setter {
g.write('(*(${val_type_str}*)map_get_and_set((map*)')
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/tests/builtin_arrays/array_append_map_with_or_expr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn test_array_append_map_with_or_expr() {
foobar := {
'foo': 'Foo'
'bar': 'Bar'
}
mut arr := []string{}
arr << foobar['foo']
arr << foobar['baz'] or { 'Unknown' }
assert arr == ['Foo', 'Unknown']
}

0 comments on commit 08c2019

Please sign in to comment.