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 const fixed array initialization handling #20812

Merged
merged 6 commits into from
Feb 13, 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
38 changes: 35 additions & 3 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -5517,6 +5517,36 @@ fn (mut g Gen) return_stmt(node ast.Return) {
}
}

// check_expr_is_const checks if the expr is elegible to be used as const initializer on C global scope
fn (mut g Gen) check_expr_is_const(expr ast.Expr) bool {
match expr {
ast.StringLiteral, ast.IntegerLiteral, ast.BoolLiteral, ast.FloatLiteral, ast.CharLiteral {
return true
}
ast.ArrayInit {
return expr.exprs.all(g.check_expr_is_const(it))
}
ast.ParExpr {
return g.check_expr_is_const(expr.expr)
}
ast.InfixExpr {
return g.check_expr_is_const(expr.left) && g.check_expr_is_const(expr.right)
}
ast.Ident, ast.StructInit, ast.EnumVal {
return true
}
ast.CastExpr {
return g.check_expr_is_const(expr.expr)
}
ast.PrefixExpr {
return g.check_expr_is_const(expr.right)
}
else {
return false
}
}
}

fn (mut g Gen) const_decl(node ast.ConstDecl) {
g.inside_const = true
defer {
Expand All @@ -5541,17 +5571,19 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
field_expr := field.expr
match field.expr {
ast.ArrayInit {
elems_are_const := field.expr.exprs.all(g.check_expr_is_const(it))
if field.expr.is_fixed && g.pref.build_mode != .build_module
&& (!g.is_cc_msvc || field.expr.elem_type != ast.string_type) {
&& (!g.is_cc_msvc || field.expr.elem_type != ast.string_type) && elems_are_const {
styp := g.typ(field.expr.typ)
val := g.expr_string(field.expr)
g.global_const_defs[util.no_dots(field.name)] = GlobalConstDef{
mod: field.mod
def: '${styp} ${const_name} = ${val}; // fixed array const'
dep_names: g.table.dependent_names_in_expr(field_expr)
}
} else if field.expr.is_fixed && g.is_cc_msvc
&& field.expr.elem_type == ast.string_type {
} else if field.expr.is_fixed
&& ((g.is_cc_msvc && field.expr.elem_type == ast.string_type)
|| !elems_are_const) {
g.const_decl_init_later_msvc_string_fixed_array(field.mod, name, field.expr,
field.typ)
} else {
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/tests/const_global_arr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
struct Foo {
index int
}

const foo5 = Foo{
index: 5
}
const foo2 = Foo{
index: 2
}

const foos = [foo5.index, foo2.index]!

fn test_main() {
fooz := [foo5.index, foo2.index]!
assert foos == fooz
}