cgen: fix error for struct with reference alias fields (#13374)

pull/13380/head
yuyi 2022-02-06 00:23:54 +08:00 committed by GitHub
parent 47ee292ba4
commit 4d485ae687
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 1 deletions

View File

@ -916,7 +916,8 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
funcprefix += 'isnil(it.${c_name(field.name)})'
funcprefix += ' ? _SLIT("nil") : '
// struct, floats and ints have a special case through the _str function
if sym.kind != .struct_ && !field.typ.is_int_valptr() && !field.typ.is_float_valptr() {
if sym.kind !in [.struct_, .alias] && !field.typ.is_int_valptr()
&& !field.typ.is_float_valptr() {
funcprefix += '*'
}
}

View File

@ -5519,6 +5519,9 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
|| sfield.typ.is_pointer()) && !sfield.typ.is_number() {
g.write('/* autoref */&')
}
if sfield.typ.is_ptr() && field_type_sym.kind == .alias {
g.write('&')
}
g.expr_with_cast(sfield.expr, sfield.typ, sfield.expected_type)
}
}

View File

@ -85,6 +85,9 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
g.expr(expr)
g.write(')')
} else if typ == ast.string_type {
if etype.is_ptr() {
g.write('*')
}
g.expr(expr)
} else if typ == ast.bool_type {
g.expr(expr)

View File

@ -0,0 +1,13 @@
type SS = string
struct ST {
data &SS
}
fn test_struct_with_reference_alias_fields() {
mut val := ST{
data: &SS('hi')
}
println(val.data)
assert '$val.data' == 'hi'
}