1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix match tmp var needing check when working with option on branches (#17837)

This commit is contained in:
Felipe Pena 2023-04-01 02:19:23 -03:00 committed by GitHub
parent e56e4b3e90
commit 5b8d6c0a60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 2 deletions

View File

@ -37,6 +37,16 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
return true
}
match expr {
ast.Ident {
return expr.or_expr.kind != .absent
}
ast.StringInterLiteral {
for e in expr.exprs {
if g.need_tmp_var_in_expr(e) {
return true
}
}
}
ast.IfExpr {
if g.need_tmp_var_in_if(expr) {
return true
@ -134,7 +144,10 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
}
}
ast.SelectorExpr {
return g.need_tmp_var_in_expr(expr.expr)
if g.need_tmp_var_in_expr(expr.expr) {
return true
}
return expr.or_block.kind != .absent
}
else {}
}

View File

@ -81,7 +81,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
''
}
cond_var = g.new_tmp_var()
g.write('${g.typ(node.cond_type)} ${cond_var} = ')
g.write('${g.typ(node.cond_type)} /*A*/ ${cond_var} = ')
g.expr(node.cond)
g.writeln(';')
g.set_current_pos_as_last_stmt_pos()

View File

@ -0,0 +1,30 @@
pub struct ParamPrecos {
pub:
code string [required]
table_ref ?i64
}
fn test_none() {
pp := ParamPrecos{
code: 'V'
}
tx_ref := match pp.table_ref {
none { 'num: none' }
else { 'num: ${pp.table_ref?}' }
}
assert tx_ref == 'num: none'
}
fn test_not_none() {
pp := ParamPrecos{
code: 'V'
table_ref: 123
}
tx_ref := match pp.table_ref {
none { 'num: none' }
else { 'num: ${pp.table_ref?}' }
}
assert tx_ref == 'num: 123'
}