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

pull/13080/head
Ken 2022-01-07 19:54:11 +09:00 committed by GitHub
parent 60527a5655
commit 7dc64de360
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 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 in [.enum_, .bool]) {
&& final_from_sym.kind in [.enum_, .bool, .i8, .char]) {
c.error('cannot convert type `$from_sym.name` to `$to_sym.name` (alias to `$final_to_sym.name`)',
node.pos)
}

View File

@ -12,4 +12,16 @@ fn test_cast_to_alias() {
r3 := u8(true)
println(r3)
assert '$r3' == '1'
r4 := u8(char(1))
println(r4)
assert r4 == 1
r5 := u8(char(-1))
println(r5)
assert '$r5' == '255'
r6 := u8(i8(-1))
println(r6)
assert r6 == 255
}