cgen: fix error for if cond with optional expr (#14334)
parent
a91226c376
commit
724e7f037a
|
@ -5133,9 +5133,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty
|
|||
g.inside_opt_data = true
|
||||
g.expr_with_cast(expr_stmt.expr, expr_stmt.typ, return_type.clear_flag(.optional))
|
||||
g.inside_opt_data = old_inside_opt_data
|
||||
if g.inside_ternary == 0 {
|
||||
g.writeln(';')
|
||||
}
|
||||
g.stmt_path_pos.delete_last()
|
||||
} else {
|
||||
g.stmt(stmt)
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
module main
|
||||
|
||||
import rand
|
||||
|
||||
interface Sample {
|
||||
mut:
|
||||
get_next() int
|
||||
}
|
||||
|
||||
struct SampleA {
|
||||
mut:
|
||||
state int
|
||||
}
|
||||
|
||||
fn (mut sample SampleA) get_next() int {
|
||||
sample.state++
|
||||
return sample.state
|
||||
}
|
||||
|
||||
struct SampleB {
|
||||
mut:
|
||||
state int = 1
|
||||
}
|
||||
|
||||
fn (mut sample SampleB) get_next() int {
|
||||
sample.state += 2
|
||||
return sample.state
|
||||
}
|
||||
|
||||
fn create_sampler() Sample {
|
||||
return if rand.intn(1) or { 0 } == 0 { Sample(SampleA{}) } else { Sample(SampleB{}) }
|
||||
}
|
||||
|
||||
fn test_if_cond_with_optional() {
|
||||
mut sample := create_sampler()
|
||||
mut ret := sample.get_next()
|
||||
println(ret)
|
||||
assert ret == 1
|
||||
|
||||
ret = sample.get_next()
|
||||
println(ret)
|
||||
assert ret == 2
|
||||
}
|
Loading…
Reference in New Issue