From 5d4492ac6f0d1b506421da524b0f33536b776ce0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 30 Aug 2022 16:42:21 +0800 Subject: [PATCH] checker: check cast to reference struct (fix #15590) (#15601) --- vlib/v/checker/checker.v | 10 ++++++++++ vlib/v/checker/tests/cast_to_ref_struct_err.out | 7 +++++++ vlib/v/checker/tests/cast_to_ref_struct_err.vv | 11 +++++++++++ 3 files changed, 28 insertions(+) create mode 100644 vlib/v/checker/tests/cast_to_ref_struct_err.out create mode 100644 vlib/v/checker/tests/cast_to_ref_struct_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a857691d9..bc0150b5f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2430,6 +2430,16 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { ft := c.table.type_to_str(from_type) c.error('cannot cast `$ft` to struct', node.pos) } + } else if to_sym.kind == .struct_ && to_type.is_ptr() { + if from_sym.kind == .alias { + from_type = (from_sym.info as ast.Alias).parent_type.derive_add_muls(from_type) + } + if !from_type.is_int() && final_from_sym.kind != .enum_ && !from_type.is_pointer() + && !from_type.is_ptr() { + ft := c.table.type_to_str(from_type) + tt := c.table.type_to_str(to_type) + c.error('cannot cast `$ft` to `$tt`', node.pos) + } } else if to_sym.kind == .interface_ { if c.type_implements(from_type, to_type, node.pos) { if !from_type.is_ptr() && !from_type.is_pointer() && from_sym.kind != .interface_ diff --git a/vlib/v/checker/tests/cast_to_ref_struct_err.out b/vlib/v/checker/tests/cast_to_ref_struct_err.out new file mode 100644 index 000000000..348aa96ce --- /dev/null +++ b/vlib/v/checker/tests/cast_to_ref_struct_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/cast_to_ref_struct_err.vv:9:16: error: cannot cast `string` to `&SomeError` + 7 | + 8 | fn main() { + 9 | a := unsafe { &SomeError('yo') } + | ~~~~~~~~~~~~~~~~ + 10 | println(a) + 11 | } diff --git a/vlib/v/checker/tests/cast_to_ref_struct_err.vv b/vlib/v/checker/tests/cast_to_ref_struct_err.vv new file mode 100644 index 000000000..f50318bb0 --- /dev/null +++ b/vlib/v/checker/tests/cast_to_ref_struct_err.vv @@ -0,0 +1,11 @@ +module main + +pub struct SomeError { +mut: + msg string +} + +fn main() { + a := unsafe { &SomeError('yo') } + println(a) +} -- 2.30.2