depyler-core 3.19.8

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
//! 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 tests for a function if applicable
    pub fn generate_tests(&self, func: &HirFunction) -> Result<Option<proc_macro2::TokenStream>> {
        // Only generate tests for pure functions
        if !func.properties.is_pure {
            return Ok(None);
        }

        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);
            }
        }

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

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

                #(#test_functions)*
            }
        }))
    }

    /// 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();

        let property_checks: Vec<_> = properties
            .iter()
            .map(|prop| self.property_to_assertion(prop, &func_name, &param_names))
            .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> {
        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] {
                // 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 property to assertion code
    fn property_to_assertion(
        &self,
        prop: &TestProperty,
        func_name: &syn::Ident,
        params: &[syn::Ident],
    ) -> proc_macro2::TokenStream {
        match prop {
            TestProperty::Identity => {
                // DEPYLER-0189: Bounds check before accessing params
                if params.is_empty() {
                    return quote! {};
                }
                let param = &params[0];
                quote! {
                    let result = #func_name(#param.clone());
                    if result != #param {
                        return TestResult::failed();
                    }
                }
            }
            TestProperty::Commutative => {
                // DEPYLER-0189: Bounds check before accessing params
                if params.len() < 2 {
                    return quote! {};
                }
                let (a, b) = (&params[0], &params[1]);
                quote! {
                    let result1 = #func_name(#a.clone(), #b.clone());
                    let result2 = #func_name(#b.clone(), #a.clone());
                    if result1 != result2 {
                        return TestResult::failed();
                    }
                }
            }
            TestProperty::NonNegative => {
                quote! {
                    let result = #func_name(#(#params.clone()),*);
                    if result < 0 {
                        return TestResult::failed();
                    }
                }
            }
            TestProperty::LengthPreserving => {
                // DEPYLER-0189: Bounds check before accessing params
                if params.is_empty() {
                    return quote! {};
                }
                let param = &params[0];
                quote! {
                    let input_len = #param.len();
                    let result = #func_name(#param.clone());
                    if result.len() != input_len {
                        return TestResult::failed();
                    }
                }
            }
            TestProperty::Sorted => {
                quote! {
                    let result = #func_name(#(#params.clone()),*);
                    for i in 1..result.len() {
                        if result[i-1] > result[i] {
                            return TestResult::failed();
                        }
                    }
                }
            }
            TestProperty::SameElements => {
                // DEPYLER-0189: Bounds check before accessing params (THIS WAS THE PANIC!)
                if params.is_empty() {
                    return quote! {};
                }
                let param = &params[0];
                quote! {
                    let mut input_sorted = #param.clone();
                    input_sorted.sort();
                    let mut result = #func_name(#param.clone());
                    result.sort();
                    if input_sorted != result {
                        return TestResult::failed();
                    }
                }
            }
            TestProperty::Idempotent => {
                quote! {
                    let once = #func_name(#(#params.clone()),*);
                    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) => {
                // 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::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,
}