mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix error of match in fn_call (#8900)
This commit is contained in:
parent
51125541c7
commit
302baaa7b4
@ -3589,14 +3589,15 @@ 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.expr(node.cond)
|
||||
g.writeln('; ')
|
||||
g.writeln(';')
|
||||
g.stmt_path_pos << g.out.len
|
||||
g.write(line)
|
||||
}
|
||||
if need_tmp_var {
|
||||
g.empty_line = true
|
||||
cur_line = g.go_before_stmt(0)
|
||||
tmp_var = g.new_tmp_var()
|
||||
g.writeln('\t${g.typ(node.return_type)} $tmp_var;')
|
||||
g.writeln('${g.typ(node.return_type)} $tmp_var;')
|
||||
}
|
||||
|
||||
if is_expr && !need_tmp_var {
|
||||
|
42
vlib/v/tests/match_in_fn_call_test.v
Normal file
42
vlib/v/tests/match_in_fn_call_test.v
Normal file
@ -0,0 +1,42 @@
|
||||
struct Data {
|
||||
array []int
|
||||
}
|
||||
|
||||
fn (d Data) len() int {
|
||||
return d.array.len
|
||||
}
|
||||
|
||||
fn make_result() []Data {
|
||||
return []
|
||||
}
|
||||
|
||||
fn f_doesnotcompile(d Data) []Data {
|
||||
return match d.len() {
|
||||
1 { make_result() }
|
||||
else { make_result() }
|
||||
}
|
||||
}
|
||||
|
||||
fn f_compiles1(d Data) []Data {
|
||||
return match d.array.len {
|
||||
1 { make_result() }
|
||||
else { make_result() }
|
||||
}
|
||||
}
|
||||
|
||||
fn f_compiles2(d Data) []Data {
|
||||
length := d.array.len
|
||||
return match length {
|
||||
1 { make_result() }
|
||||
else { make_result() }
|
||||
}
|
||||
}
|
||||
|
||||
fn test_match_in_fn_call() {
|
||||
println(f_doesnotcompile({}))
|
||||
assert f_doesnotcompile({}) == []Data{}
|
||||
println(f_compiles1({}))
|
||||
assert f_compiles1({}) == []Data{}
|
||||
println(f_compiles2({}))
|
||||
assert f_compiles2({}) == []Data{}
|
||||
}
|
Loading…
Reference in New Issue
Block a user