Skip to content

Commit 331ccac

Browse files
authored
cgen: fix c struct option alias printing (#21496)
1 parent c23c543 commit 331ccac

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

vlib/v/gen/c/auto_str_methods.v

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ fn (mut g Gen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string)
179179
}
180180
parent_type := typ.clear_flag(.option)
181181
sym := g.table.sym(parent_type)
182-
sym_has_str_method, _, _ := sym.str_method_info()
182+
sym_has_str_method, expects_ptr, _ := sym.str_method_info()
183183
parent_str_fn_name := g.get_str_fn(parent_type)
184184

185185
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)
188188
g.auto_str_funcs.writeln('string indent_${str_fn_name}(${styp} it, int indent_count) {')
189189
g.auto_str_funcs.writeln('\tstring res;')
190190
g.auto_str_funcs.writeln('\tif (it.state == 0) {')
191-
deref := if typ.is_ptr() { '**(${sym.cname}**)&' } else { '*(${sym.cname}*)' }
191+
deref := if typ.is_ptr() {
192+
'**(${sym.cname}**)&'
193+
} else if expects_ptr {
194+
'(${sym.cname}*)'
195+
} else {
196+
'*(${sym.cname}*)'
197+
}
192198
if sym.kind == .string {
193199
if typ.nr_muls() > 1 {
194200
g.auto_str_funcs.writeln('\t\tres = ptr_str(*(${sym.cname}**)&it.data);')

vlib/v/gen/c/fn.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,9 @@ fn (mut g Gen) gen_to_str_method_call(node ast.CallExpr) bool {
11171117
}
11181118
g.gen_expr_to_string(left_node, rec_type)
11191119
return true
1120+
} else if left_node.or_expr.kind == .propagate_option {
1121+
g.gen_expr_to_string(left_node, g.unwrap_generic(node.left_type))
1122+
return true
11201123
}
11211124
}
11221125
} else if left_node is ast.None {

vlib/v/tests/c_structs/cstruct.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ typedef struct Foo {
2626
typedef struct Bar {
2727
int a;
2828
} Bar;
29+
30+
///
31+
32+
typedef struct TestAlias {
33+
int a;
34+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "@VMODROOT/cstruct.h"
2+
3+
struct C.TestAlias {
4+
}
5+
6+
type Foo = C.TestAlias
7+
8+
fn call() ?Foo {
9+
return Foo{}
10+
}
11+
12+
fn test_main() {
13+
a := call()
14+
assert a?.str() == 'Foo(C.TestAlias{})'
15+
}

0 commit comments

Comments
 (0)