depyler-core 3.19.29

Core transpilation engine for the Depyler Python-to-Rust transpiler
Documentation
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
//! Automatic test generation for transpiled functions
//!
//! This module generates property-based tests using quickcheck
//! for pure functions with appropriate properties.

use crate::hir::{BinOp, HirExpr, HirFunction, HirStmt, Type};
use anyhow::Result;
use quote::quote;
use syn;

/// Configuration for test generation
#[derive(Debug, Clone)]
pub struct TestGenConfig {
    /// Generate property-based tests
    pub generate_property_tests: bool,
    /// Generate example-based tests
    pub generate_example_tests: bool,
    /// Maximum number of test cases for quickcheck
    pub max_test_cases: usize,
    /// Generate shrinking tests
    pub enable_shrinking: bool,
}

impl Default for TestGenConfig {
    fn default() -> Self {
        Self {
            generate_property_tests: true,
            generate_example_tests: true,
            max_test_cases: 100,
            enable_shrinking: true,
        }
    }
}

/// Test generator for HIR functions
pub struct TestGenerator {
    config: TestGenConfig,
}

impl TestGenerator {
    pub fn new(config: TestGenConfig) -> Self {
        Self { config }
    }

    /// Generate test items for a single function (without mod tests wrapper)
    ///
    /// DEPYLER-0280 FIX: This generates test functions only, not the module wrapper.
    /// The module wrapper should be added once at the file level.
    pub fn generate_test_items_for_function(
        &self,
        func: &HirFunction,
    ) -> Result<Vec<proc_macro2::TokenStream>> {
        // Only generate tests for pure functions
        if !func.properties.is_pure {
            return Ok(Vec::new());
        }

        let mut test_functions = Vec::new();

        // Generate property-based tests
        if self.config.generate_property_tests {
            if let Some(prop_test) = self.generate_property_test(func)? {
                test_functions.push(prop_test);
            }
        }

        // Generate example-based tests
        if self.config.generate_example_tests {
            if let Some(example_test) = self.generate_example_test(func)? {
                test_functions.push(example_test);
            }
        }

        Ok(test_functions)
    }

    /// Generate a complete test module for multiple functions
    ///
    /// DEPYLER-0280 FIX: Wraps all test items in a single `mod tests {}` block.
    /// This prevents "the name `tests` is defined multiple times" errors.
    pub fn generate_tests_module(
        &self,
        functions: &[HirFunction],
    ) -> Result<Option<proc_macro2::TokenStream>> {
        let mut all_test_items = Vec::new();

        // Collect test items from all functions
        for func in functions {
            let test_items = self.generate_test_items_for_function(func)?;
            all_test_items.extend(test_items);
        }

        // If no tests were generated, return None
        if all_test_items.is_empty() {
            return Ok(None);
        }

        // Wrap all tests in a single mod tests block
        Ok(Some(quote! {
            #[cfg(test)]
            mod tests {
                use super::*;
                use quickcheck::{quickcheck, TestResult};

                #(#all_test_items)*
            }
        }))
    }

    /// Generate tests for a function if applicable (DEPRECATED - use generate_tests_module instead)
    ///
    /// DEPYLER-0280: This function is deprecated because it creates duplicate `mod tests {}` blocks.
    /// Use `generate_tests_module()` for module-level test generation instead.
    #[deprecated(
        since = "3.19.22",
        note = "Use generate_tests_module() to avoid duplicate mod tests blocks (DEPYLER-0280)"
    )]
    pub fn generate_tests(&self, func: &HirFunction) -> Result<Option<proc_macro2::TokenStream>> {
        let test_items = self.generate_test_items_for_function(func)?;

        if test_items.is_empty() {
            return Ok(None);
        }

        Ok(Some(quote! {
            #[cfg(test)]
            mod tests {
                use super::*;
                use quickcheck::{quickcheck, TestResult};

                #(#test_items)*
            }
        }))
    }

    /// Generate property-based test for a function
    fn generate_property_test(
        &self,
        func: &HirFunction,
    ) -> Result<Option<proc_macro2::TokenStream>> {
        let func_name = syn::Ident::new(&func.name, proc_macro2::Span::call_site());
        let test_name = syn::Ident::new(
            &format!("quickcheck_{}", func.name),
            proc_macro2::Span::call_site(),
        );

        // Determine properties to test based on function analysis
        let properties = self.analyze_function_properties(func);

        if properties.is_empty() {
            return Ok(None);
        }

        // Generate parameter types and names for quickcheck
        let param_types: Vec<_> = func
            .params
            .iter()
            .map(|param| self.type_to_quickcheck_type(&param.ty))
            .collect();

        let param_names: Vec<_> = func
            .params
            .iter()
            .map(|param| syn::Ident::new(&param.name, proc_macro2::Span::call_site()))
            .collect();

        // DEPYLER-0281: Pass function parameters for type-aware conversions
        let property_checks: Vec<_> = properties
            .iter()
            .map(|prop| self.property_to_assertion(prop, &func_name, &param_names, &func.params))
            .collect();

        Ok(Some(quote! {
            #[test]
            fn #test_name() {
                fn prop(#(#param_names: #param_types),*) -> TestResult {
                    #(#property_checks)*
                    TestResult::passed()
                }

                quickcheck(prop as fn(#(#param_types),*) -> TestResult);
            }
        }))
    }

    /// Generate example-based test
    fn generate_example_test(
        &self,
        func: &HirFunction,
    ) -> Result<Option<proc_macro2::TokenStream>> {
        let test_name = syn::Ident::new(
            &format!("test_{}_examples", func.name),
            proc_macro2::Span::call_site(),
        );

        // Generate test cases based on function type
        let test_cases = self.generate_test_cases(func);

        if test_cases.is_empty() {
            return Ok(None);
        }

        Ok(Some(quote! {
            #[test]
            fn #test_name() {
                #(#test_cases)*
            }
        }))
    }

    /// Analyze function to determine testable properties
    fn analyze_function_properties(&self, func: &HirFunction) -> Vec<TestProperty> {
        // DEPYLER-0282 FIXED: String parameters now correctly use Cow<'_, str> instead of
        // Cow<'static, str>, so property tests work properly with local String values.
        // The DEPYLER-0281 workaround has been removed.

        let mut properties = Vec::new();

        // Check for common patterns
        if self.is_identity_function(func) {
            properties.push(TestProperty::Identity);
        }

        if self.is_commutative(func) {
            properties.push(TestProperty::Commutative);
        }

        if self.is_associative(func) {
            properties.push(TestProperty::Associative);
        }

        if self.returns_non_negative(func) {
            properties.push(TestProperty::NonNegative);
        }

        if self.preserves_length(func) {
            properties.push(TestProperty::LengthPreserving);
        }

        if self.is_idempotent(func) {
            properties.push(TestProperty::Idempotent);
        }

        if self.is_sorting_function(func) {
            properties.push(TestProperty::Sorted);
            properties.push(TestProperty::SameElements);
        }

        properties
    }

    /// Check if function is an identity function
    fn is_identity_function(&self, func: &HirFunction) -> bool {
        // Simple case: function with one parameter that returns it unchanged
        if func.params.len() == 1 && func.body.len() == 1 {
            if let HirStmt::Return(Some(HirExpr::Var(name))) = &func.body[0] {
                return name == &func.params[0].name;
            }
        }
        false
    }

    /// Check if function is commutative (like addition)
    fn is_commutative(&self, func: &HirFunction) -> bool {
        if func.params.len() == 2 && func.body.len() == 1 {
            if let HirStmt::Return(Some(HirExpr::Binary { op, left, right })) = &func.body[0] {
                // DEPYLER-0286 FIX: String concatenation (BinOp::Add on strings) is NOT commutative!
                // "ab" + "cd" ≠ "cd" + "ab"
                // Only numeric addition is commutative, not string concatenation.

                // Check if this is string concatenation (Add with String parameters)
                let is_string_concat = matches!(op, BinOp::Add)
                    && (matches!(func.params[0].ty, Type::String)
                        || matches!(func.params[1].ty, Type::String));

                // If it's string concatenation, it's NOT commutative
                if is_string_concat {
                    return false;
                }

                // Check if it's a commutative operation
                matches!(
                    op,
                    BinOp::Add | BinOp::Mul | BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor
                ) && self.is_simple_param_reference(left, &func.params[0].name)
                    && self.is_simple_param_reference(right, &func.params[1].name)
            } else {
                false
            }
        } else {
            false
        }
    }

    /// Check if expression is a simple parameter reference
    fn is_simple_param_reference(&self, expr: &HirExpr, param_name: &str) -> bool {
        matches!(expr, HirExpr::Var(name) if name == param_name)
    }

    /// Check if function is associative
    fn is_associative(&self, _func: &HirFunction) -> bool {
        // This is more complex to detect automatically
        // For now, return false
        false
    }

    /// Check if function always returns non-negative values
    fn returns_non_negative(&self, func: &HirFunction) -> bool {
        // Check for abs-like patterns
        func.name.contains("abs") || func.name.contains("magnitude")
    }

    /// Check if function preserves collection length
    fn preserves_length(&self, func: &HirFunction) -> bool {
        // Check if input and output are both lists/arrays
        if func.params.len() == 1 {
            if let (Type::List(_), Type::List(_)) = (&func.params[0].ty, &func.ret_type) {
                // Simple heuristic: sorting and mapping functions preserve length
                return func.name.contains("sort") || func.name.contains("map");
            }
        }
        false
    }

    /// Check if function is idempotent
    fn is_idempotent(&self, func: &HirFunction) -> bool {
        func.name.contains("normalize") || func.name.contains("clean")
    }

    /// Check if function is a sorting function
    fn is_sorting_function(&self, func: &HirFunction) -> bool {
        // DEPYLER-0189: Must have at least one parameter to be a sorting function
        !func.params.is_empty() && func.name.contains("sort")
    }

    /// Convert Python type to quickcheck-compatible type
    #[allow(clippy::only_used_in_recursion)]
    fn type_to_quickcheck_type(&self, ty: &Type) -> proc_macro2::TokenStream {
        match ty {
            Type::Int => quote! { i32 },
            Type::Float => quote! { f64 },
            Type::String => quote! { String },
            Type::Bool => quote! { bool },
            Type::List(inner) => {
                let inner_type = self.type_to_quickcheck_type(inner);
                quote! { Vec<#inner_type> }
            }
            _ => quote! { () }, // Unsupported type
        }
    }

    /// Convert a property test argument to match function signature
    ///
    /// DEPYLER-0281 FIX: Property tests use simple QuickCheck types (String, Vec<T>),
    /// but functions may have complex signatures (Cow<'static, str>, &Vec<T>).
    /// This function generates conversion code to bridge the gap.
    fn convert_arg_for_property_test(
        &self,
        ty: &Type,
        arg_name: &syn::Ident,
    ) -> proc_macro2::TokenStream {
        match ty {
            Type::String => {
                // DEPYLER-0281 FIX: String parameters may become Cow<'static, str> or &str
                // Use (&*arg).into() which converts via type inference:
                //   - For `fn f(s: Cow<'static, str>)`: &str → Cow::Owned via From<String> after clone
                //   - For `fn f(s: &str)`: &str → &str (into() is no-op when T: Copy)
                // The &* dereferences String → str, then borrows as &str for inference.
                quote! { (&*#arg_name).into() }
            }
            Type::List(_) => {
                // List parameters become &Vec<T>
                quote! { &#arg_name }
            }
            Type::Dict(_, _) => {
                // Dict parameters become &HashMap<K, V>
                quote! { &#arg_name }
            }
            _ => {
                // Simple types (int, float, bool) - use directly with clone
                quote! { #arg_name.clone() }
            }
        }
    }

    /// Convert property to assertion code
    ///
    /// DEPYLER-0281: Now accepts func_params to enable type-aware argument conversions.
    /// This ensures property tests work with complex types like Cow<'static, str>.
    fn property_to_assertion(
        &self,
        prop: &TestProperty,
        func_name: &syn::Ident,
        params: &[syn::Ident],
        func_params: &[crate::hir::HirParam],
    ) -> proc_macro2::TokenStream {
        match prop {
            TestProperty::Identity => {
                // DEPYLER-0189: Bounds check before accessing params
                if params.is_empty() || func_params.is_empty() {
                    return quote! {};
                }
                let param = &params[0];

                // DEPYLER-0281: Convert argument to match function signature
                let param_converted = self.convert_arg_for_property_test(&func_params[0].ty, param);

                quote! {
                    let result = #func_name(#param_converted);
                    if result != #param {
                        return TestResult::failed();
                    }
                }
            }
            TestProperty::Commutative => {
                // DEPYLER-0189: Bounds check before accessing params
                if params.len() < 2 || func_params.len() < 2 {
                    return quote! {};
                }
                let (a, b) = (&params[0], &params[1]);

                // DEPYLER-0281 FIX: Convert arguments to match function signature
                // QuickCheck generates simple types (String), but functions may expect
                // complex types (Cow<'static, str>). Convert accordingly.
                let a_converted = self.convert_arg_for_property_test(&func_params[0].ty, a);
                let b_converted = self.convert_arg_for_property_test(&func_params[1].ty, b);

                // DEPYLER-0284 FIX: Check for potential overflow with integer addition
                // DEPYLER-0285 FIX: Check for NaN in float operations
                let special_value_check = if matches!(func_params[0].ty, Type::Int)
                    && matches!(func_params[1].ty, Type::Int)
                {
                    quote! {
                        // Skip test if values would overflow
                        if (#a > 0 && #b > i32::MAX - #a) || (#a < 0 && #b < i32::MIN - #a) {
                            return TestResult::discard();
                        }
                    }
                } else if matches!(func_params[0].ty, Type::Float)
                    && matches!(func_params[1].ty, Type::Float)
                {
                    quote! {
                        // Skip test if either value is NaN or infinite
                        if #a.is_nan() || #b.is_nan() || #a.is_infinite() || #b.is_infinite() {
                            return TestResult::discard();
                        }
                    }
                } else {
                    quote! {}
                };

                quote! {
                    #special_value_check
                    let result1 = #func_name(#a_converted, #b_converted);
                    let result2 = #func_name(#b_converted, #a_converted);
                    if result1 != result2 {
                        return TestResult::failed();
                    }
                }
            }
            TestProperty::NonNegative => {
                // DEPYLER-0281: Convert all arguments to match function signature
                let converted_args: Vec<_> = params
                    .iter()
                    .zip(func_params.iter())
                    .map(|(param, func_param)| {
                        self.convert_arg_for_property_test(&func_param.ty, param)
                    })
                    .collect();

                quote! {
                    let result = #func_name(#(#converted_args),*);
                    if result < 0 {
                        return TestResult::failed();
                    }
                }
            }
            TestProperty::LengthPreserving => {
                // DEPYLER-0189: Bounds check before accessing params
                if params.is_empty() || func_params.is_empty() {
                    return quote! {};
                }
                let param = &params[0];

                // DEPYLER-0281: Convert argument to match function signature
                let param_converted = self.convert_arg_for_property_test(&func_params[0].ty, param);

                quote! {
                    let input_len = #param.len();
                    let result = #func_name(#param_converted);
                    if result.len() != input_len {
                        return TestResult::failed();
                    }
                }
            }
            TestProperty::Sorted => {
                // DEPYLER-0281: Convert all arguments to match function signature
                let converted_args: Vec<_> = params
                    .iter()
                    .zip(func_params.iter())
                    .map(|(param, func_param)| {
                        self.convert_arg_for_property_test(&func_param.ty, param)
                    })
                    .collect();

                quote! {
                    let result = #func_name(#(#converted_args),*);
                    for i in 1..result.len() {
                        if result[i-1] > result[i] {
                            return TestResult::failed();
                        }
                    }
                }
            }
            TestProperty::SameElements => {
                // DEPYLER-0189: Bounds check before accessing params
                if params.is_empty() || func_params.is_empty() {
                    return quote! {};
                }
                let param = &params[0];

                // DEPYLER-0281: Convert argument to match function signature
                let param_converted = self.convert_arg_for_property_test(&func_params[0].ty, param);

                quote! {
                    let mut input_sorted = #param.clone();
                    input_sorted.sort();
                    let mut result = #func_name(#param_converted);
                    result.sort();
                    if input_sorted != result {
                        return TestResult::failed();
                    }
                }
            }
            TestProperty::Idempotent => {
                // DEPYLER-0281: Convert all arguments to match function signature
                let converted_args: Vec<_> = params
                    .iter()
                    .zip(func_params.iter())
                    .map(|(param, func_param)| {
                        self.convert_arg_for_property_test(&func_param.ty, param)
                    })
                    .collect();

                quote! {
                    let once = #func_name(#(#converted_args),*);
                    let twice = #func_name(once.clone());
                    if once != twice {
                        return TestResult::failed();
                    }
                }
            }
            _ => quote! {},
        }
    }

    /// Generate example test cases
    fn generate_test_cases(&self, func: &HirFunction) -> Vec<proc_macro2::TokenStream> {
        let func_name = syn::Ident::new(&func.name, proc_macro2::Span::call_site());
        let mut cases = Vec::new();

        // Generate basic test cases based on function type and parameters
        match (&func.ret_type, func.params.len()) {
            (Type::Int, 0) => {
                // No parameters - just call the function
                cases.push(quote! {
                    let _ = #func_name();
                });
            }
            (Type::Int, 1) => {
                // DEPYLER-0269: Check actual parameter type before generating test values
                let param_type = &func.params[0].ty;
                match param_type {
                    Type::Int => {
                        // Single integer parameter
                        if func.name.contains("abs") {
                            // Special case for absolute value functions
                            cases.push(quote! {
                                assert_eq!(#func_name(0), 0);
                                assert_eq!(#func_name(1), 1);
                                assert_eq!(#func_name(-1), 1);
                                assert_eq!(#func_name(i32::MIN + 1), i32::MAX);
                            });
                        } else {
                            // General case
                            cases.push(quote! {
                                assert_eq!(#func_name(0), 0);
                                assert_eq!(#func_name(1), 1);
                                assert_eq!(#func_name(-1), -1);
                            });
                        }
                    }
                    Type::List(_) => {
                        // DEPYLER-0283 FIX: List parameter returning int
                        // Detect if it's a sum function vs length function by name
                        if func.name.contains("sum") {
                            // Sum function - test sum of elements
                            cases.push(quote! {
                                assert_eq!(#func_name(&vec![]), 0);
                                assert_eq!(#func_name(&vec![1]), 1);
                                assert_eq!(#func_name(&vec![1, 2, 3]), 6);  // 1+2+3=6
                            });
                        } else if func.name.contains("len")
                            || func.name.contains("count")
                            || func.name.contains("size")
                        {
                            // Length/count function - test length
                            cases.push(quote! {
                                assert_eq!(#func_name(&vec![]), 0);
                                assert_eq!(#func_name(&vec![1]), 1);
                                assert_eq!(#func_name(&vec![1, 2, 3]), 3);
                            });
                        } else {
                            // Unknown function - use conservative length-based test
                            cases.push(quote! {
                                assert_eq!(#func_name(&vec![]), 0);
                                assert_eq!(#func_name(&vec![1]), 1);
                                assert_eq!(#func_name(&vec![1, 2, 3]), 3);
                            });
                        }
                    }
                    Type::String => {
                        // String parameter - generate string test cases
                        cases.push(quote! {
                            assert_eq!(#func_name(""), 0);
                            assert_eq!(#func_name("a"), 1);
                            assert_eq!(#func_name("abc"), 3);
                        });
                    }
                    _ => {
                        // Unsupported parameter type - skip test generation
                    }
                }
            }
            (Type::Int, 2)
                if matches!(&func.params[0].ty, Type::Int)
                    && matches!(&func.params[1].ty, Type::Int) =>
            {
                // Two integer parameters - test basic cases
                cases.push(quote! {
                    assert_eq!(#func_name(0, 0), 0);
                    assert_eq!(#func_name(1, 2), 3);
                    assert_eq!(#func_name(-1, 1), 0);
                });
            }
            (Type::Bool, _) => {
                // Test boolean functions
                if func.params.len() == 1 {
                    cases.push(quote! {
                        // Test with edge cases
                        let _ = #func_name(Default::default());
                    });
                }
            }
            (Type::List(_), _) => {
                // Test with empty and single-element lists
                if func.params.len() == 1 && matches!(&func.params[0].ty, Type::List(_)) {
                    cases.push(quote! {
                        assert_eq!(#func_name(vec![]), vec![]);
                        assert_eq!(#func_name(vec![1]), vec![1]);
                    });
                }
            }
            _ => {}
        }

        cases
    }
}

/// Properties that can be tested
#[derive(Debug, Clone, PartialEq)]
enum TestProperty {
    Identity,
    Commutative,
    Associative,
    NonNegative,
    LengthPreserving,
    Sorted,
    SameElements,
    Idempotent,
    #[allow(dead_code)]
    Distributive,
    #[allow(dead_code)]
    Monotonic,
}