Skip to content

Commit 2641a55

Browse files
authored
cgen: fix codegen for match on return (fix #23661) (#23851)
1 parent f4b51d0 commit 2641a55

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

vlib/v/gen/c/match.v

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import v.util
88

99
fn (mut g Gen) need_tmp_var_in_match(node ast.MatchExpr) bool {
1010
if node.is_expr && node.return_type != ast.void_type && node.return_type != 0 {
11+
if g.inside_struct_init {
12+
return true
13+
}
1114
if g.table.sym(node.return_type).kind in [.sum_type, .multi_return]
1215
|| node.return_type.has_option_or_result() {
1316
return true

vlib/v/tests/match_on_return_test.v

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
struct Bauds {
2+
standard bool
3+
bauds u32
4+
}
5+
6+
fn max(v u32) !u32 {
7+
if v > 38400 {
8+
return error('too fast')
9+
}
10+
return v
11+
}
12+
13+
fn new_bauds(bauds u32) !&Bauds {
14+
return &Bauds{
15+
standard: match bauds {
16+
300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400 { true }
17+
else { false }
18+
}
19+
bauds: max(bauds)!
20+
}
21+
}
22+
23+
fn test_main() {
24+
t := new_bauds(310)!
25+
assert '${t}' == '&Bauds{
26+
standard: false
27+
bauds: 310
28+
}'
29+
}

0 commit comments

Comments
 (0)