Skip to content

Commit a83786d

Browse files
authored
checker: minor cleanup in cast_expr() (#12954)
1 parent d69d2c6 commit a83786d

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

vlib/v/checker/checker.v

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3487,10 +3487,8 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
34873487
c.error('cannot convert type `$from_type_sym.name` to `$to_type_sym.name` (alias to `$to_type_sym_final.name`)',
34883488
node.pos)
34893489
}
3490-
} else if to_type_sym.kind == .byte && from_type != ast.voidptr_type
3491-
&& from_type_sym.kind != .enum_ && !from_type.is_int() && !from_type.is_float()
3492-
&& from_type != ast.bool_type && !from_type.is_ptr() && from_type_sym.kind == .alias
3493-
&& from_type_sym_final.name != 'byte' {
3490+
} else if to_type_sym.kind == .byte && from_type_sym.kind == .alias
3491+
&& from_type_sym_final.kind != .byte && !from_type.is_ptr() {
34943492
type_name := c.table.type_to_str(from_type)
34953493
c.error('cannot cast type `$type_name` to `byte`', node.pos)
34963494
} else if to_type_sym.kind == .struct_ && !to_type.is_ptr()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/cast_alias_to_byte_err.vv:5:7: error: cannot cast type `Foo` to `byte`
2+
3 | fn main() {
3+
4 | a := Foo('hello')
4+
5 | b := byte(a)
5+
| ~~~~~~~
6+
6 | println(b)
7+
7 | }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type Foo = string
2+
3+
fn main() {
4+
a := Foo('hello')
5+
b := byte(a)
6+
println(b)
7+
}

0 commit comments

Comments
 (0)