From a83786d867a6bf2a9d643a093731394cbe0f2322 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 24 Dec 2021 17:38:27 +0800 Subject: [PATCH] checker: minor cleanup in cast_expr() (#12954) --- vlib/v/checker/checker.v | 6 ++---- vlib/v/checker/tests/cast_alias_to_byte_err.out | 7 +++++++ vlib/v/checker/tests/cast_alias_to_byte_err.vv | 7 +++++++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 vlib/v/checker/tests/cast_alias_to_byte_err.out create mode 100644 vlib/v/checker/tests/cast_alias_to_byte_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b29cd7c09d..43a863eea9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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`)', node.pos) } - } else if to_type_sym.kind == .byte && from_type != ast.voidptr_type - && from_type_sym.kind != .enum_ && !from_type.is_int() && !from_type.is_float() - && from_type != ast.bool_type && !from_type.is_ptr() && from_type_sym.kind == .alias - && from_type_sym_final.name != 'byte' { + } else if to_type_sym.kind == .byte && from_type_sym.kind == .alias + && from_type_sym_final.kind != .byte && !from_type.is_ptr() { type_name := c.table.type_to_str(from_type) c.error('cannot cast type `$type_name` to `byte`', node.pos) } else if to_type_sym.kind == .struct_ && !to_type.is_ptr() diff --git a/vlib/v/checker/tests/cast_alias_to_byte_err.out b/vlib/v/checker/tests/cast_alias_to_byte_err.out new file mode 100644 index 0000000000..e9404d5da3 --- /dev/null +++ b/vlib/v/checker/tests/cast_alias_to_byte_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/cast_alias_to_byte_err.vv b/vlib/v/checker/tests/cast_alias_to_byte_err.vv new file mode 100644 index 0000000000..29d837e84c --- /dev/null +++ b/vlib/v/checker/tests/cast_alias_to_byte_err.vv @@ -0,0 +1,7 @@ +type Foo = string + +fn main() { + a := Foo('hello') + b := byte(a) + println(b) +}