1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check cast to reference struct (fix #15590) (#15601)

This commit is contained in:
yuyi 2022-08-30 16:42:21 +08:00 committed by GitHub
parent a9b54e9b98
commit 5d4492ac6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 0 deletions

View File

@ -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_

View File

@ -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 | }

View File

@ -0,0 +1,11 @@
module main
pub struct SomeError {
mut:
msg string
}
fn main() {
a := unsafe { &SomeError('yo') }
println(a)
}