checker: prevent string(byte), suggest byte.str() instead

pull/5806/head
Delyan Angelov 2020-07-11 17:09:15 +03:00
parent 839bd0e150
commit 37d739684c
3 changed files with 11 additions and 0 deletions

View File

@ -2126,6 +2126,9 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
node.expr_type = c.expr(node.expr)
from_type_sym := c.table.get_type_symbol(node.expr_type)
to_type_sym := c.table.get_type_symbol(node.typ)
if node.expr_type == table.byte_type && to_type_sym.kind == .string {
c.error('can not cast type `byte` to string, use `${node.expr.str()}.str()` instead.', node.pos)
}
if to_type_sym.kind == .sum_type {
if node.expr_type in [table.any_int_type, table.any_flt_type] {
node.expr_type = c.promote_num(node.expr_type, if node.expr_type == table.any_int_type { table.int_type } else { table.f64_type })

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/cast_string_with_byte_err.v:2:19: error: can not cast type `byte` to string, use `by.str()` instead.
1 | for by in 'abc' {
2 | println(string(by))
| ~~
3 | }

View File

@ -0,0 +1,3 @@
for by in 'abc' {
println(string(by))
}