From 8500c8885c898bc0177229a969ae846cefe9f105 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 15 May 2020 21:55:03 +0800 Subject: [PATCH] cgen: fix optional_void error; handle `?` return type --- vlib/v/gen/cgen.v | 3 ++- vlib/v/parser/parse_type.v | 5 ++++- vlib/v/tests/option_test.v | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index a422475262..c5538a7f4e 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -323,8 +323,9 @@ fn (g &Gen) base_type(t table.Type) string { fn (mut g Gen) register_optional(t table.Type, styp string) { // g.typedefs2.writeln('typedef Option $x;') no_ptr := styp.replace('*', '_ptr') + typ := if styp == 'void' { 'void*' } else { styp } g.hotcode_definitions.writeln('typedef struct { - $styp data; + $typ data; string error; int ecode; bool ok; diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 58687ec1c9..f4e3cf43f7 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -123,7 +123,10 @@ pub fn (mut p Parser) parse_type() table.Type { p.next() p.check(.dot) } - mut typ := p.parse_any_type(is_c, is_js, nr_muls > 0) + mut typ := table.void_type + if p.tok.kind != .lcbr { + typ = p.parse_any_type(is_c, is_js, nr_muls > 0) + } if is_optional { typ = typ.set_flag(.optional) } diff --git a/vlib/v/tests/option_test.v b/vlib/v/tests/option_test.v index 976aecc3d7..d1e0b5ffd1 100644 --- a/vlib/v/tests/option_test.v +++ b/vlib/v/tests/option_test.v @@ -207,3 +207,28 @@ fn test_multi_return_opt() { } } */ + +fn foo() ?void { + return error('something') +} + +fn test_optional_void() { + foo() or { + println(err) + assert err == 'something' + return + } +} + + +fn bar() ? { + return error('bar error') +} + +fn test_optional_void_only_question() { + bar() or { + println(err) + assert err == 'bar error' + return + } +}