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

checker: fix generic fn casting &u8 to &alias to u32 (#16420)

This commit is contained in:
yuyi 2022-11-14 22:37:53 +08:00 committed by GitHub
parent 37583b04b4
commit 91ecfb917c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -2557,7 +2557,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
} }
} else if mut to_sym.info is ast.Alias && !(final_to_sym.kind == .struct_ && to_type.is_ptr()) { } else if mut to_sym.info is ast.Alias && !(final_to_sym.kind == .struct_ && to_type.is_ptr()) {
if !c.check_types(from_type, to_sym.info.parent_type) && !(final_to_sym.is_int() if !c.check_types(from_type, to_sym.info.parent_type) && !(final_to_sym.is_int()
&& final_from_sym.kind in [.enum_, .bool, .i8, .char]) { && final_from_sym.kind in [.enum_, .bool, .i8, .u8, .char]) {
ft := c.table.type_to_str(from_type) ft := c.table.type_to_str(from_type)
tt := c.table.type_to_str(to_type) tt := c.table.type_to_str(to_type)
c.error('cannot cast `$ft` to `$tt` (alias to `$final_to_sym.name`)', node.pos) c.error('cannot cast `$ft` to `$tt` (alias to `$final_to_sym.name`)', node.pos)

View File

@ -0,0 +1,15 @@
module main
type VkPresentModeKHR = u32
fn create_c_array<T>(len u32) &T {
return unsafe { &T(malloc(int(sizeof(T) * len))) }
}
fn test_generic_fn_cast_to_alias() {
arr_vk := create_c_array<VkPresentModeKHR>(5)
println(typeof(arr_vk).name)
assert typeof(arr_vk).name == '&VkPresentModeKHR'
unsafe { free(arr_vk) }
}