From 3a082621c906d0a5708ff3d2951a4954ded531f8 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Fri, 26 Feb 2021 06:26:36 +0000 Subject: [PATCH] checker: add a check for casting to an unknown type (#8968) --- vlib/v/checker/checker.v | 3 +++ vlib/v/checker/tests/cast_err.out | 9 ++++++++- vlib/v/checker/tests/cast_err.vv | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index bd3bea8606..d0c3a19a39 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 { diff --git a/vlib/v/checker/tests/cast_err.out b/vlib/v/checker/tests/cast_err.out index 86b7fdadbe..c78a99dd58 100644 --- a/vlib/v/checker/tests/cast_err.out +++ b/vlib/v/checker/tests/cast_err.out @@ -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) 5 | _ = bool([2]) | ~~~~~~~~~ - 6 | } \ No newline at end of file + 6 | } + 7 | +vlib/v/checker/tests/cast_err.vv:9:6: error: unknown type `Foo` + 7 | + 8 | fn unknown() { + 9 | _ = Foo(3) + | ~~~~~~ + 10 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/cast_err.vv b/vlib/v/checker/tests/cast_err.vv index de0faa1132..bcf8ad3935 100644 --- a/vlib/v/checker/tests/cast_err.vv +++ b/vlib/v/checker/tests/cast_err.vv @@ -4,3 +4,7 @@ fn test_bool_cast() { _ = bool(&v) _ = bool([2]) } + +fn unknown() { + _ = Foo(3) +}