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

cgen: support explicit array handling even in [direct_array_access] functions (#8241)

This commit is contained in:
Uwe Krüger 2021-01-21 12:43:54 +01:00 committed by GitHub
parent c6d6690064
commit 3ecbf78707
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -4105,7 +4105,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write('.val')
}
}
if is_direct_array_access {
if is_direct_array_access && !gen_or {
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
g.write('->')
} else {

View File

@ -14,6 +14,23 @@ fn test_array_or() {
assert good == 4
}
[direct_array_access]
fn test_array_or_direct() {
m := [3, 4, 5]
mut testvar := 17
el := m[4] or {
testvar = -43
999
}
good := m[1] or {
testvar = 11
0
}
assert testvar == -43
assert el == 999
assert good == 4
}
fn test_map_or() {
m := {
'as': 3
@ -47,6 +64,13 @@ fn get_arr_el(i int) ?int {
return r
}
[direct_array_access]
fn get_arr_el_direct(i int) ?int {
m := [3, 4, 5]
r := m[i] ?
return r
}
fn test_propagation() {
mut testvar1 := 12
mut testvar2 := 78
@ -66,12 +90,20 @@ fn test_propagation() {
testvar2 = 177
int(-67)
}
m := get_arr_el_direct(3) or {
17
}
n := get_arr_el_direct(0) or {
int(-73)
}
assert testvar1 == -34
assert testvar2 == 99
assert e == 7
assert f == 3
assert g == 12
assert h == 3
assert m == 17
assert n == 3
}
fn get_arr_el_nested(i int) ?int {