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

cgen: fix match sumtyp var with none (#15495)

This commit is contained in:
yuyi 2022-08-22 18:31:35 +08:00 committed by GitHub
parent 8db945ec52
commit 18b6311b2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -172,7 +172,11 @@ fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var str
be := unsafe { &branch.exprs[sumtype_index] }
if sym.kind == .sum_type {
g.write('${dot_or_ptr}_typ == ')
g.expr(be)
if be is ast.None {
g.write('$ast.none_type.idx() /* none */')
} else {
g.expr(be)
}
} else if sym.kind == .interface_ {
if be is ast.TypeNode {
typ := be as ast.TypeNode

View File

@ -0,0 +1,13 @@
fn number() int|none {
return none
}
fn test_match_sumtype_var_with_none() {
n := number()
ret := match n {
int { 'n: $n' }
none { '?' }
}
println(ret)
assert ret == '?'
}