From 8e9ddb13eae9cf258284cfde3ad928350d157942 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 17 Mar 2024 22:30:36 -0300 Subject: [PATCH] cgen: fix _str name generated for C struct which define str() method (#21042) --- vlib/v/gen/c/auto_str_methods.v | 3 +++ vlib/v/tests/c_structs/cstruct.h | 4 ++++ vlib/v/tests/c_structs/cstruct_test.c.v | 17 +++++++++++++++++ vlib/v/tests/c_structs/v.mod | 0 4 files changed, 24 insertions(+) create mode 100644 vlib/v/tests/c_structs/cstruct.h create mode 100644 vlib/v/tests/c_structs/cstruct_test.c.v create mode 100644 vlib/v/tests/c_structs/v.mod diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 62e640fe5..52a8a2c85 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 000000000..3f2f6870f --- /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 000000000..8291342bd --- /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 000000000..e69de29bb -- 2.39.5