cgen: fix codegen for match on return (fix #23661) (#23851)

This commit is contained in:
Felipe Pena 2025-03-03 13:52:45 -03:00 committed by GitHub
parent f4b51d064e
commit 2641a55a63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 0 deletions

View file

@ -8,6 +8,9 @@ import v.util
fn (mut g Gen) need_tmp_var_in_match(node ast.MatchExpr) bool { fn (mut g Gen) need_tmp_var_in_match(node ast.MatchExpr) bool {
if node.is_expr && node.return_type != ast.void_type && node.return_type != 0 { if node.is_expr && node.return_type != ast.void_type && node.return_type != 0 {
if g.inside_struct_init {
return true
}
if g.table.sym(node.return_type).kind in [.sum_type, .multi_return] if g.table.sym(node.return_type).kind in [.sum_type, .multi_return]
|| node.return_type.has_option_or_result() { || node.return_type.has_option_or_result() {
return true return true

View file

@ -0,0 +1,29 @@
struct Bauds {
standard bool
bauds u32
}
fn max(v u32) !u32 {
if v > 38400 {
return error('too fast')
}
return v
}
fn new_bauds(bauds u32) !&Bauds {
return &Bauds{
standard: match bauds {
300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400 { true }
else { false }
}
bauds: max(bauds)!
}
}
fn test_main() {
t := new_bauds(310)!
assert '${t}' == '&Bauds{
standard: false
bauds: 310
}'
}