mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker, cgen: fix array index optional with if expr (#14575)
This commit is contained in:
parent
846ddfd728
commit
fefb9643b2
@ -3403,6 +3403,9 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
|
||||
typ = value_type
|
||||
}
|
||||
}
|
||||
if node.or_expr.stmts.len > 0 && node.or_expr.stmts.last() is ast.ExprStmt {
|
||||
c.expected_or_type = typ
|
||||
}
|
||||
c.stmts_ending_with_expression(node.or_expr.stmts)
|
||||
c.check_expr_opt_call(node, typ)
|
||||
return typ
|
||||
|
@ -85,7 +85,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
|
||||
g.stmts(branch.stmts)
|
||||
g.expected_cast_type = prev_expected_cast_type
|
||||
}
|
||||
if node.branches.len == 1 {
|
||||
if node.branches.len == 1 && !node.is_expr {
|
||||
g.write(': 0')
|
||||
}
|
||||
g.write(')')
|
||||
|
7
vlib/v/tests/array_index_optional_with_if_expr_test.v
Normal file
7
vlib/v/tests/array_index_optional_with_if_expr_test.v
Normal file
@ -0,0 +1,7 @@
|
||||
fn test_array_index_optional_with_if_expr() {
|
||||
ret := []string{}[0] or {
|
||||
if true { 'a' } else { 'b' }
|
||||
}
|
||||
println(ret)
|
||||
assert ret == 'a'
|
||||
}
|
Loading…
Reference in New Issue
Block a user