diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 62e640fe5e5437..52a8a2c85c5125 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -83,6 +83,9 @@ fn (mut g Gen) get_str_fn(typ ast.Type) string { else {} } } + if sym.language == .c && !typ.has_flag(.option) && sym.has_method('str') { + str_fn_name = util.no_dots(g.cc_type(unwrapped, false)) + '_str' + } g.str_types << StrType{ typ: unwrapped styp: styp diff --git a/vlib/v/tests/c_structs/cstruct.h b/vlib/v/tests/c_structs/cstruct.h new file mode 100644 index 00000000000000..3f2f6870f35652 --- /dev/null +++ b/vlib/v/tests/c_structs/cstruct.h @@ -0,0 +1,4 @@ + +struct Abc { + int field; +}; diff --git a/vlib/v/tests/c_structs/cstruct_test.c.v b/vlib/v/tests/c_structs/cstruct_test.c.v new file mode 100644 index 00000000000000..8291342bd08c52 --- /dev/null +++ b/vlib/v/tests/c_structs/cstruct_test.c.v @@ -0,0 +1,17 @@ +#include "@VMODROOT/cstruct.h" + +const the_string = 'the string' + +pub struct C.Abc { + field int +} + +fn (a &C.Abc) str() string { + return 'C.Abc{}' +} + +fn test_cstruct() { + x := unsafe { &C.Abc(1) } + println(x) + assert dump(x.str()) == 'C.Abc{}' +} diff --git a/vlib/v/tests/c_structs/v.mod b/vlib/v/tests/c_structs/v.mod new file mode 100644 index 00000000000000..e69de29bb2d1d6