mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parent
d336b7b877
commit
90287f6aaa
@ -2500,6 +2500,14 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
|
||||
c.error(error_msg, node.pos)
|
||||
}
|
||||
}
|
||||
if from_sym.language == .v && !from_type.is_ptr()
|
||||
&& final_from_sym.kind in [.sum_type, .interface_]
|
||||
&& final_to_sym.kind !in [.sum_type, .interface_] {
|
||||
ft := c.table.type_to_str(from_type)
|
||||
tt := c.table.type_to_str(to_type)
|
||||
c.error('cannot cast `$ft` to `$tt`, please use `as` instead, e.g. `expr as Ident`',
|
||||
node.pos)
|
||||
}
|
||||
|
||||
if node.has_arg {
|
||||
c.expr(node.arg)
|
||||
|
6
vlib/v/checker/tests/cast_sumtype_err.out
Normal file
6
vlib/v/checker/tests/cast_sumtype_err.out
Normal file
@ -0,0 +1,6 @@
|
||||
vlib/v/checker/tests/cast_sumtype_err.vv:7:14: error: cannot cast `Test` to `int`, please use `as` instead, e.g. `expr as Ident`
|
||||
5 | fn main() {
|
||||
6 | data := Test(12)
|
||||
7 | eprintln(int(data))
|
||||
| ~~~~~~~~~
|
||||
8 | }
|
8
vlib/v/checker/tests/cast_sumtype_err.vv
Normal file
8
vlib/v/checker/tests/cast_sumtype_err.vv
Normal file
@ -0,0 +1,8 @@
|
||||
module main
|
||||
|
||||
type Test = int | u32 | f32
|
||||
|
||||
fn main() {
|
||||
data := Test(12)
|
||||
eprintln(int(data))
|
||||
}
|
Loading…
Reference in New Issue
Block a user