From 91ecfb917c2562fe35f6c3254bcc1926d9677425 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 14 Nov 2022 22:37:53 +0800 Subject: [PATCH] checker: fix generic fn casting &u8 to &alias to u32 (#16420) --- vlib/v/checker/checker.v | 2 +- vlib/v/tests/generic_fn_cast_to_alias_test.v | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generic_fn_cast_to_alias_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 93af147677..21a19c46c2 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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()) { 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) tt := c.table.type_to_str(to_type) c.error('cannot cast `$ft` to `$tt` (alias to `$final_to_sym.name`)', node.pos) diff --git a/vlib/v/tests/generic_fn_cast_to_alias_test.v b/vlib/v/tests/generic_fn_cast_to_alias_test.v new file mode 100644 index 0000000000..af3db1e529 --- /dev/null +++ b/vlib/v/tests/generic_fn_cast_to_alias_test.v @@ -0,0 +1,15 @@ +module main + +type VkPresentModeKHR = u32 + +fn create_c_array(len u32) &T { + return unsafe { &T(malloc(int(sizeof(T) * len))) } +} + +fn test_generic_fn_cast_to_alias() { + arr_vk := create_c_array(5) + + println(typeof(arr_vk).name) + assert typeof(arr_vk).name == '&VkPresentModeKHR' + unsafe { free(arr_vk) } +}