cgen: fix sizeof('str') and sizeof(r'str') (#14507)

master
yuyi 2022-05-24 10:14:38 +08:00 committed by GitHub
parent 953ef1f8c9
commit 5ade39f8db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View File

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

View File

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