Skip to content

Commit ccb3d5c

Browse files
authored
cgen: fix codegen for const to c string (fix #24235) (#24248)
1 parent f3fb8b4 commit ccb3d5c

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎vlib/v/gen/c/consts_and_globals.v‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,15 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, field_name string
227227
// TODO: ^ the above for strings, cause:
228228
// `error C2099: initializer is not a constant` errors in MSVC,
229229
// so fall back to the delayed initialisation scheme:
230+
init := if typ == ast.string_type {
231+
'_SLIT("${escaped_val}")'
232+
} else {
233+
'(${styp})"${escaped_val}"'
234+
}
230235
g.global_const_defs[util.no_dots(field_name)] = GlobalConstDef{
231236
mod: mod
232237
def: '${styp} ${cname}; // str inited later'
233-
init: '\t${cname} = _SLIT("${escaped_val}");'
238+
init: '\t${cname} = ${init};'
234239
order: -1
235240
}
236241
if g.is_autofree {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub const global_text = c'Text'
2+
pub const global_ref = &global_text
3+
pub const global_copy = global_text
4+
5+
fn test_main() {
6+
local_copy := global_text
7+
println('${global_text}')
8+
println('${global_ref}')
9+
println('${local_copy}')
10+
println('${global_copy}')
11+
assert *global_copy == c'Text'
12+
assert **global_ref == c'Text'
13+
assert *global_text == c'Text'
14+
}

0 commit comments

Comments
 (0)