Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix array fixed with default init #22059

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -5777,31 +5777,40 @@ fn (mut g Gen) return_stmt(node ast.Return) {
g.expr_with_opt(node.exprs[0], node.types[0], g.fn_decl.return_type)
} else {
if fn_return_is_fixed_array && !node.types[0].has_option_or_result() {
g.writeln('{0};')
if node.exprs[0] is ast.Ident {
g.writeln('{0};')
typ := if expr0.is_auto_deref_var() {
node.types[0].deref()
} else {
node.types[0]
}
g.write('memcpy(${tmpvar}.ret_arr, ${g.expr_string(node.exprs[0])}, sizeof(${g.typ(typ)})) /*ret*/')
g.write('memcpy(${tmpvar}.ret_arr, ${g.expr_string(node.exprs[0])}, sizeof(${g.typ(typ)}))')
} else if node.exprs[0] in [ast.ArrayInit, ast.StructInit] {
if node.exprs[0] is ast.ArrayInit && node.exprs[0].is_fixed
&& node.exprs[0].has_init {
g.write('memcpy(${tmpvar}.ret_arr, ')
g.expr_with_cast(node.exprs[0], node.types[0], g.fn_decl.return_type)
g.write(', sizeof(${g.typ(node.types[0])})) /*ret*/')
if (node.exprs[0] as ast.ArrayInit).init_expr.is_literal() {
g.write('{.ret_arr=')
g.expr_with_cast(node.exprs[0], node.types[0], g.fn_decl.return_type)
g.writeln('};')
} else {
g.writeln('{0};')
g.write('memcpy(${tmpvar}.ret_arr, ')
g.expr_with_cast(node.exprs[0], node.types[0], g.fn_decl.return_type)
g.write(', sizeof(${g.typ(node.types[0])}))')
}
} else {
g.writeln('{0};')
tmpvar2 := g.new_tmp_var()
g.write('${g.typ(node.types[0])} ${tmpvar2} = ')
g.expr_with_cast(node.exprs[0], node.types[0], g.fn_decl.return_type)
g.writeln(';')
g.write('memcpy(${tmpvar}.ret_arr, ${tmpvar2}, sizeof(${g.typ(node.types[0])})) /*ret*/')
g.write('memcpy(${tmpvar}.ret_arr, ${tmpvar2}, sizeof(${g.typ(node.types[0])}))')
}
} else {
g.writeln('{0};')
g.write('memcpy(${tmpvar}.ret_arr, ')
g.expr_with_cast(node.exprs[0], node.types[0], g.fn_decl.return_type)
g.write(', sizeof(${g.typ(node.types[0])})) /*ret*/')
g.write(', sizeof(${g.typ(node.types[0])}))')
}
} else {
g.expr_with_cast(node.exprs[0], node.types[0], g.fn_decl.return_type)
Expand Down
26 changes: 26 additions & 0 deletions vlib/v/tests/array_init_fixed_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module main

pub const n = 7
pub const nsq = n * n

pub fn empty_board(with_init bool) [nsq]u8 {
if with_init {
a := [nsq]u8{init: 0}
dump(a)
return [nsq]u8{init: 0}
} else {
a := [nsq]u8{}
dump(a)
return [nsq]u8{}
}
}

fn test_with_init() {
a := dump(empty_board(true))
assert a.len == 49
}

fn test_without_init() {
a := dump(empty_board(false))
assert a.len == 49
}
Loading