cgen: fix optional_void error; handle `?` return type

pull/4908/head
yuyi 2020-05-15 21:55:03 +08:00 committed by GitHub
parent d73bedc1fb
commit 8500c8885c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View File

@ -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;

View File

@ -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)
}

View File

@ -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
}
}