From e8a1e9d88f4fc5854dd89247ddbcd01b733b40cf Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 23 Jun 2021 20:14:08 +0800 Subject: [PATCH] cgen: fix interface only declare with optional method (#10546) --- vlib/v/gen/c/cgen.v | 6 ++++++ vlib/v/tests/interface_only_decl_with_optional_test.v | 8 ++++++++ 2 files changed, 14 insertions(+) create mode 100644 vlib/v/tests/interface_only_decl_with_optional_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index fc75a1846c..dac72172a1 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1332,6 +1332,12 @@ fn (mut g Gen) stmt(node ast.Stmt) { ast.Import {} ast.InterfaceDecl { // definitions are sorted and added in write_types + for method in node.methods { + if method.return_type.has_flag(.optional) { + // Register an optional if it's not registered yet + g.register_optional(method.return_type) + } + } } ast.Module { // g.is_builtin_mod = node.name == 'builtin' diff --git a/vlib/v/tests/interface_only_decl_with_optional_test.v b/vlib/v/tests/interface_only_decl_with_optional_test.v new file mode 100644 index 0000000000..48dce1a521 --- /dev/null +++ b/vlib/v/tests/interface_only_decl_with_optional_test.v @@ -0,0 +1,8 @@ +interface Message { + serialize() ?[]byte +} + +fn test_interface_only_decl_with_optional() { + println('test interface') + assert true +}