diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 188fff8509..fe53336286 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) diff --git a/vlib/v/checker/tests/type_cast_optional_err.out b/vlib/v/checker/tests/type_cast_optional_err.out new file mode 100644 index 0000000000..bb52bad666 --- /dev/null +++ b/vlib/v/checker/tests/type_cast_optional_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/type_cast_optional_err.vv b/vlib/v/checker/tests/type_cast_optional_err.vv new file mode 100644 index 0000000000..982559cbda --- /dev/null +++ b/vlib/v/checker/tests/type_cast_optional_err.vv @@ -0,0 +1,3 @@ +fn main() { + println(int('hi'.last_index('i'))) +}