checker: add more casts checks (#7355)

pull/7363/head
Swastik Baranwal 2020-12-16 22:20:39 +05:30 committed by GitHub
parent 1ee57649b9
commit 1a2c7cd336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View File

@ -3117,9 +3117,9 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
}
pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type {
node.expr_type = c.expr(node.expr)
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)
to_type_sym := c.table.get_type_symbol(node.typ) // type to be used as cast
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 {
@ -3189,6 +3189,10 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type {
[.sum_type, .interface_] && !c.is_builtin_mod {
type_name := c.table.type_to_str(node.typ)
c.error('cannot cast `struct` to `$type_name`', node.pos)
} else if node.expr_type.has_flag(.optional) || node.expr_type.has_flag(.variadic) {
// variadic case can happen when arrays are converted into variadic
msg := if node.expr_type.has_flag(.optional) { 'an optional' } else { 'a variadic' }
c.error('cannot type cast $msg', node.pos)
}
if node.has_arg {
c.expr(node.arg)

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/type_cast_optional_err.vv:2:13: error: cannot type cast an optional
1 | fn main() {
2 | println(int('hi'.last_index('i')))
| ~~~~~~~~~~~~~~~~~~~~~~~~~
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
println(int('hi'.last_index('i')))
}