Skip to content

Commit 42538e1

Browse files
authored
cgen: fix codegen for option return unwrapping on last statement (fix #24026) (#24030)
1 parent bed827e commit 42538e1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

vlib/v/parser/parser.v

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,20 @@ fn (mut p Parser) mark_last_call_return_as_used(mut last_stmt ast.Stmt) {
601601
}
602602
}
603603
}
604+
ast.InfixExpr {
605+
// last stmt has infix expr with CallExpr: foo()? + 'a'
606+
mut left_expr := last_stmt.expr.left
607+
for {
608+
if mut left_expr is ast.InfixExpr {
609+
left_expr = left_expr.left
610+
continue
611+
}
612+
if mut left_expr is ast.CallExpr {
613+
left_expr.is_return_used = true
614+
}
615+
break
616+
}
617+
}
604618
else {}
605619
}
606620
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn test_main() {
2+
assert some_func2(14)? + 'c' == 'aabcac'
3+
}
4+
5+
fn some_func(i int) ?string {
6+
return 'a'
7+
}
8+
9+
fn some_func2(i int) ?string {
10+
return match i {
11+
12 { some_func(1)? + 'b' }
12+
13 { some_func(1)? + 'b' + 'c' }
13+
14 { 'a' + some_func(1)? + 'b' + 'c' + some_func(1)? }
14+
else { none }
15+
}
16+
}

0 commit comments

Comments
 (0)