checker: add a check for casting to an unknown type (#8968)

pull/8980/head
Nick Treleaven 2021-02-26 06:26:36 +00:00 committed by GitHub
parent bc0507590e
commit 3a082621c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 1 deletions

View File

@ -3802,6 +3802,9 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type {
node.expr_type = c.expr(node.expr) // type to be casted
from_type_sym := c.table.get_type_symbol(node.expr_type)
to_type_sym := c.table.get_type_symbol(node.typ) // type to be used as cast
if to_type_sym.kind == .placeholder && to_type_sym.language != .c {
c.error('unknown type `$to_type_sym.name`', node.pos)
}
expr_is_ptr := node.expr_type.is_ptr() || node.expr_type.idx() in table.pointer_type_idxs
if expr_is_ptr && to_type_sym.kind == .string && !node.in_prexpr {
if node.has_arg {

View File

@ -18,3 +18,10 @@ vlib/v/checker/tests/cast_err.vv:5:6: error: cannot cast to bool - use e.g. `som
5 | _ = bool([2])
| ~~~~~~~~~
6 | }
7 |
vlib/v/checker/tests/cast_err.vv:9:6: error: unknown type `Foo`
7 |
8 | fn unknown() {
9 | _ = Foo(3)
| ~~~~~~
10 | }

View File

@ -4,3 +4,7 @@ fn test_bool_cast() {
_ = bool(&v)
_ = bool([2])
}
fn unknown() {
_ = Foo(3)
}