checker: add a check for `dump(c)`, where `typeof(c) == "char"`

pull/13818/head
Delyan Angelov 2022-03-23 21:44:25 +02:00
parent 78d9975798
commit 9bbb52e9cc
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 15 additions and 1 deletions

View File

@ -2603,10 +2603,16 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
}
ast.DumpExpr {
node.expr_type = c.expr(node.expr)
if node.expr_type.idx() == ast.void_type_idx {
etidx := node.expr_type.idx()
if etidx == ast.void_type_idx {
c.error('dump expression can not be void', node.expr.pos())
return ast.void_type
} else if etidx == ast.char_type_idx && node.expr_type.nr_muls() == 0 {
c.error('`char` values cannot be dumped directly, use dump(byte(x)) or dump(int(x)) instead',
node.expr.pos())
return ast.void_type
}
tsym := c.table.sym(node.expr_type)
c.table.dumps[int(node.expr_type)] = tsym.cname
node.cname = tsym.cname

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/dump_char.vv:3:6: error: `char` values cannot be dumped directly, use dump(byte(x)) or dump(int(x)) instead
1 | c := char(67)
2 | dump(byte(c))
3 | dump(c)
| ^

View File

@ -0,0 +1,3 @@
c := char(67)
dump(byte(c))
dump(c)