From aa3ab3ff20a0ce0559175373b870ca257c627e42 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 30 Sep 2024 08:54:39 -0300 Subject: [PATCH] cgen: fix const with c string literal (fix #22358) (#22361) --- vlib/v/gen/c/cgen.v | 3 ++- vlib/v/tests/consts/const_c_string_test.v | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/consts/const_c_string_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index c278393be..0e15b0cc1 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5953,9 +5953,10 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) { } ast.StringLiteral { val := g.expr_string(field.expr) + typ := if field.expr.language == .c { 'char*' } else { 'string' } g.global_const_defs[util.no_dots(field.name)] = GlobalConstDef{ mod: field.mod - def: 'string ${const_name}; // a string literal, inited later' + def: '${typ} ${const_name}; // a string literal, inited later' init: '\t${const_name} = ${val};' order: -1 } diff --git a/vlib/v/tests/consts/const_c_string_test.v b/vlib/v/tests/consts/const_c_string_test.v new file mode 100644 index 000000000..bf904de01 --- /dev/null +++ b/vlib/v/tests/consts/const_c_string_test.v @@ -0,0 +1,7 @@ +const msg = c'test' + +fn test_main() { + unsafe { + assert msg.vstring() == 'test' + } +} -- 2.39.5