Skip to content

Commit 4f532c0

Browse files
authored
checker: fix fn returning alias of pointer (fix #17861) (#17864)
1 parent 1dcec62 commit 4f532c0

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

vlib/v/checker/return.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
229229
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',
230230
pos)
231231
}
232-
if (exp_type.is_ptr() || exp_type.is_pointer())
233-
&& (!got_typ.is_ptr() && !got_typ.is_pointer()) && got_typ != ast.int_literal_type
232+
unaliased_got_typ := c.table.unaliased_type(got_typ)
233+
if (exp_type.is_ptr() || exp_type.is_pointer()) && !got_typ.is_real_pointer()
234+
&& !unaliased_got_typ.is_real_pointer() && got_typ != ast.int_literal_type
234235
&& !c.pref.translated && !c.file.is_translated {
235236
pos := node.exprs[expr_idxs[i]].pos()
236237
if node.exprs[expr_idxs[i]].is_auto_deref_var() {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type HANDLE = voidptr
2+
3+
struct ExampleStruct {
4+
handle HANDLE
5+
}
6+
7+
fn get_ptr(arg ExampleStruct) voidptr {
8+
return arg.handle
9+
}
10+
11+
fn test_fn_return_alias_of_ptr() {
12+
h := ExampleStruct{}
13+
r := get_ptr(h)
14+
println(r)
15+
assert r == unsafe { nil }
16+
}

0 commit comments

Comments
 (0)