checker: disallow string(bool) (#6863)

pull/6866/head
Swastik Baranwal 2020-11-18 01:53:17 +05:30 committed by GitHub
parent 9f5a3b52a7
commit 4415feb2df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View File

@ -3032,7 +3032,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type {
node.pos)
}
} else if node.typ == table.string_type &&
(from_type_sym.kind in [.any_int, .int, .byte, .byteptr] ||
(from_type_sym.kind in [.any_int, .int, .byte, .byteptr, .bool] ||
(from_type_sym.kind == .array && from_type_sym.name == 'array_byte')) {
type_name := c.table.type_to_str(node.expr_type)
c.error('cannot cast type `$type_name` to string, use `x.str()` instead', node.pos)

View File

@ -0,0 +1,12 @@
vlib/v/checker/tests/bool_string_cast_err.vv:2:13: error: cannot cast type `bool` to string, use `x.str()` instead
1 | fn main() {
2 | println(string(true))
| ~~~~~~~~~~~~
3 | println(string(false))
4 | }
vlib/v/checker/tests/bool_string_cast_err.vv:3:13: error: cannot cast type `bool` to string, use `x.str()` instead
1 | fn main() {
2 | println(string(true))
3 | println(string(false))
| ~~~~~~~~~~~~~
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
println(string(true))
println(string(false))
}