Skip to content

Commit

Permalink
cgen: fix alias to struct ptr on structinit (#18571)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jun 28, 2023
1 parent b2ca3ac commit 83ee282
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion vlib/v/gen/c/auto_str_methods.v
Expand Up @@ -259,7 +259,8 @@ fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string
if str_method_expects_ptr {
g.auto_str_funcs.writeln('\tstring tmp_ds = ${parent_str_fn_name}(&it);')
} else {
g.auto_str_funcs.writeln('\tstring tmp_ds = ${parent_str_fn_name}(it);')
deref, _ := deref_kind(str_method_expects_ptr, info.parent_type.is_ptr(), info.parent_type)
g.auto_str_funcs.writeln('\tstring tmp_ds = ${parent_str_fn_name}(${deref}it);')
}
g.auto_str_funcs.writeln('\tstring res = str_intp(3, _MOV((StrIntpData[]){
{_SLIT0, ${c.si_s_code}, {.d_s = indents }},
Expand Down
6 changes: 5 additions & 1 deletion vlib/v/gen/c/struct.v
Expand Up @@ -22,7 +22,7 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
g.empty_line = false
g.write(s)
}
styp := g.typ(node.typ)
styp := g.typ(g.table.unaliased_type(node.typ)).replace('*', '')
mut shared_styp := '' // only needed for shared x := St{...
if styp in c.skip_struct_init {
// needed for c++ compilers
Expand Down Expand Up @@ -71,6 +71,10 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
g.write('{')
}
} else {
// alias to pointer type
if g.table.sym(node.typ).kind == .alias && g.table.unaliased_type(node.typ).is_ptr() {
g.write('&')
}
if is_multiline {
g.writeln('(${styp}){')
} else {
Expand Down
16 changes: 16 additions & 0 deletions vlib/v/tests/alias_to_ptr_arg_test.v
@@ -0,0 +1,16 @@
struct Foo {
name string
age int
}

type Boo = &Foo

fn foo(f Boo) {
println(f)
dump(f)
}

fn test_main() {
foo(name: '')
assert true
}

0 comments on commit 83ee282

Please sign in to comment.