cgen: fix c struct option alias printing (#21496)

This commit is contained in:
Felipe Pena 2024-05-13 13:23:58 -03:00 committed by GitHub
parent c23c543b67
commit 331ccac3e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 2 deletions

View file

@ -179,7 +179,7 @@ fn (mut g Gen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string)
}
parent_type := typ.clear_flag(.option)
sym := g.table.sym(parent_type)
sym_has_str_method, _, _ := sym.str_method_info()
sym_has_str_method, expects_ptr, _ := sym.str_method_info()
parent_str_fn_name := g.get_str_fn(parent_type)
g.definitions.writeln('string ${str_fn_name}(${styp} it); // auto')
@ -188,7 +188,13 @@ fn (mut g Gen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string)
g.auto_str_funcs.writeln('string indent_${str_fn_name}(${styp} it, int indent_count) {')
g.auto_str_funcs.writeln('\tstring res;')
g.auto_str_funcs.writeln('\tif (it.state == 0) {')
deref := if typ.is_ptr() { '**(${sym.cname}**)&' } else { '*(${sym.cname}*)' }
deref := if typ.is_ptr() {
'**(${sym.cname}**)&'
} else if expects_ptr {
'(${sym.cname}*)'
} else {
'*(${sym.cname}*)'
}
if sym.kind == .string {
if typ.nr_muls() > 1 {
g.auto_str_funcs.writeln('\t\tres = ptr_str(*(${sym.cname}**)&it.data);')

View file

@ -1117,6 +1117,9 @@ fn (mut g Gen) gen_to_str_method_call(node ast.CallExpr) bool {
}
g.gen_expr_to_string(left_node, rec_type)
return true
} else if left_node.or_expr.kind == .propagate_option {
g.gen_expr_to_string(left_node, g.unwrap_generic(node.left_type))
return true
}
}
} else if left_node is ast.None {

View file

@ -26,3 +26,9 @@ typedef struct Foo {
typedef struct Bar {
int a;
} Bar;
///
typedef struct TestAlias {
int a;
};

View file

@ -0,0 +1,15 @@
#include "@VMODROOT/cstruct.h"
struct C.TestAlias {
}
type Foo = C.TestAlias
fn call() ?Foo {
return Foo{}
}
fn test_main() {
a := call()
assert a?.str() == 'Foo(C.TestAlias{})'
}