checker: add a check for casting to an unknown type (#8968)
parent
bc0507590e
commit
3a082621c9
|
@ -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
|
node.expr_type = c.expr(node.expr) // type to be casted
|
||||||
from_type_sym := c.table.get_type_symbol(node.expr_type)
|
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
|
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
|
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 expr_is_ptr && to_type_sym.kind == .string && !node.in_prexpr {
|
||||||
if node.has_arg {
|
if node.has_arg {
|
||||||
|
|
|
@ -17,4 +17,11 @@ vlib/v/checker/tests/cast_err.vv:5:6: error: cannot cast to bool - use e.g. `som
|
||||||
4 | _ = bool(&v)
|
4 | _ = bool(&v)
|
||||||
5 | _ = bool([2])
|
5 | _ = bool([2])
|
||||||
| ~~~~~~~~~
|
| ~~~~~~~~~
|
||||||
6 | }
|
6 | }
|
||||||
|
7 |
|
||||||
|
vlib/v/checker/tests/cast_err.vv:9:6: error: unknown type `Foo`
|
||||||
|
7 |
|
||||||
|
8 | fn unknown() {
|
||||||
|
9 | _ = Foo(3)
|
||||||
|
| ~~~~~~
|
||||||
|
10 | }
|
|
@ -4,3 +4,7 @@ fn test_bool_cast() {
|
||||||
_ = bool(&v)
|
_ = bool(&v)
|
||||||
_ = bool([2])
|
_ = bool([2])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn unknown() {
|
||||||
|
_ = Foo(3)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue