From 4f532c0830d05faaee01e5cb71777f3693ddf9a8 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 4 Apr 2023 00:32:55 +0800 Subject: [PATCH] checker: fix fn returning alias of pointer (fix #17861) (#17864) --- vlib/v/checker/return.v | 5 +++-- vlib/v/tests/fn_return_alias_of_ptr_test.v | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/fn_return_alias_of_ptr_test.v diff --git a/vlib/v/checker/return.v b/vlib/v/checker/return.v index 21ccbd1d4c..f3544cddec 100644 --- a/vlib/v/checker/return.v +++ b/vlib/v/checker/return.v @@ -229,8 +229,9 @@ fn (mut c Checker) return_stmt(mut node ast.Return) { c.error('fn `${c.table.cur_fn.name}` expects you to return a non reference type `${c.table.type_to_str(exp_type)}`, but you are returning `${c.table.type_to_str(got_typ)}` instead', pos) } - if (exp_type.is_ptr() || exp_type.is_pointer()) - && (!got_typ.is_ptr() && !got_typ.is_pointer()) && got_typ != ast.int_literal_type + unaliased_got_typ := c.table.unaliased_type(got_typ) + if (exp_type.is_ptr() || exp_type.is_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() if node.exprs[expr_idxs[i]].is_auto_deref_var() { diff --git a/vlib/v/tests/fn_return_alias_of_ptr_test.v b/vlib/v/tests/fn_return_alias_of_ptr_test.v new file mode 100644 index 0000000000..0516cc785f --- /dev/null +++ b/vlib/v/tests/fn_return_alias_of_ptr_test.v @@ -0,0 +1,16 @@ +type HANDLE = voidptr + +struct ExampleStruct { + handle HANDLE +} + +fn get_ptr(arg ExampleStruct) voidptr { + return arg.handle +} + +fn test_fn_return_alias_of_ptr() { + h := ExampleStruct{} + r := get_ptr(h) + println(r) + assert r == unsafe { nil } +}