diff --git a/vlib/v/gen/c/consts_and_globals.v b/vlib/v/gen/c/consts_and_globals.v index 71e8930013..0a4ec91b9c 100644 --- a/vlib/v/gen/c/consts_and_globals.v +++ b/vlib/v/gen/c/consts_and_globals.v @@ -227,10 +227,15 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, field_name string // TODO: ^ the above for strings, cause: // `error C2099: initializer is not a constant` errors in MSVC, // so fall back to the delayed initialisation scheme: + init := if typ == ast.string_type { + '_SLIT("${escaped_val}")' + } else { + '(${styp})"${escaped_val}"' + } g.global_const_defs[util.no_dots(field_name)] = GlobalConstDef{ mod: mod def: '${styp} ${cname}; // str inited later' - init: '\t${cname} = _SLIT("${escaped_val}");' + init: '\t${cname} = ${init};' order: -1 } if g.is_autofree { diff --git a/vlib/v/tests/consts/const_cstr_test.v b/vlib/v/tests/consts/const_cstr_test.v new file mode 100644 index 0000000000..905f3e2592 --- /dev/null +++ b/vlib/v/tests/consts/const_cstr_test.v @@ -0,0 +1,14 @@ +pub const global_text = c'Text' +pub const global_ref = &global_text +pub const global_copy = global_text + +fn test_main() { + local_copy := global_text + println('${global_text}') + println('${global_ref}') + println('${local_copy}') + println('${global_copy}') + assert *global_copy == c'Text' + assert **global_ref == c'Text' + assert *global_text == c'Text' +}