mirror of
https://github.com/vlang/v.git
synced 2025-09-09 15:27:05 -04:00
parent
f3fb8b46ad
commit
ccb3d5cfcd
@ -227,10 +227,15 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, field_name string
|
|||||||
// TODO: ^ the above for strings, cause:
|
// TODO: ^ the above for strings, cause:
|
||||||
// `error C2099: initializer is not a constant` errors in MSVC,
|
// `error C2099: initializer is not a constant` errors in MSVC,
|
||||||
// so fall back to the delayed initialisation scheme:
|
// 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{
|
g.global_const_defs[util.no_dots(field_name)] = GlobalConstDef{
|
||||||
mod: mod
|
mod: mod
|
||||||
def: '${styp} ${cname}; // str inited later'
|
def: '${styp} ${cname}; // str inited later'
|
||||||
init: '\t${cname} = _SLIT("${escaped_val}");'
|
init: '\t${cname} = ${init};'
|
||||||
order: -1
|
order: -1
|
||||||
}
|
}
|
||||||
if g.is_autofree {
|
if g.is_autofree {
|
||||||
|
14
vlib/v/tests/consts/const_cstr_test.v
Normal file
14
vlib/v/tests/consts/const_cstr_test.v
Normal 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'
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user