mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
@@ -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)
|
ft := c.table.type_to_str(from_type)
|
||||||
c.error('cannot cast `$ft` to struct', node.pos)
|
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_ {
|
} else if to_sym.kind == .interface_ {
|
||||||
if c.type_implements(from_type, to_type, node.pos) {
|
if c.type_implements(from_type, to_type, node.pos) {
|
||||||
if !from_type.is_ptr() && !from_type.is_pointer() && from_sym.kind != .interface_
|
if !from_type.is_ptr() && !from_type.is_pointer() && from_sym.kind != .interface_
|
||||||
|
7
vlib/v/checker/tests/cast_to_ref_struct_err.out
Normal file
7
vlib/v/checker/tests/cast_to_ref_struct_err.out
Normal 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 | }
|
11
vlib/v/checker/tests/cast_to_ref_struct_err.vv
Normal file
11
vlib/v/checker/tests/cast_to_ref_struct_err.vv
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
pub struct SomeError {
|
||||||
|
mut:
|
||||||
|
msg string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
a := unsafe { &SomeError('yo') }
|
||||||
|
println(a)
|
||||||
|
}
|
Reference in New Issue
Block a user