Skip to content

Commit 55436ca

Browse files
authored
cgen: fix nested or in assign decl (fix #25864) (#25865)
1 parent 3caa1b7 commit 55436ca

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

vlib/v/gen/c/assign.v

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,24 @@ fn (mut g Gen) expr_with_opt_or_block(expr ast.Expr, expr_typ ast.Type, var_expr
3737
or_expr := (expr as ast.Ident).or_expr
3838
stmts := or_expr.stmts
3939
scope := or_expr.scope
40+
last_stmt := stmts.last()
4041
// handles stmt block which returns something
4142
// e.g. { return none }
42-
if stmts.len > 0 && stmts.last() is ast.ExprStmt && stmts.last().typ != ast.void_type {
43-
g.gen_or_block_stmts(c_name(var_expr.str()), '', stmts, ret_typ, false,
44-
scope, expr.pos())
43+
if stmts.len > 0 && last_stmt is ast.ExprStmt && last_stmt.typ != ast.void_type {
44+
var_expr_name := c_name(var_expr.str())
45+
if last_stmt.expr is ast.Ident && last_stmt.expr.or_expr.kind != .absent {
46+
g.write('${var_expr_name} = ')
47+
g.expr_with_opt_or_block(last_stmt.expr, last_stmt.typ, var_expr,
48+
ret_typ, in_heap)
49+
} else {
50+
g.gen_or_block_stmts(var_expr_name, '', stmts, ret_typ, false, scope,
51+
expr.pos())
52+
}
4553
} else {
4654
// handles stmt block which doesn't returns value
4755
// e.g. { return }
4856
g.stmts(stmts)
49-
if stmts.len > 0 && stmts.last() is ast.ExprStmt {
57+
if stmts.len > 0 && last_stmt is ast.ExprStmt {
5058
g.writeln(';')
5159
}
5260
g.write_defer_stmts(scope, false, expr.pos())
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module main
2+
3+
fn fx1() ?int {
4+
return none
5+
}
6+
7+
fn fx2() ?int {
8+
return none
9+
}
10+
11+
fn fx3() ?int {
12+
return none
13+
}
14+
15+
fn test_nested_or_in_assign_decl() {
16+
x1 := fx1()
17+
x2 := fx2()
18+
x3 := fx3()
19+
def := 123
20+
21+
y := x1 or { x2 or { x3 or { def } } }
22+
assert y == 123
23+
}

0 commit comments

Comments
 (0)