| 1 | @[params] |
| 2 | struct Data { |
| 3 | array []int |
| 4 | } |
| 5 | |
| 6 | fn (d Data) len() int { |
| 7 | return d.array.len |
| 8 | } |
| 9 | |
| 10 | fn make_result() []Data { |
| 11 | return [] |
| 12 | } |
| 13 | |
| 14 | // make_result2 is here to ensure that |
| 15 | // the branches contain different code, |
| 16 | // so the tests passes even with -cstrict -cc gcc |
| 17 | fn make_result2() []Data { |
| 18 | return [] |
| 19 | } |
| 20 | |
| 21 | fn f_doesnotcompile(d Data) []Data { |
| 22 | return match d.len() { |
| 23 | 1 { make_result() } |
| 24 | else { make_result2() } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn f_compiles1(d Data) []Data { |
| 29 | return match d.array.len { |
| 30 | 1 { make_result() } |
| 31 | else { make_result2() } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn f_compiles2(d Data) []Data { |
| 36 | length := d.array.len |
| 37 | return match length { |
| 38 | 1 { make_result() } |
| 39 | else { make_result2() } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn test_match_in_fn_call() { |
| 44 | println(f_doesnotcompile()) |
| 45 | assert f_doesnotcompile() == []Data{} |
| 46 | println(f_compiles1()) |
| 47 | assert f_compiles1() == []Data{} |
| 48 | println(f_compiles2()) |
| 49 | assert f_compiles2() == []Data{} |
| 50 | } |
| 51 | |