checker: fix error for cast to u8 (#13058)

pull/13072/head
yuyi 2022-01-07 00:36:31 +08:00 committed by GitHub
parent 1654d8b810
commit 1c382ad2b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -2669,7 +2669,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
}
} else if mut to_sym.info is ast.Alias {
if !c.check_types(from_type, to_sym.info.parent_type) && !(final_to_sym.is_int()
&& final_from_sym.kind == .enum_) {
&& final_from_sym.kind in [.enum_, .bool]) {
c.error('cannot convert type `$from_sym.name` to `$to_sym.name` (alias to `$final_to_sym.name`)',
node.pos)
}

View File

@ -0,0 +1,16 @@
module main
fn test_cast_to_alias() {
r1 := u8(byte(1))
println(r1)
assert '$r1' == '1'
r2 := u8(byte(true))
println(r2)
assert '$r2' == '1'
r3 := u8(true)
println(r3)
assert '$r3' == '1'
}