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:
parent
001144fa82
commit
672066b65b
@ -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 { '' }
|
tmp_opt_ptr := if gen_or { g.new_tmp_var() } else { '' }
|
||||||
if gen_or {
|
if gen_or {
|
||||||
g.write('$elem_type_str* $tmp_opt_ptr = ($elem_type_str*)/*ee elem_ptr_typ */(array_get_with_check(')
|
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 {
|
} else {
|
||||||
if needs_clone {
|
if needs_clone {
|
||||||
g.write('/*2*/string_clone(')
|
g.write('/*2*/string_clone(')
|
||||||
|
23
vlib/v/tests/array_index_optional.v
Normal file
23
vlib/v/tests/array_index_optional.v
Normal 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'
|
||||||
|
}
|
@ -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'
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user