Skip to content

Commit

Permalink
checker: disallow string(bool) (#6863)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Nov 17, 2020
1 parent 9f5a3b5 commit 4415feb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/checker/tests/bool_string_cast_err.out
Original file line number Diff line number Diff line change
@@ -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 | }
4 changes: 4 additions & 0 deletions vlib/v/checker/tests/bool_string_cast_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println(string(true))
println(string(false))
}

0 comments on commit 4415feb

Please sign in to comment.