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:
parent
18b6311b2f
commit
d7501cc9a1
@ -619,7 +619,11 @@ fn (mut g Gen) infix_expr_is_op(node ast.InfixExpr) {
|
|||||||
} else if left_sym.kind == .sum_type {
|
} else if left_sym.kind == .sum_type {
|
||||||
g.write('_typ $cmp_op ')
|
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) {
|
fn (mut g Gen) gen_interface_is_op(node ast.InfixExpr) {
|
||||||
|
3
vlib/v/tests/inout/printing_sumtype_with_none.out
Normal file
3
vlib/v/tests/inout/printing_sumtype_with_none.out
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
first field is a string
|
||||||
|
second field is none
|
||||||
|
third field is None
|
35
vlib/v/tests/inout/printing_sumtype_with_none.vv
Normal file
35
vlib/v/tests/inout/printing_sumtype_with_none.vv
Normal 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'
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user