checker: minor cleanup in cast_expr() (#12954)

pull/12957/head
yuyi 2021-12-24 17:38:27 +08:00 committed by GitHub
parent d69d2c600b
commit a83786d867
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 4 deletions

View File

@ -3487,10 +3487,8 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
c.error('cannot convert type `$from_type_sym.name` to `$to_type_sym.name` (alias to `$to_type_sym_final.name`)', c.error('cannot convert type `$from_type_sym.name` to `$to_type_sym.name` (alias to `$to_type_sym_final.name`)',
node.pos) node.pos)
} }
} else if to_type_sym.kind == .byte && from_type != ast.voidptr_type } else if to_type_sym.kind == .byte && from_type_sym.kind == .alias
&& from_type_sym.kind != .enum_ && !from_type.is_int() && !from_type.is_float() && from_type_sym_final.kind != .byte && !from_type.is_ptr() {
&& from_type != ast.bool_type && !from_type.is_ptr() && from_type_sym.kind == .alias
&& from_type_sym_final.name != 'byte' {
type_name := c.table.type_to_str(from_type) type_name := c.table.type_to_str(from_type)
c.error('cannot cast type `$type_name` to `byte`', node.pos) c.error('cannot cast type `$type_name` to `byte`', node.pos)
} else if to_type_sym.kind == .struct_ && !to_type.is_ptr() } else if to_type_sym.kind == .struct_ && !to_type.is_ptr()

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/cast_alias_to_byte_err.vv:5:7: error: cannot cast type `Foo` to `byte`
3 | fn main() {
4 | a := Foo('hello')
5 | b := byte(a)
| ~~~~~~~
6 | println(b)
7 | }

View File

@ -0,0 +1,7 @@
type Foo = string
fn main() {
a := Foo('hello')
b := byte(a)
println(b)
}