From 5ade39f8dbdaf996099aabe73c18226dd946241e Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 24 May 2022 10:14:38 +0800 Subject: [PATCH] cgen: fix sizeof('str') and sizeof(r'str') (#14507) --- vlib/v/gen/c/cgen.v | 10 ++++++---- vlib/v/tests/sizeof_test.v | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 48acb8f012..ce20b6294b 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5450,11 +5450,13 @@ fn (mut g Gen) size_of(node ast.SizeOf) { g.error('unknown type `$sym.name`', node.pos) } if node.expr is ast.StringLiteral { - g.write('sizeof("$node.expr.val")') - } else { - styp := g.typ(node_typ) - g.write('sizeof(${util.no_dots(styp)})') + if node.expr.language == .c { + g.write('sizeof("$node.expr.val")') + return + } } + styp := g.typ(node_typ) + g.write('sizeof(${util.no_dots(styp)})') } fn (mut g Gen) enum_val(node ast.EnumVal) { diff --git a/vlib/v/tests/sizeof_test.v b/vlib/v/tests/sizeof_test.v index 1d56001d8e..34a808523f 100644 --- a/vlib/v/tests/sizeof_test.v +++ b/vlib/v/tests/sizeof_test.v @@ -25,5 +25,6 @@ fn test_sizeof() { assert sizeof(flag.Flag) > 4 assert sizeof(c'hello') == 6 - assert sizeof(r'hello') == 6 + assert sizeof(r'hello') == 16 + assert sizeof('hello') == 16 }