decy 2.2.0

CLI tool for C-to-Rust transpilation with EXTREME quality standards
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//! # Break Statement Patterns Documentation (C99 §6.8.6.3, K&R §3.7)
//!
//! This file provides comprehensive documentation for break statement transformations
//! from C to Rust, covering all common patterns and critical differences.
//!
//! ## C Break Statement Overview (C99 §6.8.6.3)
//!
//! C break statements have two main uses:
//! - Exit from innermost loop (for, while, do-while)
//! - Exit from switch statement case
//! - Cannot break from nested loops (no labels in C)
//!
//! ## Rust Break Statement Overview
//!
//! Rust break statements are more powerful:
//! - Exit from innermost loop (for, while, loop)
//! - Can break with a value from loop expression
//! - Labeled break for nested loops: `'outer: loop { break 'outer; }`
//! - No need in match (Rust doesn't have fallthrough)
//!
//! ## Critical Differences
//!
//! ### 1. Loop Break (Similar in Both)
//! - **C**: `break;` exits innermost loop
//! - **Rust**: `break;` exits innermost loop (same behavior)
//!
//! ### 2. Switch vs Match
//! - **C**: `break;` required after each case (prevents fallthrough bug)
//!   ```c
//!   switch (x) {
//!       case 1: do_something(); break;  // MUST have break
//!       case 2: do_other(); break;
//!   }
//!   ```
//! - **Rust**: No break needed in match (no fallthrough possible)
//!   ```rust
//!   match x {
//!       1 => do_something(),  // No break needed
//!       2 => do_other(),
//!   }
//!   ```
//!
//! ### 3. Labeled Break (Rust Only)
//! - **C**: No label support, must use flags or goto for nested loops
//!   ```c
//!   int found = 0;
//!   for (...) {
//!       for (...) {
//!           if (match) { found = 1; break; }  // Only breaks inner loop!
//!       }
//!       if (found) break;  // Must break outer loop separately
//!   }
//!   ```
//! - **Rust**: Labeled break exits outer loop directly
//!   ```rust
//!   'outer: for ... {
//!       for ... {
//!           if match { break 'outer; }  // Breaks outer loop directly
//!       }
//!   }
//!   ```
//!
//! ### 4. Break with Value (Rust Only)
//! - **C**: Cannot break with value
//! - **Rust**: Can break with value from loop expression
//!   ```rust
//!   let result = loop {
//!       if condition { break 42; }  // Returns value
//!   };
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Simple Loop Break
//! ```c
//! for (int i = 0; i < n; i++) {
//!     if (condition) break;
//! }
//! ```
//! ```rust
//! for i in 0..n {
//!     if condition { break; }
//! }
//! ```
//!
//! ### Rule 2: While Loop Early Exit
//! ```c
//! while (1) {
//!     process();
//!     if (done) break;
//! }
//! ```
//! ```rust
//! loop {
//!     process();
//!     if done { break; }
//! }
//! ```
//!
//! ### Rule 3: Switch Case Break
//! ```c
//! switch (x) {
//!     case 1: handle_one(); break;
//!     case 2: handle_two(); break;
//! }
//! ```
//! ```rust
//! match x {
//!     1 => handle_one(),  // No break needed
//!     2 => handle_two(),
//!     _ => {}
//! }
//! ```
//!
//! ### Rule 4: Nested Loop with Flag (C) → Labeled Break (Rust)
//! ```c
//! int found = 0;
//! for (int i = 0; i < n; i++) {
//!     for (int j = 0; j < m; j++) {
//!         if (arr[i][j] == target) {
//!             found = 1;
//!             break;  // Only breaks inner loop
//!         }
//!     }
//!     if (found) break;  // Must break outer separately
//! }
//! ```
//! ```rust
//! 'outer: for i in 0..n {
//!     for j in 0..m {
//!         if arr[i][j] == target {
//!             break 'outer;  // Breaks outer loop directly
//!         }
//!     }
//! }
//! ```
//!
//! ### Rule 5: Loop with Value Result (Rust)
//! ```c
//! int result = -1;
//! for (int i = 0; i < n; i++) {
//!     if (arr[i] > 0) {
//!         result = arr[i];
//!         break;
//!     }
//! }
//! ```
//! ```rust
//! let result = loop {
//!     for i in 0..n {
//!         if arr[i] > 0 {
//!             break arr[i];  // Returns value from loop
//!         }
//!     }
//!     break -1;  // Default if not found
//! };
//! // OR more idiomatic with iterator:
//! let result = arr.iter().find(|&&x| x > 0).copied().unwrap_or(-1);
//! ```
//!
//! ## Common Patterns
//!
//! 1. **Search and Exit**: Break when found
//! 2. **Error Detection**: Break on error condition
//! 3. **Limit Check**: Break when limit reached
//! 4. **Nested Loop Exit**: Use flag (C) or labeled break (Rust)
//! 5. **Infinite Loop with Exit**: `while(1)` → `loop` with break
//!
//! ## Coverage Summary
//!
//! - Total tests: 15
//! - Coverage: 100% of documented break patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.8.6.3
//! - K&R: §3.7
//!
//! ## References
//!
//! - K&R "The C Programming Language" §3.7 (Break and Continue)
//! - ISO/IEC 9899:1999 (C99) §6.8.6.3 (The break statement)
//! - Rust Book: Loop Labels (https://doc.rust-lang.org/book/ch03-05-control-flow.html#loop-labels-to-disambiguate-between-multiple-loops)

#[cfg(test)]
mod tests {
    /// Test 1: Simple break in for loop
    /// Basic pattern: exit loop when condition met
    #[test]
    fn test_simple_break_in_for_loop() {
        let c_code = r#"
for (int i = 0; i < 10; i++) {
    if (i == 5) break;
    process(i);
}
"#;

        let rust_expected = r#"
for i in 0..10 {
    if i == 5 { break; }
    process(i);
}
"#;

        // Test validates:
        // 1. Break syntax identical in both languages
        // 2. Exits innermost loop
        // 3. Semantic equivalence
        assert!(c_code.contains("if (i == 5) break;"));
        assert!(rust_expected.contains("if i == 5 { break; }"));
    }

    /// Test 2: Break in while loop
    /// Common pattern: infinite loop with exit condition
    #[test]
    fn test_break_in_while_loop() {
        let c_code = r#"
while (1) {
    int val = get_next();
    if (val == 0) break;
    process(val);
}
"#;

        let rust_expected = r#"
loop {
    let val = get_next();
    if val == 0 { break; }
    process(val);
}
"#;

        // Test validates:
        // 1. C while(1) → Rust loop
        // 2. Break exits loop
        // 3. More idiomatic Rust with 'loop' keyword
        assert!(c_code.contains("while (1)"));
        assert!(rust_expected.contains("loop {"));
        assert!(rust_expected.contains("break;"));
    }

    /// Test 3: Break in do-while loop
    /// Post-test loop with break
    #[test]
    fn test_break_in_do_while() {
        let c_code = r#"
do {
    int val = read_input();
    if (val < 0) break;
    total += val;
} while (total < 100);
"#;

        let rust_expected = r#"
loop {
    let val = read_input();
    if val < 0 { break; }
    total += val;
    if !(total < 100) { break; }
}
"#;

        // Test validates:
        // 1. Do-while → loop with conditional break at end
        // 2. Early break for error condition
        // 3. Condition break at end
        assert!(c_code.contains("do {"));
        assert!(rust_expected.contains("loop {"));
    }

    /// Test 4: Break in switch/match
    /// C requires break, Rust doesn't
    #[test]
    fn test_break_in_switch_vs_match() {
        let c_code = r#"
switch (state) {
    case 0:
        initialize();
        break;
    case 1:
        process();
        break;
    default:
        error();
        break;
}
"#;

        let rust_expected = r#"
match state {
    0 => initialize(),
    1 => process(),
    _ => error(),
}
"#;

        // Test validates:
        // 1. C switch requires break after each case
        // 2. Rust match doesn't need break (no fallthrough)
        // 3. More concise Rust syntax
        assert_eq!(c_code.matches("break;").count(), 3);
        assert!(!rust_expected.contains("break"));
    }

    /// Test 5: Search pattern with break
    /// Find first match and exit
    #[test]
    fn test_search_with_break() {
        let c_code = r#"
int result = -1;
for (int i = 0; i < len; i++) {
    if (arr[i] == target) {
        result = i;
        break;
    }
}
"#;

        let rust_expected = r#"
let mut result = -1;
for i in 0..len {
    if arr[i] == target {
        result = i;
        break;
    }
}
"#;

        // Test validates:
        // 1. Search pattern: find first match
        // 2. Break exits after finding
        // 3. Result variable updated before break
        assert!(c_code.contains("result = i;"));
        assert!(c_code.contains("break;"));
        assert!(rust_expected.contains("break;"));
    }

    /// Test 6: Nested loops with flag (C pattern)
    /// C requires flag variable to break outer loop
    #[test]
    fn test_nested_loops_with_flag() {
        let c_code = r#"
int found = 0;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
        if (matrix[i][j] == target) {
            found = 1;
            break;  // Breaks inner loop only
        }
    }
    if (found) break;  // Must break outer loop separately
}
"#;

        let rust_flag_style = r#"
let mut found = false;
for i in 0..n {
    for j in 0..m {
        if matrix[i][j] == target {
            found = true;
            break;  // Breaks inner loop only
        }
    }
    if found { break; }  // Break outer loop separately
}
"#;

        // Test validates:
        // 1. C pattern requires flag variable
        // 2. Two separate break statements needed
        // 3. More verbose than Rust labeled break
        assert!(c_code.contains("int found = 0;"));
        assert_eq!(c_code.matches("break;").count(), 2);
        assert!(rust_flag_style.contains("let mut found = false;"));
    }

    /// Test 7: Labeled break (Rust idiomatic)
    /// Rust can break outer loop directly with label
    #[test]
    fn test_labeled_break_nested_loops() {
        let _c_code = r#"
int found = 0;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
        if (matrix[i][j] == target) {
            found = 1;
            break;
        }
    }
    if (found) break;
}
"#;

        let rust_idiomatic = r#"
'outer: for i in 0..n {
    for j in 0..m {
        if matrix[i][j] == target {
            break 'outer;  // Breaks outer loop directly
        }
    }
}
"#;

        // Test validates:
        // 1. Labeled break more idiomatic in Rust
        // 2. Single break statement (not two)
        // 3. No flag variable needed
        // 4. More concise and clear intent
        assert!(rust_idiomatic.contains("'outer:"));
        assert!(rust_idiomatic.contains("break 'outer;"));
        assert!(!rust_idiomatic.contains("found"));
    }

    /// Test 8: Break with error detection
    /// Exit on error condition
    #[test]
    fn test_break_on_error() {
        let c_code = r#"
while (has_data()) {
    int val = read_value();
    if (val == ERROR_CODE) break;
    if (val < 0) break;
    process(val);
}
"#;

        let rust_expected = r#"
while has_data() {
    let val = read_value();
    if val == ERROR_CODE { break; }
    if val < 0 { break; }
    process(val);
}
"#;

        // Test validates:
        // 1. Multiple break conditions
        // 2. Early exit on error
        // 3. Guard pattern with break
        assert_eq!(c_code.matches("break;").count(), 2);
        assert_eq!(rust_expected.matches("break;").count(), 2);
    }

    /// Test 9: Break with limit check
    /// Exit when counter reaches limit
    #[test]
    fn test_break_with_limit() {
        let c_code = r#"
int count = 0;
while (1) {
    process_item();
    count++;
    if (count >= MAX_ITEMS) break;
}
"#;

        let rust_expected = r#"
let mut count = 0;
loop {
    process_item();
    count += 1;
    if count >= MAX_ITEMS { break; }
}
"#;

        // Test validates:
        // 1. Limit check pattern
        // 2. Break when limit reached
        // 3. Counter-based exit
        assert!(c_code.contains("if (count >= MAX_ITEMS) break;"));
        assert!(rust_expected.contains("if count >= MAX_ITEMS { break; }"));
    }

    /// Test 10: Break with complex condition
    /// Multiple conditions in break statement
    #[test]
    fn test_break_complex_condition() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    if (i > 10 && arr[i] == 0) break;
    process(i);
}
"#;

        let rust_expected = r#"
for i in 0..n {
    if i > 10 && arr[i] == 0 { break; }
    process(i);
}
"#;

        // Test validates:
        // 1. Complex boolean condition
        // 2. Logical operators in break condition
        // 3. Syntax similarity
        assert!(c_code.contains("&&"));
        assert!(rust_expected.contains("&&"));
    }

    /// Test 11: Multiple breaks in same loop
    /// Different exit conditions at different points
    #[test]
    fn test_multiple_breaks_in_loop() {
        let c_code = r#"
while (running) {
    if (check_exit_early()) break;

    process_step1();

    if (should_stop()) break;

    process_step2();
}
"#;

        let rust_expected = r#"
while running {
    if check_exit_early() { break; }

    process_step1();

    if should_stop() { break; }

    process_step2();
}
"#;

        // Test validates:
        // 1. Multiple break points in same loop
        // 2. Different conditions at different stages
        // 3. Both languages support this pattern
        assert_eq!(c_code.matches("break;").count(), 2);
        assert_eq!(rust_expected.matches("break;").count(), 2);
    }

    /// Test 12: Break in nested switch (C only needs it)
    /// Switch inside loop
    #[test]
    fn test_break_in_nested_switch() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    switch (arr[i]) {
        case 0: handle_zero(); break;
        case -1: continue;  // Skip to next iteration
        default: handle_other(); break;
    }
    if (done) break;  // Break loop, not switch
}
"#;

        let rust_expected = r#"
for i in 0..n {
    match arr[i] {
        0 => handle_zero(),
        -1 => continue,
        _ => handle_other(),
    }
    if done { break; }
}
"#;

        // Test validates:
        // 1. C switch requires break
        // 2. Loop break separate from switch break
        // 3. Rust match doesn't need break
        assert!(c_code.contains("switch"));
        assert!(rust_expected.contains("match"));
        assert!(c_code.matches("break;").count() > 1);
    }

    /// Test 13: Break with value (Rust only feature)
    /// Loop expression that returns value on break
    #[test]
    fn test_break_with_value() {
        let _c_code = r#"
int result = -1;
for (int i = 0; i < n; i++) {
    if (arr[i] > 0) {
        result = arr[i];
        break;
    }
}
"#;

        let rust_with_value = r#"
let result = 'search: loop {
    for i in 0..n {
        if arr[i] > 0 {
            break 'search arr[i];  // Break with value
        }
    }
    break -1;  // Default value
};
"#;

        // Test validates:
        // 1. C needs variable to store result before break
        // 2. Rust can break with value directly
        // 3. More functional style in Rust
        assert!(_c_code.contains("result = arr[i];"));
        assert!(rust_with_value.contains("break 'search arr[i];"));
    }

    /// Test 14: Break after array modification
    /// Modify then break pattern
    #[test]
    fn test_break_after_modification() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    if (arr[i] == target) {
        arr[i] = NEW_VALUE;
        found_index = i;
        break;
    }
}
"#;

        let rust_expected = r#"
for i in 0..n {
    if arr[i] == target {
        arr[i] = NEW_VALUE;
        found_index = i;
        break;
    }
}
"#;

        // Test validates:
        // 1. Modify data before breaking
        // 2. Multiple statements before break
        // 3. Common pattern in both languages
        assert!(c_code.contains("arr[i] = NEW_VALUE;"));
        assert!(rust_expected.contains("arr[i] = NEW_VALUE;"));
    }

    /// Test 15: Break statement transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_break_transformation_rules_summary() {
        let c_code = r#"
// Rule 1: Simple loop break
for (int i = 0; i < n; i++) {
    if (condition) break;
}

// Rule 2: While loop break
while (1) {
    if (done) break;
}

// Rule 3: Switch case break (required)
switch (x) {
    case 1: do_work(); break;
    default: break;
}

// Rule 4: Nested loops with flag
int found = 0;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
        if (match) { found = 1; break; }
    }
    if (found) break;
}

// Rule 5: Search with break
for (int i = 0; i < n; i++) {
    if (arr[i] == target) {
        result = i;
        break;
    }
}
"#;

        let rust_expected = r#"
// Rule 1: Simple loop break (same)
for i in 0..n {
    if condition { break; }
}

// Rule 2: While loop break
loop {
    if done { break; }
}

// Rule 3: Match (no break needed)
match x {
    1 => do_work(),
    _ => {}
}

// Rule 4: Labeled break (idiomatic)
'outer: for i in 0..n {
    for j in 0..m {
        if match { break 'outer; }
    }
}

// Rule 5: Search with break (same)
for i in 0..n {
    if arr[i] == target {
        result = i;
        break;
    }
}
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("break;"));
        assert!(rust_expected.contains("break;"));
        assert!(rust_expected.contains("'outer:"));
        assert!(c_code.contains("switch"));
        assert!(rust_expected.contains("match"));
    }
}