Skip to content

Commit df3ee9a

Browse files
authored
cgen: fix concat with matchexpr + option string (#17985)
1 parent 8445642 commit df3ee9a

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

vlib/v/gen/c/infix.v

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,25 @@ fn (mut g Gen) infix_expr_arithmetic_op(node ast.InfixExpr) {
722722
g.gen_plain_infix_expr(node)
723723
return
724724
}
725+
726+
mut right_var := ''
727+
if node.right is ast.Ident && (node.right as ast.Ident).or_expr.kind != .absent {
728+
cur_line := g.go_before_stmt(0).trim_space()
729+
right_var = g.new_tmp_var()
730+
g.write('${g.typ(right.typ)} ${right_var} = ')
731+
g.op_arg(node.right, method.params[1].typ, right.typ)
732+
g.writeln(';')
733+
g.write(cur_line)
734+
}
725735
g.write(method_name)
726736
g.write('(')
727737
g.op_arg(node.left, method.params[0].typ, left.typ)
728-
g.write(', ')
729-
g.op_arg(node.right, method.params[1].typ, right.typ)
738+
if right_var != '' {
739+
g.write(', ${right_var}')
740+
} else {
741+
g.write(', ')
742+
g.op_arg(node.right, method.params[1].typ, right.typ)
743+
}
730744
g.write(')')
731745
}
732746
}

vlib/v/tests/option_concat_str_test.v

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fn test_str_concat() {
2+
opt := ?string(none)
3+
x := match 1 {
4+
0 { 'abc' }
5+
else { 'def' }
6+
} + opt or { '!!!' }
7+
assert x == 'def!!!'
8+
9+
y := opt or { '!!!' } + match 1 {
10+
0 { 'abc' }
11+
else { 'def' }
12+
}
13+
println(y)
14+
assert y == '!!!def'
15+
}

0 commit comments

Comments
 (0)