mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix concat with matchexpr + option string (#17985)
This commit is contained in:
parent
8445642567
commit
df3ee9a64a
@ -722,11 +722,25 @@ fn (mut g Gen) infix_expr_arithmetic_op(node ast.InfixExpr) {
|
|||||||
g.gen_plain_infix_expr(node)
|
g.gen_plain_infix_expr(node)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mut right_var := ''
|
||||||
|
if node.right is ast.Ident && (node.right as ast.Ident).or_expr.kind != .absent {
|
||||||
|
cur_line := g.go_before_stmt(0).trim_space()
|
||||||
|
right_var = g.new_tmp_var()
|
||||||
|
g.write('${g.typ(right.typ)} ${right_var} = ')
|
||||||
|
g.op_arg(node.right, method.params[1].typ, right.typ)
|
||||||
|
g.writeln(';')
|
||||||
|
g.write(cur_line)
|
||||||
|
}
|
||||||
g.write(method_name)
|
g.write(method_name)
|
||||||
g.write('(')
|
g.write('(')
|
||||||
g.op_arg(node.left, method.params[0].typ, left.typ)
|
g.op_arg(node.left, method.params[0].typ, left.typ)
|
||||||
|
if right_var != '' {
|
||||||
|
g.write(', ${right_var}')
|
||||||
|
} else {
|
||||||
g.write(', ')
|
g.write(', ')
|
||||||
g.op_arg(node.right, method.params[1].typ, right.typ)
|
g.op_arg(node.right, method.params[1].typ, right.typ)
|
||||||
|
}
|
||||||
g.write(')')
|
g.write(')')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
vlib/v/tests/option_concat_str_test.v
Normal file
15
vlib/v/tests/option_concat_str_test.v
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
fn test_str_concat() {
|
||||||
|
opt := ?string(none)
|
||||||
|
x := match 1 {
|
||||||
|
0 { 'abc' }
|
||||||
|
else { 'def' }
|
||||||
|
} + opt or { '!!!' }
|
||||||
|
assert x == 'def!!!'
|
||||||
|
|
||||||
|
y := opt or { '!!!' } + match 1 {
|
||||||
|
0 { 'abc' }
|
||||||
|
else { 'def' }
|
||||||
|
}
|
||||||
|
println(y)
|
||||||
|
assert y == '!!!def'
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user