| 1 | // Regression test for https://github.com/vlang/v/issues/27485 |
| 2 | // Assigning an `if`/`match` expression whose branches are `spawn` calls used to |
| 3 | // emit a C ternary with statement-level spawn setup inside it, which failed to |
| 4 | // compile (and, once that was fixed, produced an unjoinable/detached thread). |
| 5 | |
| 6 | fn ret(x int) int { |
| 7 | return x |
| 8 | } |
| 9 | |
| 10 | fn test_spawn_in_if_expr() { |
| 11 | c := true |
| 12 | t := if c { spawn ret(11) } else { spawn ret(22) } |
| 13 | assert t.wait() == 11 |
| 14 | } |
| 15 | |
| 16 | fn test_spawn_in_if_expr_false_branch() { |
| 17 | c := false |
| 18 | t := if c { spawn ret(11) } else { spawn ret(22) } |
| 19 | assert t.wait() == 22 |
| 20 | } |
| 21 | |
| 22 | fn test_spawn_in_match_expr() { |
| 23 | x := 2 |
| 24 | t := match x { |
| 25 | 1 { spawn ret(1) } |
| 26 | 2 { spawn ret(2) } |
| 27 | else { spawn ret(99) } |
| 28 | } |
| 29 | |
| 30 | assert t.wait() == 2 |
| 31 | } |
| 32 | |
| 33 | fn test_spawn_in_if_expr_collected_into_array() { |
| 34 | mut threads := []thread int{} |
| 35 | for i in 0 .. 4 { |
| 36 | threads << if i % 2 == 0 { spawn ret(i) } else { spawn ret(i * 10) } |
| 37 | } |
| 38 | assert threads.wait() == [0, 10, 2, 30] |
| 39 | } |
| 40 | |