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

checker, cgen: fix type alias of pointer (#17904)

This commit is contained in:
yuyi 2023-04-07 16:19:25 +08:00 committed by GitHub
parent eb6dd82d72
commit 237f9446e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 6 deletions

View File

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

View File

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

View File

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