Skip to content

Commit

Permalink
cgen: fix const with c string literal (fix #22358) (#22361)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Sep 30, 2024
1 parent 04aba8f commit aa3ab3f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/tests/consts/const_c_string_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const msg = c'test'

fn test_main() {
unsafe {
assert msg.vstring() == 'test'
}
}

0 comments on commit aa3ab3f

Please sign in to comment.