| 1 | import os |
| 2 | |
| 3 | const fixed_array_force_vexe = @VEXE |
| 4 | const fixed_array_force_tests_dir = os.dir(@FILE) |
| 5 | const fixed_array_force_v3_dir = os.dir(fixed_array_force_tests_dir) |
| 6 | const fixed_array_force_vlib_dir = os.dir(fixed_array_force_v3_dir) |
| 7 | const fixed_array_force_v3_src = os.join_path(fixed_array_force_v3_dir, 'v3.v') |
| 8 | |
| 9 | fn fixed_array_force_build_v3() string { |
| 10 | v3_bin := os.join_path(os.temp_dir(), 'v3_fixed_array_force_test_${os.getpid()}') |
| 11 | os.rm(v3_bin) or {} |
| 12 | build := |
| 13 | os.execute('${fixed_array_force_vexe} -gc none -path "${fixed_array_force_vlib_dir}|@vlib|@vmodules" -o ${v3_bin} ${fixed_array_force_v3_src}') |
| 14 | assert build.exit_code == 0, build.output |
| 15 | return v3_bin |
| 16 | } |
| 17 | |
| 18 | fn fixed_array_force_write_project() string { |
| 19 | root := os.join_path(os.temp_dir(), 'v3_fixed_array_force_${os.getpid()}') |
| 20 | os.rmdir_all(root) or {} |
| 21 | fixture_dir := os.join_path(root, 'fixture') |
| 22 | os.mkdir_all(fixture_dir) or { panic(err) } |
| 23 | os.write_file(os.join_path(fixture_dir, 'pass_action.h'), '#ifndef V3_FIXED_ARRAY_FORCE_PASS_ACTION_H |
| 24 | #define V3_FIXED_ARRAY_FORCE_PASS_ACTION_H |
| 25 | struct pass_action { |
| 26 | int colors[4]; |
| 27 | }; |
| 28 | #endif |
| 29 | ') or { |
| 30 | panic(err) |
| 31 | } |
| 32 | os.write_file(os.join_path(fixture_dir, 'fixture.v'), 'module fixture |
| 33 | |
| 34 | #include "@DIR/pass_action.h" |
| 35 | |
| 36 | pub struct Color { |
| 37 | pub: |
| 38 | r int |
| 39 | } |
| 40 | |
| 41 | pub struct DirectPass { |
| 42 | pub: |
| 43 | colors [4]Color |
| 44 | } |
| 45 | |
| 46 | pub struct C.pass_action { |
| 47 | pub: |
| 48 | colors [4]int |
| 49 | } |
| 50 | |
| 51 | pub type Pass = C.pass_action |
| 52 | |
| 53 | pub const direct_pass = DirectPass{ |
| 54 | colors: [ |
| 55 | Color{r: 1}, |
| 56 | Color{r: 2}, |
| 57 | Color{r: 3}, |
| 58 | Color{r: 4}, |
| 59 | ]! |
| 60 | } |
| 61 | |
| 62 | pub const imported_pass = Pass{ |
| 63 | colors: [ |
| 64 | 5, |
| 65 | 6, |
| 66 | 7, |
| 67 | 8, |
| 68 | ]! |
| 69 | } |
| 70 | |
| 71 | pub fn pass_score() int { |
| 72 | return direct_pass.colors[0].r + direct_pass.colors[1].r + direct_pass.colors[2].r + direct_pass.colors[3].r + |
| 73 | imported_pass.colors[0] + imported_pass.colors[1] + imported_pass.colors[2] + imported_pass.colors[3] |
| 74 | } |
| 75 | ') or { |
| 76 | panic(err) |
| 77 | } |
| 78 | os.write_file(os.join_path(root, 'main.v'), 'module main |
| 79 | |
| 80 | import fixture |
| 81 | |
| 82 | fn next_function_marker() int { |
| 83 | return 6 |
| 84 | } |
| 85 | |
| 86 | fn main() { |
| 87 | println(int_str(fixture.pass_score() + next_function_marker())) |
| 88 | } |
| 89 | ') or { |
| 90 | panic(err) |
| 91 | } |
| 92 | return root |
| 93 | } |
| 94 | |
| 95 | fn test_fixed_array_force_array_literal_const_field_does_not_leak_array_temp() { |
| 96 | v3_bin := fixed_array_force_build_v3() |
| 97 | root := fixed_array_force_write_project() |
| 98 | bin := os.join_path(root, 'out') |
| 99 | compile := os.execute('${v3_bin} ${root} -b c -o ${bin}') |
| 100 | assert compile.exit_code == 0, compile.output |
| 101 | run := os.execute(bin) |
| 102 | assert run.exit_code == 0, run.output |
| 103 | assert run.output.trim_space() == '42' |
| 104 | generated := os.read_file(bin + '.c') or { panic(err) } |
| 105 | assert !generated.contains('__arr_lit_'), generated |
| 106 | assert !generated.contains('Array fixture____arr_lit'), generated |
| 107 | assert generated.contains('fixture__imported_pass'), generated |
| 108 | assert generated.contains('next_function_marker'), generated |
| 109 | } |
| 110 | |