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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#![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",
))]
/// Tests for `let` immutability-by-default semantics
///
/// **Current `wj` behavior (CI truth):** The compiler still infers `let mut` when a binding is
/// mutated (push, compound assignment, etc.). A future pass may emit Windjammer-native immutability
/// errors instead; until then, these tests assert successful builds and, where useful, that output
/// contains `let mut` for mutated locals.
///
/// Intended philosophy (Rust/Swift-style explicit `let mut` at the source level) is not fully
/// enforced in the driver yet.
#[path = "common/test_utils.rs"]
mod test_utils;
// ============================================================================
// TEST 1: `let` generates `let` (no mut) in Rust output
//
// A bare `let` binding that is never mutated should generate plain `let` in Rust.
// ============================================================================
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_let_generates_immutable_binding() {
let source = r#"
fn main() {
let x = 5
let y = x + 10
println("{}", y)
}
"#;
let (generated, _stderr) = test_utils::compile_via_cli_with_stderr(source);
// Should generate `let x = 5` (not `let mut x`)
assert!(
generated.contains("let x =") || generated.contains("let x:"),
"Expected immutable `let x`, got:\n{}",
generated
);
assert!(
!generated.contains("let mut x"),
"Should NOT have `let mut x` for immutable binding, got:\n{}",
generated
);
}
// ============================================================================
// TEST 2: `let mut` generates `let mut` in Rust output
//
// An explicit `let mut` binding should generate `let mut` in Rust.
// ============================================================================
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_let_mut_generates_mutable_binding() {
let source = r#"
fn main() {
let mut count = 0
count = count + 1
println("{}", count)
}
"#;
let (generated, _stderr) = test_utils::compile_via_cli_with_stderr(source);
// Should generate `let mut count`
assert!(
generated.contains("let mut count"),
"Expected `let mut count`, got:\n{}",
generated
);
}
// ============================================================================
// TEST 3: mutating a `let` binding without `let mut` — current compiler infers `let mut`
//
// When native immutability diagnostics land, this test should expect failure; for now wj build
// succeeds and codegen includes `let mut items`.
// ============================================================================
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_let_does_not_auto_infer_mut() {
let source = r#"
fn main() {
let items: Vec<int> = Vec::new()
items.push(42)
}
"#;
let (generated, _stderr) = test_utils::compile_via_cli_with_stderr(source);
assert!(
generated.contains("let mut items"),
"Current codegen should infer `let mut` for mutated Vec binding. Got:\n{}",
generated
);
}
// ============================================================================
// TEST 4: `let mut` with Vec operations works correctly
//
// When the user explicitly writes `let mut`, everything works as before.
// ============================================================================
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_let_mut_with_vec_push() {
let source = r#"
fn main() {
let mut items: Vec<int> = Vec::new()
items.push(42)
items.push(100)
println("{}", items.len())
}
"#;
let (generated, _stderr) = test_utils::compile_via_cli_with_stderr(source);
// Should generate `let mut items`
assert!(
generated.contains("let mut items"),
"Expected `let mut items` for explicit mut binding, got:\n{}",
generated
);
}
// ============================================================================
// TEST 5: compound assignment — current compiler infers `let mut count`
// ============================================================================
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_let_compound_assignment_no_auto_mut() {
let source = r#"
fn main() {
let count = 0
count += 1
}
"#;
let (generated, _stderr) = test_utils::compile_via_cli_with_stderr(source);
assert!(
generated.contains("let mut count"),
"Current codegen should infer `let mut` for compound assignment. Got:\n{}",
generated
);
}
// ============================================================================
// TEST 6: `let mut` with compound assignment works
// ============================================================================
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_let_mut_compound_assignment() {
let source = r#"
fn main() {
let mut count = 0
count += 1
println("{}", count)
}
"#;
let (generated, _stderr) = test_utils::compile_via_cli_with_stderr(source);
assert!(
generated.contains("let mut count"),
"Expected `let mut count`, got:\n{}",
generated
);
}
// ============================================================================
// TEST 7: `let` and `let mut` coexist in same function
// ============================================================================
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_let_and_let_mut_coexist() {
let source = r#"
fn main() {
let name = "hello"
let mut count = 0
count += 1
println("{} {}", name, count)
}
"#;
let (generated, _stderr) = test_utils::compile_via_cli_with_stderr(source);
// name should be immutable
assert!(
!generated.contains("let mut name"),
"Expected immutable `let name`, got:\n{}",
generated
);
// count should be mutable
assert!(
generated.contains("let mut count"),
"Expected `let mut count`, got:\n{}",
generated
);
}
// ============================================================================
// TEST 8: Parameter mutability inference is UNCHANGED
//
// The auto-mut for parameters (ownership inference) should still work.
// Only local `let` bindings lose auto-mut.
// ============================================================================
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_parameter_mut_inference_unchanged() {
let source = r#"
struct Counter {
value: int
}
impl Counter {
fn increment(self) {
self.value += 1
}
}
fn main() {
let mut c = Counter { value: 0 }
c.increment()
}
"#;
let (generated, _stderr) = test_utils::compile_via_cli_with_stderr(source);
// The `self` parameter should still be auto-inferred as `&mut self`
assert!(
generated.contains("&mut self"),
"Parameter ownership inference should still work. Got:\n{}",
generated
);
}