Skip to content

Commit cb79e57

Browse files
authored
cgen: fix generated code for match bar()?.a { (matchexpr with call expr using propagation) (#17150)
1 parent 9a86456 commit cb79e57

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

vlib/v/gen/c/match.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
6767
}
6868
if node.cond in [ast.Ident, ast.IntegerLiteral, ast.StringLiteral, ast.FloatLiteral]
6969
|| (node.cond is ast.SelectorExpr
70-
&& (node.cond as ast.SelectorExpr).or_block.kind == .absent) {
70+
&& (node.cond as ast.SelectorExpr).or_block.kind == .absent
71+
&& ((node.cond as ast.SelectorExpr).expr !is ast.CallExpr
72+
|| ((node.cond as ast.SelectorExpr).expr as ast.CallExpr).or_block.kind == .absent)) {
7173
cond_var = g.expr_string(node.cond)
7274
} else {
7375
line := if is_expr {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
struct Foo {
2+
pub:
3+
a int
4+
}
5+
6+
fn foo() !Foo {
7+
return Foo{1}
8+
}
9+
10+
fn bar() ?Foo {
11+
return Foo{2}
12+
}
13+
14+
fn test_main() {
15+
match foo()!.a {
16+
1 {
17+
assert true
18+
}
19+
else {
20+
assert false
21+
}
22+
}
23+
24+
match bar()?.a {
25+
2 {
26+
assert true
27+
}
28+
else {
29+
assert false
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)