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

checker: fix fn returning alias of pointer (fix #17861) (#17864)

This commit is contained in:
yuyi 2023-04-04 00:32:55 +08:00 committed by GitHub
parent 1dcec62c19
commit 4f532c0830
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -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() {

View File

@ -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 }
}