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

cgen: fix if sumtype var is none (#15496)

This commit is contained in:
yuyi 2022-08-22 18:32:27 +08:00 committed by GitHub
parent 18b6311b2f
commit d7501cc9a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 1 deletions

View File

@ -619,7 +619,11 @@ fn (mut g Gen) infix_expr_is_op(node ast.InfixExpr) {
} else if left_sym.kind == .sum_type {
g.write('_typ $cmp_op ')
}
g.expr(node.right)
if node.right is ast.None {
g.write('$ast.none_type.idx() /* none */')
} else {
g.expr(node.right)
}
}
fn (mut g Gen) gen_interface_is_op(node ast.InfixExpr) {

View File

@ -0,0 +1,3 @@
first field is a string
second field is none
third field is None

View File

@ -0,0 +1,35 @@
module main
struct None {}
struct MyStruct {
my_first_field string|none
my_second_field string|none
my_third_field string|None
}
fn main() {
s := MyStruct{
my_first_field: 'blah'
my_second_field: none
my_third_field: None{}
}
println(if s.my_first_field is string {
'first field is a string'
} else {
'first field is none'
})
println(if s.my_second_field is none {
'second field is none'
} else {
'second field is a string'
})
println(if s.my_third_field is None {
'third field is None'
} else {
'third field is a string'
})
}