cgen: fix codegen for const to c string (fix #24235) (#24248)

This commit is contained in:
Felipe Pena 2025-04-16 11:36:28 -03:00 committed by GitHub
parent f3fb8b46ad
commit ccb3d5cfcd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 1 deletions

View File

@ -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 {

View File

@ -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'
}