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

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

This commit is contained in:
yuyi 2022-05-08 01:20:00 +08:00 committed by GitHub
parent a91226c376
commit 724e7f037a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
}