1
0
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:
Felipe Pena 2023-04-18 06:40:37 -03:00 committed by GitHub
parent 8445642567
commit df3ee9a64a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -722,11 +722,25 @@ fn (mut g Gen) infix_expr_arithmetic_op(node ast.InfixExpr) {
g.gen_plain_infix_expr(node)
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('(')
g.op_arg(node.left, method.params[0].typ, left.typ)
g.write(', ')
g.op_arg(node.right, method.params[1].typ, right.typ)
if right_var != '' {
g.write(', ${right_var}')
} else {
g.write(', ')
g.op_arg(node.right, method.params[1].typ, right.typ)
}
g.write(')')
}
}

View 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'
}