From 237f9446e265cf8ac25571f4fb5762e0f23247f1 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 7 Apr 2023 16:19:25 +0800 Subject: [PATCH] checker, cgen: fix type alias of pointer (#17904) --- vlib/v/checker/return.v | 7 ++++--- vlib/v/gen/c/fn.v | 7 ++++--- .../tests/type_alias_of_pointer_types_test.v | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/vlib/v/checker/return.v b/vlib/v/checker/return.v index f3544cddec..43d9221b65 100644 --- a/vlib/v/checker/return.v +++ b/vlib/v/checker/return.v @@ -219,8 +219,9 @@ fn (mut c Checker) return_stmt(mut node ast.Return) { pos) } } - if (got_typ.is_ptr() || got_typ.is_pointer()) - && (!exp_type.is_ptr() && !exp_type.is_pointer()) { + unaliased_exp_typ := c.table.unaliased_type(exp_type) + if got_typ.is_real_pointer() && !exp_type.is_real_pointer() + && !unaliased_exp_typ.is_real_pointer() { pos := node.exprs[expr_idxs[i]].pos() if node.exprs[expr_idxs[i]].is_auto_deref_var() { continue @@ -230,7 +231,7 @@ fn (mut c Checker) return_stmt(mut node ast.Return) { pos) } unaliased_got_typ := c.table.unaliased_type(got_typ) - if (exp_type.is_ptr() || exp_type.is_pointer()) && !got_typ.is_real_pointer() + if exp_type.is_real_pointer() && !got_typ.is_real_pointer() && !unaliased_got_typ.is_real_pointer() && got_typ != ast.int_literal_type && !c.pref.translated && !c.file.is_translated { pos := node.exprs[expr_idxs[i]].pos() diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 18ab03183a..9175ddc256 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -2351,8 +2351,8 @@ fn (mut g Gen) keep_alive_call_postgen(node ast.CallExpr, tmp_cnt_save int) { fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang ast.Language) { arg_typ := g.unwrap_generic(arg.typ) arg_sym := g.table.sym(arg_typ) - exp_is_ptr := expected_type.is_ptr() || expected_type.idx() in ast.pointer_type_idxs - arg_is_ptr := arg_typ.is_ptr() || arg_typ.idx() in ast.pointer_type_idxs + exp_is_ptr := expected_type.is_real_pointer() + arg_is_ptr := arg_typ.is_real_pointer() if expected_type == 0 { g.checker_bug('ref_or_deref_arg expected_type is 0', arg.pos) } @@ -2360,7 +2360,8 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as mut needs_closing := false if arg.is_mut && !exp_is_ptr { g.write('&/*mut*/') - } else if exp_is_ptr && !arg_is_ptr { + } else if exp_is_ptr && !arg_is_ptr && !(arg_sym.kind == .alias + && g.table.unaliased_type(arg_typ).is_pointer() && expected_type.is_pointer()) { if arg.is_mut { if exp_sym.kind == .array { if (arg.expr is ast.Ident && (arg.expr as ast.Ident).kind == .variable) diff --git a/vlib/v/tests/type_alias_of_pointer_types_test.v b/vlib/v/tests/type_alias_of_pointer_types_test.v index 1dcabd9af0..8761e247d7 100644 --- a/vlib/v/tests/type_alias_of_pointer_types_test.v +++ b/vlib/v/tests/type_alias_of_pointer_types_test.v @@ -72,3 +72,24 @@ fn mut_alias(mut ps PZZMyStructInt) int { // dump(ptr_str(voidptr(ps))) return 123 } + +type HANDLE = voidptr + +fn example(arg voidptr) { + println('Handle value in function is: ${arg}') + assert '${arg}' == '0' +} + +fn test_alias_of_pointer() { + handle := get_handle() + + println('Actual handle value is: ${handle}') + println('Memory address of handle is: ${&handle}') + + example(handle) + assert '${handle}' == '0' +} + +fn get_handle() HANDLE { + return unsafe { nil } +}