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

cgen: fix alias to struct ptr on structinit (#18571)

This commit is contained in:
Felipe Pena 2023-06-28 01:02:32 -03:00 committed by GitHub
parent b2ca3ac089
commit 83ee2827d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 2 deletions

View File

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

View File

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

View File

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