checker: optimize check string cast error

pull/4771/head
yuyi 2020-05-07 18:34:06 +08:00 committed by GitHub
parent 9b0b6ec2db
commit 78efe72c4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 6 deletions

View File

@ -1582,16 +1582,12 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
if it.typ == table.string_type && !(sym.kind in [.byte, .byteptr] || sym.kind ==
.array && sym.name == 'array_byte') {
type_name := c.table.type_to_str(it.expr_type)
c.error('cannot cast type `$type_name` to string', it.pos)
c.error('cannot cast type `$type_name` to string, use `x.str()` instead', it.pos)
}
if it.has_arg {
c.expr(it.arg)
}
it.typname = c.table.get_type_symbol(it.typ).name
if it.typ == table.string_type && !it.has_arg && it.expr_type.is_number() && !it.expr_type.is_ptr() {
// s := c.table.get_type_symbol(it.expr_type)
c.error('use `number.str()` instead of `string(number)`', it.pos)
}
return it.typ
}
ast.CallExpr {

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/cast_string_err.v:2:14: error: cannot cast type `int` to string
vlib/v/checker/tests/cast_string_err.v:2:14: error: cannot cast type `int` to string, use `x.str()` instead
1 | fn main() {
2 | a := string(1)
| ^