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:
parent
e56e4b3e90
commit
5b8d6c0a60
@ -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 {}
|
||||
}
|
||||
|
@ -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()
|
||||
|
30
vlib/v/tests/option_match_expr_test.v
Normal file
30
vlib/v/tests/option_match_expr_test.v
Normal 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'
|
||||
}
|
Loading…
Reference in New Issue
Block a user