parser: fix sizeof(c'str') (fix #14499) (#14502)

master
yuyi 2022-05-23 06:59:39 +08:00 committed by GitHub
parent 863eeca2e0
commit 4ef9e2c05a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 4 deletions

View File

@ -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) {

View File

@ -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 {

View File

@ -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
}