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

v.gen.c: fix uninitialised value usages regression after e5fb4e4

This commit is contained in:
Delyan Angelov 2021-09-23 12:18:10 +03:00
parent 8837712f2b
commit 930b95f76c
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 28 additions and 18 deletions

View File

@ -153,7 +153,8 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) {
verror('map() requires an array')
}
g.empty_line = true
g.writeln('$ret_typ $tmp;')
noscan := g.check_noscan(ret_info.elem_type)
g.writeln('$ret_typ $tmp = __new_array${noscan}(0, 0, sizeof($ret_elem_type));')
if g.infix_left_var_name.len > 0 {
g.writeln('if ($g.infix_left_var_name) {')
g.indent++
@ -162,7 +163,6 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) {
g.expr(node.left)
g.writeln(';')
g.writeln('int ${tmp}_len = ${tmp}_orig.len;')
noscan := g.check_noscan(ret_info.elem_type)
g.writeln('$tmp = __new_array${noscan}(0, ${tmp}_len, sizeof($ret_elem_type));\n')
i := g.new_tmp_var()
g.writeln('for (int $i = 0; $i < ${tmp}_len; ++$i) {')
@ -336,7 +336,8 @@ fn (mut g Gen) gen_array_filter(node ast.CallExpr) {
styp := g.typ(node.return_type)
elem_type_str := g.typ(info.elem_type)
g.empty_line = true
g.writeln('$styp $tmp;')
noscan := g.check_noscan(info.elem_type)
g.writeln('$styp $tmp = __new_array${noscan}(0, 0, sizeof($elem_type_str));')
if g.infix_left_var_name.len > 0 {
g.writeln('if ($g.infix_left_var_name) {')
g.indent++
@ -345,7 +346,6 @@ fn (mut g Gen) gen_array_filter(node ast.CallExpr) {
g.expr(node.left)
g.writeln(';')
g.writeln('int ${tmp}_len = ${tmp}_orig.len;')
noscan := g.check_noscan(info.elem_type)
g.writeln('$tmp = __new_array${noscan}(0, ${tmp}_len, sizeof($elem_type_str));\n')
i := g.new_tmp_var()
g.writeln('for (int $i = 0; $i < ${tmp}_len; ++$i) {')

View File

@ -1,4 +1,4 @@
fn test_if_expr_with_array_call() {
fn test_if_expr_with_array_call_all() {
arr := ['']
for i in arr {
@ -14,6 +14,10 @@ fn test_if_expr_with_array_call() {
assert true
}
}
}
fn test_if_expr_with_array_call_any() {
arr := ['']
for i in arr {
if i.len == 0 || i[1..].bytes().any(it.is_letter()) {
@ -28,25 +32,31 @@ fn test_if_expr_with_array_call() {
assert true
}
}
}
arr2 := ['abc']
fn test_if_expr_with_array_call_map() {
arr := ['abc']
if arr2.len == 1 || arr2[1].bytes().map(it.is_letter())[0] {
if arr.len == 1 || arr[1].bytes().map(it.is_letter())[0] {
println('yes')
assert true
}
if arr2.len == 1 || (arr2[1].bytes().map(it.is_letter())[0]) {
println('yes')
assert true
}
if arr2.len == 1 || arr2[1].bytes().filter(it.is_letter()).len == 0 {
println('yes')
assert true
}
if arr2.len == 1 || (arr2[1].bytes().filter(it.is_letter()).len == 0) {
if arr.len == 1 || (arr[1].bytes().map(it.is_letter())[0]) {
println('yes')
assert true
}
}
fn test_if_expr_with_array_call_filter() {
arr := ['abc']
if arr.len == 1 || arr[1].bytes().filter(it.is_letter()).len == 0 {
println('yes')
assert true
}
if arr.len == 1 || (arr[1].bytes().filter(it.is_letter()).len == 0) {
println('yes')
assert true
}