Skip to content

Commit dbe9be1

Browse files
authored
cgen: fix cast type changed before last stmt in stmts_with_tmp_var (#25915)
1 parent f108077 commit dbe9be1

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,9 +2149,11 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
21492149
if g.inside_ternary > 0 {
21502150
g.write('(')
21512151
}
2152+
expected_cast_type := g.expected_cast_type
21522153
mut last_stmt_was_return := false
21532154
for i, stmt in stmts {
21542155
if i == stmts.len - 1 {
2156+
g.expected_cast_type = expected_cast_type
21552157
if stmt is ast.Return {
21562158
last_stmt_was_return = true
21572159
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct Foo {
2+
}
3+
4+
struct Bar {
5+
b bool
6+
}
7+
8+
type FooBar = Foo | Bar
9+
10+
fn get_foobar(f FooBar) FooBar {
11+
return match f {
12+
Foo {
13+
Foo{}
14+
}
15+
Bar {
16+
b := match f.b {
17+
true { false }
18+
false { true }
19+
}
20+
Bar{
21+
b: b
22+
}
23+
}
24+
}
25+
}
26+
27+
fn test_return_match_expr_with_nest_match_expr() {
28+
assert get_foobar(Bar{}) == FooBar(Bar{
29+
b: true
30+
})
31+
}

0 commit comments

Comments
 (0)