Skip to content

Commit e8754af

Browse files
authored
parser: fix last stmt is fn call in or_expr (fix #25732) (#25739)
1 parent cfc3dd0 commit e8754af

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

vlib/v/parser/parser.v

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,9 @@ fn (mut p Parser) mark_last_call_return_as_used(mut last_stmt ast.Stmt) {
530530
ast.CallExpr {
531531
// last stmt on block is CallExpr
532532
last_stmt.expr.is_return_used = true
533-
}
534-
ast.IfExpr {
535-
// last stmt on block is: if .. { foo() } else { bar() }
536-
for mut branch in last_stmt.expr.branches {
537-
if branch.stmts.len > 0 {
538-
mut last_if_stmt := branch.stmts.last()
539-
p.mark_last_call_return_as_used(mut last_if_stmt)
540-
}
533+
if last_stmt.expr.or_block.stmts.len > 0 {
534+
mut or_block_last_stmt := last_stmt.expr.or_block.stmts.last()
535+
p.mark_last_call_return_as_used(mut or_block_last_stmt)
541536
}
542537
}
543538
ast.ConcatExpr {
@@ -548,20 +543,47 @@ fn (mut p Parser) mark_last_call_return_as_used(mut last_stmt ast.Stmt) {
548543
}
549544
}
550545
}
546+
ast.IfExpr {
547+
// last stmt on block is: if .. { foo() } else { bar() }
548+
for mut branch in last_stmt.expr.branches {
549+
if branch.stmts.len > 0 {
550+
mut last_if_stmt := branch.stmts.last()
551+
p.mark_last_call_return_as_used(mut last_if_stmt)
552+
}
553+
}
554+
}
551555
ast.InfixExpr {
556+
if last_stmt.expr.or_block.stmts.len > 0 {
557+
mut or_block_last_stmt := last_stmt.expr.or_block.stmts.last()
558+
p.mark_last_call_return_as_used(mut or_block_last_stmt)
559+
}
552560
// last stmt has infix expr with CallExpr: foo()? + 'a'
553561
mut left_expr := last_stmt.expr.left
554562
for {
555563
if mut left_expr is ast.InfixExpr {
564+
if left_expr.or_block.stmts.len > 0 {
565+
mut or_block_last_stmt := left_expr.or_block.stmts.last()
566+
p.mark_last_call_return_as_used(mut or_block_last_stmt)
567+
}
556568
left_expr = left_expr.left
557569
continue
558570
}
559571
if mut left_expr is ast.CallExpr {
560572
left_expr.is_return_used = true
573+
if left_expr.or_block.stmts.len > 0 {
574+
mut or_block_last_stmt := left_expr.or_block.stmts.last()
575+
p.mark_last_call_return_as_used(mut or_block_last_stmt)
576+
}
561577
}
562578
break
563579
}
564580
}
581+
ast.ComptimeCall, ast.ComptimeSelector, ast.PrefixExpr, ast.SelectorExpr {
582+
if last_stmt.expr.or_block.stmts.len > 0 {
583+
mut or_block_last_stmt := last_stmt.expr.or_block.stmts.last()
584+
p.mark_last_call_return_as_used(mut or_block_last_stmt)
585+
}
586+
}
565587
else {}
566588
}
567589
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module main
2+
3+
fn x1() ?int {
4+
return none
5+
}
6+
7+
fn x2() ?int {
8+
return none
9+
}
10+
11+
fn x3() ?int {
12+
return none
13+
}
14+
15+
fn def() int {
16+
return 123
17+
}
18+
19+
fn test_last_stmt_fn_call_or_expr() {
20+
y := x1() or { x2() or { x3() or { def() } } }
21+
assert y == 123
22+
}

0 commit comments

Comments
 (0)