cgen: fix codegen for option return unwrapping on last statement (fix #24026) (#24030)

This commit is contained in:
Felipe Pena 2025-03-24 10:44:04 -03:00 committed by GitHub
parent bed827e663
commit 42538e19ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View file

@ -601,6 +601,20 @@ fn (mut p Parser) mark_last_call_return_as_used(mut last_stmt ast.Stmt) {
}
}
}
ast.InfixExpr {
// last stmt has infix expr with CallExpr: foo()? + 'a'
mut left_expr := last_stmt.expr.left
for {
if mut left_expr is ast.InfixExpr {
left_expr = left_expr.left
continue
}
if mut left_expr is ast.CallExpr {
left_expr.is_return_used = true
}
break
}
}
else {}
}
}

View file

@ -0,0 +1,16 @@
fn test_main() {
assert some_func2(14)? + 'c' == 'aabcac'
}
fn some_func(i int) ?string {
return 'a'
}
fn some_func2(i int) ?string {
return match i {
12 { some_func(1)? + 'b' }
13 { some_func(1)? + 'b' + 'c' }
14 { 'a' + some_func(1)? + 'b' + 'c' + some_func(1)? }
else { none }
}
}