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

cgen: fix optional indexes with mutable arrays (#15399)

This commit is contained in:
StunxFS 2022-08-13 04:58:31 -04:00 committed by GitHub
parent 001144fa82
commit 672066b65b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 7 deletions

View File

@ -252,6 +252,9 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
tmp_opt_ptr := if gen_or { g.new_tmp_var() } else { '' }
if gen_or {
g.write('$elem_type_str* $tmp_opt_ptr = ($elem_type_str*)/*ee elem_ptr_typ */(array_get_with_check(')
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
g.write('*')
}
} else {
if needs_clone {
g.write('/*2*/string_clone(')

View File

@ -0,0 +1,23 @@
struct Arrs {
mut:
x [][]int
}
fn test_mut_array_index_optional() {
mut arr := Arrs{
x: [[1, 2]]
}
for mut sub_arr in arr.x {
x := sub_arr[0] or { 3 }
println(x)
assert x == 1
}
}
fn test_array_index_optional_with_if_expr() {
ret := []string{}[0] or {
if true { 'a' } else { 'b' }
}
println(ret)
assert ret == 'a'
}

View File

@ -1,7 +0,0 @@
fn test_array_index_optional_with_if_expr() {
ret := []string{}[0] or {
if true { 'a' } else { 'b' }
}
println(ret)
assert ret == 'a'
}