diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index c5f129d2a1..48acb8f012 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5449,8 +5449,12 @@ fn (mut g Gen) size_of(node ast.SizeOf) { if sym.language == .v && sym.kind in [.placeholder, .any] { g.error('unknown type `$sym.name`', node.pos) } - styp := g.typ(node_typ) - g.write('sizeof(${util.no_dots(styp)})') + 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)})') + } } fn (mut g Gen) enum_val(node ast.EnumVal) { diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 560d606539..9d02331c91 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -202,12 +202,17 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr { p.next() // sizeof p.check(.lpar) pos := p.tok.pos() - is_known_var := p.mark_var_as_used(p.tok.lit) + mut is_known_var := p.mark_var_as_used(p.tok.lit) || p.table.global_scope.known_const(p.mod + '.' + p.tok.lit) //|| p.table.known_fn(p.mod + '.' + p.tok.lit) // assume `mod.` prefix leads to a type - is_type := p.known_import(p.tok.lit) || p.tok.kind.is_start_of_type() + mut is_type := p.known_import(p.tok.lit) || p.tok.kind.is_start_of_type() || (p.tok.lit.len > 0 && p.tok.lit[0].is_capital()) + + if p.tok.lit in ['c', 'r'] && p.peek_tok.kind == .string { + is_known_var = false + is_type = false + } if is_known_var || !is_type { expr := p.expr(0) if is_reftype { diff --git a/vlib/v/tests/sizeof_test.v b/vlib/v/tests/sizeof_test.v index 2242680209..1d56001d8e 100644 --- a/vlib/v/tests/sizeof_test.v +++ b/vlib/v/tests/sizeof_test.v @@ -23,4 +23,7 @@ fn test_sizeof() { s := S2{} assert sizeof(s.i) == 4 assert sizeof(flag.Flag) > 4 + + assert sizeof(c'hello') == 6 + assert sizeof(r'hello') == 6 }