1
0
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:
yuyi 2021-02-23 15:53:07 +08:00 committed by GitHub
parent 51125541c7
commit 302baaa7b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 2 deletions

View File

@ -3589,14 +3589,15 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
cond_var = g.new_tmp_var() cond_var = g.new_tmp_var()
g.write('${g.typ(node.cond_type)} $cond_var = ') g.write('${g.typ(node.cond_type)} $cond_var = ')
g.expr(node.cond) g.expr(node.cond)
g.writeln('; ') g.writeln(';')
g.stmt_path_pos << g.out.len
g.write(line) g.write(line)
} }
if need_tmp_var { if need_tmp_var {
g.empty_line = true g.empty_line = true
cur_line = g.go_before_stmt(0) cur_line = g.go_before_stmt(0)
tmp_var = g.new_tmp_var() 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 { if is_expr && !need_tmp_var {

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