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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#![allow(missing_docs)]
// Issue #40: Mutable variable increment inside match expression
// Bug: Variable mutations inside match arms don't propagate to outer scope
//
// Five Whys Analysis:
// 1. Why does loop print "Character 0: a" infinitely?
// - Because `i` is never incremented from 0
// 2. Why is `i` never incremented?
// - Because `i = i + 1` inside match arm doesn't update outer scope's `i`
// 3. Why doesn't assignment update outer scope?
// - Because match creates new scope with push_scope() that shadows outer variables
// 4. Why does new scope shadow instead of mutate?
// - Because pattern bindings use env_set() which creates new bindings, not mutations
// 5. Why doesn't mutation propagate?
// - Because pop_scope() discards all changes made in match arm scope
//
// ROOT CAUSE: Match expression creates isolated scope that doesn't propagate mutations
// to outer scope variables. Need to track which variables are mutations vs new bindings.
use assert_cmd::Command;
use predicates::prelude::*;
fn ruchy_cmd() -> Command {
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
}
/// RED TEST: Issue #40 - Mutable variable in match expression (original bug report)
#[test]
fn test_issue_040_mutable_in_match_string_iteration() {
let code = r#"
let s = "abc".to_string();
let mut i = 0;
loop {
if i >= s.len() {
break;
}
match s.chars().nth(i) {
Some(ch) => {
println("Character " + i.to_string() + ": " + ch.to_string());
i = i + 1;
}
None => break
}
}
println("Done");
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.timeout(std::time::Duration::from_secs(5))
.assert()
.success()
.stdout(predicate::str::contains("Character 0: a"))
.stdout(predicate::str::contains("Character 1: b"))
.stdout(predicate::str::contains("Character 2: c"))
.stdout(predicate::str::contains("Done"));
}
/// RED TEST: Simple mutable increment in match
#[test]
#[ignore = "BUG: Mutable match patterns not working"]
fn test_issue_040_simple_mutable_increment() {
let code = r"
let mut x = 0;
match 1 {
1 => x = x + 1
_ => {}
}
println(x)
";
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("1\nnil\n");
}
/// RED TEST: Multiple mutations in match
#[test]
#[ignore = "BUG: Mutable match patterns not working"]
fn test_issue_040_multiple_mutations() {
let code = r#"
let mut x = 0;
let mut y = 0;
match 1 {
1 => {
x = x + 1;
y = y + 2;
}
_ => {}
}
println(x.to_string() + "," + y.to_string())
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("1,2\nnil\n");
}
/// RED TEST: Mutation in nested match
#[test]
#[ignore = "BUG: Mutable match patterns not working"]
fn test_issue_040_nested_match_mutation() {
let code = r"
let mut counter = 0;
match 1 {
1 => {
match 2 {
2 => counter = counter + 1
_ => {}
}
}
_ => {}
}
println(counter)
";
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("1\nnil\n");
}
/// RED TEST: Mutation with pattern binding (ensure pattern vars don't interfere)
#[test]
#[ignore = "BUG: Mutable match patterns not working"]
fn test_issue_040_mutation_with_pattern_binding() {
let code = r"
let mut sum = 0;
match Some(5) {
Some(n) => {
sum = sum + n;
}
None => {}
}
println(sum)
";
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("5\nnil\n");
}
/// RED TEST: Mutation in loop with match (comprehensive)
#[test]
#[ignore = "BUG: Mutable match patterns not working"]
fn test_issue_040_loop_with_match_mutation() {
let code = r"
let mut i = 0;
let mut sum = 0;
loop {
if i >= 5 {
break;
}
match i {
0 => sum = sum + 1
1 => sum = sum + 2
2 => sum = sum + 3
3 => sum = sum + 4
4 => sum = sum + 5
_ => {}
}
i = i + 1;
}
println(sum)
";
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("15\nnil\n"); // 1+2+3+4+5 = 15
}
/// RED TEST: Guard condition doesn't prevent mutation
#[test]
#[ignore = "BUG: Mutable match patterns not working"]
fn test_issue_040_mutation_with_guard() {
let code = r"
let mut x = 0;
let value = 5;
match value {
n if n > 0 => x = x + n
_ => {}
}
println(x)
";
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("5\nnil\n");
}
#[cfg(test)]
mod property_tests {
use std::process::Command;
/// Property test: Counter increments correctly N times in match
#[test]
#[ignore = "Run with: cargo test property_tests -- --ignored"]
fn proptest_counter_increment_in_match() {
for n in 0..100 {
let code = format!(
r"
let mut counter = 0;
let mut i = 0;
loop {{
if i >= {n} {{
break;
}}
match i {{
_ => counter = counter + 1
}}
i = i + 1;
}}
println(counter)
"
);
let output = Command::new("ruchy")
.arg("-e")
.arg(&code)
.output()
.expect("Failed to run ruchy");
let stdout = String::from_utf8_lossy(&output.stdout);
let result: i32 = stdout
.lines()
.next()
.and_then(|s| s.parse().ok())
.unwrap_or(-1);
assert_eq!(
result, n,
"Counter should increment {n} times, got {result}"
);
}
}
/// Property test: Mutation persists across match arms
#[test]
#[ignore = "Property test - run with --ignored"]
fn proptest_mutation_persists_across_arms() {
for initial in 0..50 {
for increment in 1..20 {
let code = format!(
r"
let mut x = {initial};
match {increment} {{
n => x = x + n
}}
println(x)
"
);
let output = Command::new("ruchy")
.arg("-e")
.arg(&code)
.output()
.expect("Failed to run ruchy");
let stdout = String::from_utf8_lossy(&output.stdout);
let result: i32 = stdout
.lines()
.next()
.and_then(|s| s.parse().ok())
.unwrap_or(-1);
assert_eq!(
result,
initial + increment,
"Mutation failed: {} + {} should equal {}",
initial,
increment,
initial + increment
);
}
}
}
}