Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix codegen for match on return #23851

Merged
merged 3 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions vlib/v/gen/c/match.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import v.util

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 g.inside_struct_init {
return true
}
if g.table.sym(node.return_type).kind in [.sum_type, .multi_return]
|| node.return_type.has_option_or_result() {
return true
Expand Down
29 changes: 29 additions & 0 deletions vlib/v/tests/match_on_return_test.v
Original file line number Diff line number Diff line change
@@ -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
}'
}
Loading