diff --git a/vlib/v/gen/c/consts_and_globals.v b/vlib/v/gen/c/consts_and_globals.v index 11aeb43c1c..e92febfb9b 100644 --- a/vlib/v/gen/c/consts_and_globals.v +++ b/vlib/v/gen/c/consts_and_globals.v @@ -340,10 +340,15 @@ fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ func := (expr_sym.info as ast.FnType).func def = g.fn_var_signature(func.return_type, func.params.map(it.typ), cname) } + init_str := init.str().trim_right('\n') g.global_const_defs[util.no_dots(name)] = GlobalConstDef{ mod: mod def: '${def}; // inited later' - init: init.str().trim_right('\n') + init: if init_str.count('\n') > 1 { + '{\n${init_str}\n}' + } else { + init_str + } dep_names: g.table.dependent_names_in_expr(expr) } if g.is_autofree { diff --git a/vlib/v/slow_tests/run_project_folders/issue_24521_constants_in_different_v_files/a.v b/vlib/v/slow_tests/run_project_folders/issue_24521_constants_in_different_v_files/a.v new file mode 100644 index 0000000000..cb5acd0216 --- /dev/null +++ b/vlib/v/slow_tests/run_project_folders/issue_24521_constants_in_different_v_files/a.v @@ -0,0 +1,12 @@ +module main + +import json + +pub struct JwtHeader { +pub: + alg string + typ string + cty string +} + +const f1 = json.encode(JwtHeader{ alg: '1', typ: '2', cty: '3' }) diff --git a/vlib/v/slow_tests/run_project_folders/issue_24521_constants_in_different_v_files/b.v b/vlib/v/slow_tests/run_project_folders/issue_24521_constants_in_different_v_files/b.v new file mode 100644 index 0000000000..96e98cdfcb --- /dev/null +++ b/vlib/v/slow_tests/run_project_folders/issue_24521_constants_in_different_v_files/b.v @@ -0,0 +1,5 @@ +module main + +import json + +const f2 = json.encode(JwtHeader{ alg: 'a', typ: 'b', cty: 'c' }) diff --git a/vlib/v/slow_tests/run_project_folders/issue_24521_constants_in_different_v_files/main.v b/vlib/v/slow_tests/run_project_folders/issue_24521_constants_in_different_v_files/main.v new file mode 100644 index 0000000000..a0fcc3ffd7 --- /dev/null +++ b/vlib/v/slow_tests/run_project_folders/issue_24521_constants_in_different_v_files/main.v @@ -0,0 +1,7 @@ +module main + +fn main() { + println(f1) + println(f2) + println('OK') +}