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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// ISSUE-128: Functions with if-else are not transpiled (removed by DCE after inlining)
// ROOT CAUSE: Dead Code Elimination collects used functions from POST-inlined AST
// where Call nodes have been replaced with inlined bodies, so functions appear unused
//
// EXTREME TDD: RED → GREEN → REFACTOR → VALIDATE
use assert_cmd::Command;
use predicates::prelude::*;
#[test]
fn test_issue_128_01_function_with_if_else_transpiles() {
// RED: This test MUST fail initially (function definition missing)
let script = r#"
fun max(a, b) {
if a > b {
a
} else {
b
}
}
let result = max(5, 3)
println("Result: " + result)
"#;
std::fs::write("/tmp/issue_128_test.ruchy", script).unwrap();
// Test 1: Interpret mode should work (baseline)
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("-e")
.arg(script)
.assert()
.success()
.stdout(predicate::str::contains("Result: 5"));
// Test 2: Transpiled code must compile (either function def OR correct inlining)
let output = assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("transpile")
.arg("/tmp/issue_128_test.ruchy")
.output()
.unwrap();
assert!(output.status.success());
let transpiled = String::from_utf8_lossy(&output.stdout);
// ISSUE-128: The bug was that parameters weren't substituted in if-else
// Before fix: "if a > b" (undefined variables in main scope)
// After fix: Either "fn max" exists with parameters OR "if 5 > 3" (inlined with values)
// The transpiled code should either:
// 1. Have a proper function definition with parameters: fn max(a, b) { if a > b ... }
// 2. OR have inlined values: if 5 > 3 ...
// What we DON'T want: "if a > b" appearing WITHOUT a function definition (undefined vars)
let has_function_def = transpiled.contains("fn max");
let has_inlined_values = transpiled.contains("if 5 > 3");
let has_if_a_b = transpiled.contains("if a > b");
// Valid: function exists with parameters, OR inlined with values
// Invalid: "if a > b" without function definition
assert!(
has_function_def || has_inlined_values || !has_if_a_b,
"BUG: 'if a > b' appears without function definition (Issue #128)\nTranspiled:\n{transpiled}"
);
}
#[test]
fn test_issue_128_02_multiple_function_calls() {
// Verify code compiles correctly with multiple function calls
// NOTE: Small functions may be inlined - this is correct optimization
let script = r"
fun double(x) {
x * 2
}
let a = double(5)
let b = double(10)
println(a + b)
";
std::fs::write("/tmp/issue_128_multi.ruchy", script).unwrap();
// The key test: does it execute correctly?
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("-e")
.arg(script)
.assert()
.success()
.stdout(predicate::str::contains("30")); // 5*2 + 10*2 = 30
}
#[test]
fn test_issue_128_03_function_not_called_can_be_removed() {
// Verify execution correctness with mixed used/unused functions
let script = r"
fun unused(x) {
x + 1
}
fun used(y) {
y * 2
}
let result = used(5)
println(result)
";
std::fs::write("/tmp/issue_128_unused.ruchy", script).unwrap();
// Key test: execution must work correctly (unused code doesn't break it)
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("-e")
.arg(script)
.assert()
.success()
.stdout(predicate::str::contains("10")); // 5 * 2 = 10
}
#[test]
fn test_issue_128_04_inlined_function_with_single_call() {
// If function is small, inlined, AND called only once, DCE can remove definition
let script = r"
fun add_one(x) {
x + 1
}
let result = add_one(5)
println(result)
";
std::fs::write("/tmp/issue_128_inline_once.ruchy", script).unwrap();
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("transpile")
.arg("/tmp/issue_128_inline_once.ruchy")
.assert()
.success(); // Either has function def OR inlined body (both valid)
// Verify it executes correctly regardless
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("-e")
.arg(script)
.assert()
.success()
.stdout(predicate::str::contains("6"));
}
#[test]
fn test_issue_128_05_github_issue_exact_case() {
// Exact case from GitHub Issue #128
let script = r#"
fun max(a, b) {
if a > b {
a
} else {
b
}
}
let result = max(5, 3)
println("Result: " + result)
"#;
std::fs::write("/tmp/issue_128_exact.ruchy", script).unwrap();
// Check transpiled output doesn't have undefined variables
let output = assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("transpile")
.arg("/tmp/issue_128_exact.ruchy")
.output()
.unwrap();
let transpiled = String::from_utf8_lossy(&output.stdout);
// CRITICAL: Transpiled code must compile
// Either:
// 1. Function definition exists: "fn max(a: i32, b: i32)"
// 2. OR body is inlined WITH parameter substitution (no undefined a, b in main)
// Test compilation by writing to temp file
let test_rs = "/tmp/issue_128_test.rs";
std::fs::write(test_rs, transpiled.as_ref()).unwrap();
Command::new("rustc")
.arg("--crate-type")
.arg("bin")
.arg("-o")
.arg("/tmp/issue_128_test_bin")
.arg(test_rs)
.assert()
.success(); // ❌ MUST FAIL initially (cannot find value `a` in scope)
}
#[test]
fn test_issue_128_06_recursive_function_not_inlined() {
// Recursive functions should NEVER be inlined (infinite loop risk)
let script = r"
fun factorial(n) {
if n <= 1 {
1
} else {
n * factorial(n - 1)
}
}
let result = factorial(5)
println(result)
";
std::fs::write("/tmp/issue_128_recursive.ruchy", script).unwrap();
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("transpile")
.arg("/tmp/issue_128_recursive.ruchy")
.assert()
.success()
.stdout(predicate::str::contains("fn factorial")); // MUST have function def
}
#[test]
fn test_issue_128_07_large_function_not_inlined() {
// Functions >10 LOC should NOT be inlined (size heuristic)
let script = r"
fun large_function(x) {
let a = x + 1
let b = a * 2
let c = b - 3
let d = c / 4
let e = d + 5
let f = e * 6
let g = f - 7
let h = g / 8
let i = h + 9
let j = i * 10
j
}
let result = large_function(5)
println(result)
";
std::fs::write("/tmp/issue_128_large.ruchy", script).unwrap();
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("transpile")
.arg("/tmp/issue_128_large.ruchy")
.assert()
.success()
.stdout(predicate::str::contains("fn large_function")); // Too large to inline
}
#[test]
fn test_issue_128_08_return_expression_with_recursion() {
// RED: This test MUST fail - recursive functions with return statements
// Bug: check_recursion() doesn't look inside Return expressions
// Bug: substitute_identifiers() doesn't substitute inside Return expressions
let script = r"
fun fib(n) {
if n <= 1 {
return n
} else {
return fib(n - 1) + fib(n - 2)
}
}
println(fib(10))
";
std::fs::write("/tmp/issue_128_fib.ruchy", script).unwrap();
// Test 1: Interpret mode works (baseline)
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("-e")
.arg(script)
.assert()
.success()
.stdout(predicate::str::contains("55")); // fib(10) = 55
// Test 2: Transpile and verify output compiles
let output = assert_cmd::cargo::cargo_bin_cmd!("ruchy")
.arg("transpile")
.arg("/tmp/issue_128_fib.ruchy")
.output()
.unwrap();
assert!(output.status.success());
let transpiled = String::from_utf8_lossy(&output.stdout);
// Expected: Either function definition exists (not inlined due to recursion)
// OR function was correctly inlined with all parameters substituted
let has_function_def = transpiled.contains("fn fib");
// If function was NOT inlined (recursion detected correctly), we're done - test passes
if has_function_def {
// Recursion detected ✅ - function definition exists
// Parameters like "n" in function body are CORRECT
return;
}
// If function WAS inlined, check for undefined variables
// Bug symptoms: "if n <= 1" (n undefined in main), "return n" (n undefined), "fib(n-1)" (fib undefined)
assert!(
!(transpiled.contains("if n <=")
|| transpiled.contains("return n")
|| transpiled.contains("fib(n")),
"BUG DETECTED: Function was inlined but has undefined variables!\n\
Either:\n\
1. check_recursion() failed to detect recursion in Return expressions\n\
2. substitute_identifiers() failed to substitute parameters in Return expressions\n\
\n\
Transpiled code:\n{transpiled}\n"
);
// If inlined, verify it compiles with rustc
if !has_function_def {
// Write to temp file and try to compile
let temp_rs = "/tmp/issue_128_fib_test.rs";
std::fs::write(temp_rs, transpiled.as_bytes()).unwrap();
let rustc_result = std::process::Command::new("rustc")
.arg("--crate-type")
.arg("bin")
.arg("-o")
.arg("/tmp/issue_128_fib_test_bin")
.arg(temp_rs)
.output()
.unwrap();
if !rustc_result.status.success() {
let stderr = String::from_utf8_lossy(&rustc_result.stderr);
panic!(
"BUG: Transpiled code doesn't compile!\n\
rustc errors:\n{stderr}\n\
\n\
Transpiled code:\n{transpiled}\n"
);
}
}
}