From 6bf2ad1ff072f97e55b271f1b148a73a96cb50fb Mon Sep 17 00:00:00 2001 From: shove Date: Fri, 14 Oct 2022 15:34:42 +0800 Subject: [PATCH] cgen: fix missing * of optional non-ref structs (fix: #16070) (#16071) --- vlib/v/gen/c/cgen.v | 4 ++-- vlib/v/tests/option_test.v | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index e123c69d5f..e4859fbc1f 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1036,7 +1036,7 @@ fn (g Gen) optional_type_text(styp string, base string) string { } else if base.starts_with('anon_fn') { 'void*' } else { - base + if base.starts_with('struct ') && !base.ends_with('*') { '$base*' } else { base } } ret := 'struct $styp { byte state; @@ -1053,7 +1053,7 @@ fn (g Gen) result_type_text(styp string, base string) string { } else if base.starts_with('anon_fn') { 'void*' } else { - base + if base.starts_with('struct ') && !base.ends_with('*') { '$base*' } else { base } } ret := 'struct $styp { bool is_error; diff --git a/vlib/v/tests/option_test.v b/vlib/v/tests/option_test.v index 3100e307d5..2d12a4743a 100644 --- a/vlib/v/tests/option_test.v +++ b/vlib/v/tests/option_test.v @@ -390,6 +390,15 @@ fn test_optional_ref_c_struct_gen() { _ := get_opt_pointer_to_c_struct() or { &C.stat{} } } +// For issue #16070: cgen error: missing * of optional non-ref structs +fn get_opt_to_c_struct() ?C.stat { + return none +} + +fn test_optional_c_struct_gen() { + _ := get_opt_to_c_struct() or { C.stat{} } +} + // For issue #16062: checker disallow the return of voidptr(nil) in or block struct Bar {}