1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix sumtype with none (#14965)

This commit is contained in:
yuyi 2022-07-07 02:03:42 +08:00 committed by GitHub
parent d86b4951c7
commit f4b39fbe4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -2078,7 +2078,11 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr
g.write('&')
}
}
g.expr(expr)
if got_styp == 'none' && !g.cur_fn.return_type.has_flag(.optional) {
g.write('(none){EMPTY_STRUCT_INITIALIZATION}')
} else {
g.expr(expr)
}
g.write(')'.repeat(rparen_n))
}

View File

@ -0,0 +1,18 @@
module main
fn string_none() string | none {
return none
}
fn test_sumtype_with_none() {
x := string_none()
res := match x {
string {
false
}
none {
true
}
}
assert res
}