cgen: fix error for if cond with optional expr (#14334)

yuyi 2022-05-08 01:20:00 +08:00 committed by Jef Roosens
parent 2367b29262
commit bd21ec396c
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 44 additions and 3 deletions

View File

@ -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.writeln(';')
g.stmt_path_pos.delete_last()
} else {
g.stmt(stmt)

View File

@ -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
}