checker: disallow string(bool) (#6863)
parent
9f5a3b52a7
commit
4415feb2df
|
@ -3032,7 +3032,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type {
|
||||||
node.pos)
|
node.pos)
|
||||||
}
|
}
|
||||||
} else if node.typ == table.string_type &&
|
} 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')) {
|
(from_type_sym.kind == .array && from_type_sym.name == 'array_byte')) {
|
||||||
type_name := c.table.type_to_str(node.expr_type)
|
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)
|
c.error('cannot cast type `$type_name` to string, use `x.str()` instead', node.pos)
|
||||||
|
|
|
@ -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 | }
|
|
@ -0,0 +1,4 @@
|
||||||
|
fn main() {
|
||||||
|
println(string(true))
|
||||||
|
println(string(false))
|
||||||
|
}
|
Loading…
Reference in New Issue