1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#![cfg(any(
not(any(
feature = "parser_tests",
feature = "analyzer_tests",
feature = "codegen_tests",
feature = "interpreter_tests",
feature = "conformance_tests",
feature = "integration_tests",
)),
feature = "analyzer_tests",
))]
/// TDD Test: No .clone() on intermediate Vec when using nested indexing
///
/// Bug: `self.tiles[row][col]` generates `self.tiles[row as usize].clone()[col as usize]`
/// which clones the ENTIRE inner Vec just to access one element.
/// In Rust, `vec[i][j]` auto-derefs through the reference returned by the first index.
///
/// Root Cause: The auto-clone for Vec indexing fired on the inner Index expression
/// because `in_field_access_object` wasn't set when generating the object of an Index.
///
/// Fix: Set `in_field_access_object = true` before generating the object of an Index,
/// same as we do for FieldAccess objects.
#[path = "common/test_utils.rs"]
mod test_utils;
#[test]
fn test_no_clone_on_2d_vec_nested_index() {
// Pattern from windjammer-game world/tilemap.wj:
// self.tiles[row][col] should NOT clone the entire row Vec
let source = r#"
pub struct Grid {
pub cells: Vec<Vec<i32>>,
}
impl Grid {
pub fn get(self, row: i32, col: i32) -> i32 {
self.cells[row][col]
}
}
"#;
let generated = test_utils::compile_single(source);
println!("Generated:\n{}", generated);
// Should NOT clone the inner Vec: cells[row].clone()[col] is WRONG
assert!(
!generated.contains(".clone()["),
"Should not clone intermediate Vec in nested indexing.\nGenerated:\n{}",
generated
);
}
#[test]
fn test_no_clone_on_2d_bool_nested_index() {
// Pattern from windjammer-game pathfinding/grid.wj:
// self.walkable[x][y] — bool is Copy, no clone needed at all
let source = r#"
pub struct PathGrid {
pub walkable: Vec<Vec<bool>>,
}
impl PathGrid {
pub fn is_walkable(self, x: i32, y: i32) -> bool {
self.walkable[x][y]
}
}
"#;
let generated = test_utils::compile_single(source);
println!("Generated:\n{}", generated);
assert!(
!generated.contains(".clone()["),
"Should not clone intermediate Vec for bool (Copy) nested index.\nGenerated:\n{}",
generated
);
assert!(
!generated.contains(".clone()"),
"Bool is Copy — no clone needed at all.\nGenerated:\n{}",
generated
);
}
#[test]
fn test_2d_vec_non_copy_final_element_clone_ok() {
// When the final element is non-Copy and consumed, .clone() on the element IS ok
// But the intermediate Vec should NEVER be cloned
let source = r#"
pub struct Grid {
pub names: Vec<Vec<string>>,
}
impl Grid {
pub fn get_name(self, row: i32, col: i32) -> string {
self.names[row][col]
}
}
"#;
let generated = test_utils::compile_single(source);
println!("Generated:\n{}", generated);
// The final element clone is OK (String is not Copy)
// But the intermediate Vec clone is NOT OK
assert!(
!generated.contains("].clone()["),
"Should not clone intermediate Vec even when final element needs clone.\nGenerated:\n{}",
generated
);
}