1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix cast enum to alias (#12968)

This commit is contained in:
yuyi 2021-12-26 17:34:20 +08:00 committed by GitHub
parent 10f63b3cd7
commit 03864e4ab8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View File

@ -3483,7 +3483,8 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
c.error('cannot cast `$from_type_sym.name` to `$to_type_sym.name`', node.pos)
}
} else if mut to_type_sym.info is ast.Alias {
if !c.check_types(from_type, to_type_sym.info.parent_type) {
if !c.check_types(from_type, to_type_sym.info.parent_type) && !(to_type_sym_final.is_int()
&& from_type_sym_final.kind == .enum_) {
c.error('cannot convert type `$from_type_sym.name` to `$to_type_sym.name` (alias to `$to_type_sym_final.name`)',
node.pos)
}

View File

@ -0,0 +1,36 @@
module main
type CEnum = int
enum Enum {
value = 1
}
fn foo(n int) string {
return '$n'
}
fn bar(n CEnum) string {
return '$n'
}
fn test_cast_to_alias() {
e := Enum.value
mut ret_str := ''
ret_str = foo(int(e))
println(ret_str)
assert ret_str == '1'
ret_str = bar(int(e))
println(ret_str)
assert ret_str == '1'
ret_str = foo(CEnum(e))
println(ret_str)
assert ret_str == '1'
ret_str = bar(CEnum(e))
println(ret_str)
assert ret_str == '1'
}