depyler-core 3.19.20

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
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
//! Enhanced borrowing context for proper ownership pattern inference
//!
//! This module provides comprehensive analysis of parameter usage patterns
//! to determine optimal borrowing strategies for function parameters.

use crate::hir::{AssignTarget, HirExpr, HirFunction, HirStmt, Type as PythonType};
use crate::type_mapper::{RustType, TypeMapper};
use indexmap::IndexMap;
use std::collections::{HashMap, HashSet};

/// Comprehensive borrowing context for analyzing parameter usage
#[derive(Debug)]
pub struct BorrowingContext {
    /// Usage patterns for each parameter
    param_usage: HashMap<String, ParameterUsagePattern>,
    /// Variables that are moved or consumed
    moved_vars: HashSet<String>,
    /// Variables that are borrowed mutably
    mut_borrowed_vars: HashSet<String>,
    /// Variables that are borrowed immutably
    immut_borrowed_vars: HashSet<String>,
    /// Control flow context stack
    context_stack: Vec<AnalysisContext>,
    /// Function return type for escape analysis
    return_type: Option<PythonType>,
}

/// Detailed parameter usage pattern
#[derive(Debug, Clone, Default)]
pub struct ParameterUsagePattern {
    /// Parameter is read without modification
    pub is_read: bool,
    /// Parameter is modified (assigned to)
    pub is_mutated: bool,
    /// Parameter is moved/consumed (passed to function that takes ownership)
    pub is_moved: bool,
    /// Parameter escapes through return
    pub escapes_through_return: bool,
    /// Parameter is stored in a struct/container
    pub is_stored: bool,
    /// Parameter is used in a closure
    pub used_in_closure: bool,
    /// Parameter is used in loops (affects borrowing strategy)
    pub used_in_loop: bool,
    /// Nested field access patterns
    pub field_accesses: HashSet<String>,
    /// Method calls on the parameter
    pub method_calls: HashSet<String>,
    /// Specific expressions where parameter is used
    pub usage_sites: Vec<UsageSite>,
}

/// Site where a parameter is used
#[derive(Debug, Clone)]
pub struct UsageSite {
    /// Type of usage
    pub usage_type: UsageType,
    /// Whether in a loop context
    pub in_loop: bool,
    /// Whether in a conditional context
    pub in_conditional: bool,
    /// Depth of borrowing (for nested borrows)
    pub borrow_depth: usize,
}

/// Type of parameter usage
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UsageType {
    /// Simple read access
    Read,
    /// Mutable access
    Write,
    /// Method call that might mutate
    MethodCall(String),
    /// Passed to another function
    FunctionArg { takes_ownership: bool },
    /// Returned from function
    Return,
    /// Stored in a data structure
    Store,
    /// Used in a closure
    Closure { captures_by_value: bool },
    /// Field access
    FieldAccess(String),
    /// Index access
    IndexAccess,
}

/// Analysis context for control flow
#[derive(Debug, Clone)]
#[allow(dead_code)]
enum AnalysisContext {
    Loop,
    Conditional,
    Closure { captures: HashSet<String> },
    Function,
}

/// Result of borrowing analysis
#[derive(Debug, Clone)]
pub struct BorrowingAnalysisResult {
    /// Recommended borrowing strategy for each parameter
    pub param_strategies: IndexMap<String, BorrowingStrategy>,
    /// Additional insights
    pub insights: Vec<BorrowingInsight>,
}

/// Recommended borrowing strategy
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BorrowingStrategy {
    /// Take ownership (move)
    TakeOwnership,
    /// Borrow immutably
    BorrowImmutable { lifetime: Option<String> },
    /// Borrow mutably
    BorrowMutable { lifetime: Option<String> },
    /// Use Cow for flexibility
    UseCow { lifetime: String },
    /// Use Arc/Rc for shared ownership
    UseSharedOwnership { is_thread_safe: bool },
}

/// Insights from borrowing analysis
#[derive(Debug, Clone)]
pub enum BorrowingInsight {
    /// Parameter could be borrowed but is currently moved
    UnnecessaryMove(String),
    /// Parameter could use a more specific lifetime
    LifetimeOptimization { param: String, suggestion: String },
    /// Parameter access pattern suggests Copy trait
    SuggestCopyDerive(String),
    /// Multiple mutable borrows detected
    PotentialBorrowConflict {
        param: String,
        locations: Vec<String>,
    },
}

impl BorrowingContext {
    pub fn new(return_type: Option<PythonType>) -> Self {
        Self {
            param_usage: HashMap::new(),
            moved_vars: HashSet::new(),
            mut_borrowed_vars: HashSet::new(),
            immut_borrowed_vars: HashSet::new(),
            context_stack: vec![AnalysisContext::Function],
            return_type,
        }
    }

    /// Analyze a function to determine optimal borrowing strategies
    pub fn analyze_function(
        &mut self,
        func: &HirFunction,
        type_mapper: &TypeMapper,
    ) -> BorrowingAnalysisResult {
        // Initialize parameter tracking
        for param in &func.params {
            self.param_usage
                .insert(param.name.clone(), ParameterUsagePattern::default());
        }

        // Analyze function body
        for stmt in &func.body {
            self.analyze_statement(stmt);
        }

        // Determine borrowing strategies based on usage patterns
        self.determine_strategies(func, type_mapper)
    }

    /// Analyze a statement for parameter usage
    fn analyze_statement(&mut self, stmt: &HirStmt) {
        match stmt {
            HirStmt::Assign { target, value, .. } => {
                // Check if assigning to a parameter (mutation)
                let in_loop = self.is_in_loop();
                let in_conditional = self.is_in_conditional();
                if let AssignTarget::Symbol(symbol) = target {
                    if let Some(usage) = self.param_usage.get_mut(symbol) {
                        usage.is_mutated = true;
                        usage.usage_sites.push(UsageSite {
                            usage_type: UsageType::Write,
                            in_loop,
                            in_conditional,
                            borrow_depth: 0,
                        });
                    }
                }
                self.analyze_expression(value, 0);
            }
            HirStmt::Return(expr) => {
                if let Some(e) = expr {
                    self.analyze_expression_for_return(e);
                }
            }
            HirStmt::If {
                condition,
                then_body,
                else_body,
            } => {
                self.analyze_expression(condition, 0);
                self.context_stack.push(AnalysisContext::Conditional);
                for stmt in then_body {
                    self.analyze_statement(stmt);
                }
                if let Some(else_stmts) = else_body {
                    for stmt in else_stmts {
                        self.analyze_statement(stmt);
                    }
                }
                self.context_stack.pop();
            }
            HirStmt::While { condition, body } => {
                self.context_stack.push(AnalysisContext::Loop);
                self.analyze_expression(condition, 0);
                for stmt in body {
                    self.analyze_statement(stmt);
                }
                self.context_stack.pop();
            }
            HirStmt::For {
                target: _,
                iter,
                body,
            } => {
                self.context_stack.push(AnalysisContext::Loop);
                self.analyze_expression(iter, 0);
                for stmt in body {
                    self.analyze_statement(stmt);
                }
                self.context_stack.pop();
            }
            HirStmt::Expr(expr) => {
                self.analyze_expression(expr, 0);
            }
            HirStmt::Raise { exception, cause } => {
                if let Some(exc) = exception {
                    self.analyze_expression(exc, 0);
                }
                if let Some(c) = cause {
                    self.analyze_expression(c, 0);
                }
            }
            HirStmt::Break { .. } | HirStmt::Continue { .. } | HirStmt::Pass => {
                // Break, continue, and pass don't analyze any expressions
            }
            HirStmt::Assert { test, msg } => {
                // Analyze the test expression and optional message
                self.analyze_expression(test, 0);
                if let Some(message) = msg {
                    self.analyze_expression(message, 0);
                }
            }
            HirStmt::With {
                context,
                target: _,
                body,
            } => {
                // Analyze context expression
                self.analyze_expression(context, 0);

                // Track the target variable if present
                // Note: We don't track local variables here, only parameters

                // Analyze body statements
                for stmt in body {
                    self.analyze_statement(stmt);
                }
            }
            HirStmt::Try {
                body,
                handlers,
                orelse,
                finalbody,
            } => {
                // Analyze try body
                for stmt in body {
                    self.analyze_statement(stmt);
                }

                // Analyze except handlers
                for handler in handlers {
                    for stmt in &handler.body {
                        self.analyze_statement(stmt);
                    }
                }

                // Analyze else clause
                if let Some(else_stmts) = orelse {
                    for stmt in else_stmts {
                        self.analyze_statement(stmt);
                    }
                }

                // Analyze finally clause
                if let Some(finally_stmts) = finalbody {
                    for stmt in finally_stmts {
                        self.analyze_statement(stmt);
                    }
                }
            }
        }
    }

    /// Analyze an expression for parameter usage
    fn analyze_expression(&mut self, expr: &HirExpr, borrow_depth: usize) {
        match expr {
            HirExpr::Var(name) => {
                let in_loop = self.is_in_loop();
                let in_conditional = self.is_in_conditional();
                if let Some(usage) = self.param_usage.get_mut(name) {
                    usage.is_read = true;
                    usage.usage_sites.push(UsageSite {
                        usage_type: UsageType::Read,
                        in_loop,
                        in_conditional,
                        borrow_depth,
                    });
                    if in_loop {
                        usage.used_in_loop = true;
                    }
                }
            }
            HirExpr::Attribute { value, attr } => {
                if let HirExpr::Var(name) = &**value {
                    let in_loop = self.is_in_loop();
                    let in_conditional = self.is_in_conditional();
                    if let Some(usage) = self.param_usage.get_mut(name) {
                        usage.field_accesses.insert(attr.clone());
                        usage.usage_sites.push(UsageSite {
                            usage_type: UsageType::FieldAccess(attr.clone()),
                            in_loop,
                            in_conditional,
                            borrow_depth: borrow_depth + 1,
                        });
                    }
                }
                self.analyze_expression(value, borrow_depth + 1);
            }
            HirExpr::Call { func, args } => {
                // Analyze function calls to determine if parameters are moved
                let in_loop = self.is_in_loop();
                let in_conditional = self.is_in_conditional();
                for (i, arg) in args.iter().enumerate() {
                    if let HirExpr::Var(name) = arg {
                        let takes_ownership = self.function_takes_ownership(func, i);
                        if let Some(usage) = self.param_usage.get_mut(name) {
                            // Conservative: assume ownership transfer unless we know better
                            if takes_ownership {
                                usage.is_moved = true;
                                self.moved_vars.insert(name.clone());
                            }
                            usage.usage_sites.push(UsageSite {
                                usage_type: UsageType::FunctionArg { takes_ownership },
                                in_loop,
                                in_conditional,
                                borrow_depth,
                            });
                        }
                    }
                    self.analyze_expression(arg, borrow_depth);
                }
            }
            HirExpr::Index { base, index } => {
                if let HirExpr::Var(name) = &**base {
                    let in_loop = self.is_in_loop();
                    let in_conditional = self.is_in_conditional();
                    if let Some(usage) = self.param_usage.get_mut(name) {
                        usage.usage_sites.push(UsageSite {
                            usage_type: UsageType::IndexAccess,
                            in_loop,
                            in_conditional,
                            borrow_depth: borrow_depth + 1,
                        });
                    }
                }
                self.analyze_expression(base, borrow_depth + 1);
                self.analyze_expression(index, borrow_depth);
            }
            HirExpr::Binary { left, right, .. } => {
                self.analyze_expression(left, borrow_depth);
                self.analyze_expression(right, borrow_depth);
            }
            HirExpr::Unary { operand, .. } => {
                self.analyze_expression(operand, borrow_depth);
            }
            HirExpr::List(elements) | HirExpr::Tuple(elements) => {
                for elem in elements {
                    self.analyze_expression(elem, borrow_depth);
                }
            }
            HirExpr::Dict(pairs) => {
                for (k, v) in pairs {
                    self.analyze_expression(k, borrow_depth);
                    self.analyze_expression(v, borrow_depth);
                }
            }
            HirExpr::Borrow { expr, mutable } => {
                if let HirExpr::Var(name) = &**expr {
                    if *mutable {
                        self.mut_borrowed_vars.insert(name.clone());
                    } else {
                        self.immut_borrowed_vars.insert(name.clone());
                    }
                }
                self.analyze_expression(expr, borrow_depth + 1);
            }
            HirExpr::MethodCall { object, args, .. } => {
                self.analyze_expression(object, borrow_depth);
                for arg in args {
                    self.analyze_expression(arg, borrow_depth);
                }
            }
            HirExpr::Slice {
                base,
                start,
                stop,
                step,
            } => {
                self.analyze_expression(base, borrow_depth);
                if let Some(s) = start {
                    self.analyze_expression(s, borrow_depth);
                }
                if let Some(s) = stop {
                    self.analyze_expression(s, borrow_depth);
                }
                if let Some(s) = step {
                    self.analyze_expression(s, borrow_depth);
                }
            }
            HirExpr::Literal(_) => {}
            HirExpr::ListComp {
                element,
                target: _,
                iter,
                condition,
            } => {
                // List comprehensions create a new scope
                self.context_stack.push(AnalysisContext::Loop);

                // Analyze the iterator
                self.analyze_expression(iter, borrow_depth);

                // The target variable is local to the comprehension
                // We don't track it as a parameter usage

                // Analyze the element expression
                self.analyze_expression(element, borrow_depth);

                // Analyze the condition if present
                if let Some(cond) = condition {
                    self.analyze_expression(cond, borrow_depth);
                }

                self.context_stack.pop();
            }
            HirExpr::FString { .. } => {
                // FString support not yet implemented for borrowing analysis
            }
            HirExpr::Lambda { params: _, body } => {
                // Lambda functions capture variables by reference
                // For now, treat lambda bodies like any other expression
                self.analyze_expression(body, borrow_depth);
            }
            HirExpr::Set(elements) | HirExpr::FrozenSet(elements) => {
                for elem in elements {
                    self.analyze_expression(elem, borrow_depth);
                }
            }
            HirExpr::SetComp {
                element,
                target: _,
                iter,
                condition,
            } => {
                // Set comprehensions create a new scope
                self.context_stack.push(AnalysisContext::Loop);

                // Analyze the iterator
                self.analyze_expression(iter, borrow_depth);

                // The target variable is local to the comprehension
                // We don't track it as a parameter usage

                // Analyze the element expression
                self.analyze_expression(element, borrow_depth);

                // Analyze the condition if present
                if let Some(cond) = condition {
                    self.analyze_expression(cond, borrow_depth);
                }

                self.context_stack.pop();
            }
            HirExpr::DictComp {
                key,
                value,
                target: _,
                iter,
                condition,
            } => {
                // Dict comprehensions create a new scope
                self.context_stack.push(AnalysisContext::Loop);

                // Analyze the iterator
                self.analyze_expression(iter, borrow_depth);

                // The target variable is local to the comprehension
                // We don't track it as a parameter usage

                // Analyze the key and value expressions
                self.analyze_expression(key, borrow_depth);
                self.analyze_expression(value, borrow_depth);

                // Analyze the condition if present
                if let Some(cond) = condition {
                    self.analyze_expression(cond, borrow_depth);
                }

                self.context_stack.pop();
            }
            HirExpr::Await { value } => {
                // Await expressions don't change parameter usage patterns
                self.analyze_expression(value, borrow_depth);
            }
            HirExpr::Yield { value } => {
                // Yield expressions pass values to the iterator
                if let Some(v) = value {
                    self.analyze_expression(v, borrow_depth);
                }
            }
            HirExpr::IfExpr { test, body, orelse } => {
                // Analyze all three parts of the ternary expression
                self.analyze_expression(test, borrow_depth);
                self.analyze_expression(body, borrow_depth);
                self.analyze_expression(orelse, borrow_depth);
            }
            HirExpr::SortByKey {
                iterable, key_body, ..
            } => {
                // Analyze the iterable and the key lambda body
                self.analyze_expression(iterable, borrow_depth);
                self.analyze_expression(key_body, borrow_depth);
            }
            HirExpr::GeneratorExp {
                element,
                generators,
            } => {
                // Analyze element expression and all generator iterables
                self.analyze_expression(element, borrow_depth);
                for gen in generators {
                    self.analyze_expression(&gen.iter, borrow_depth);
                    for cond in &gen.conditions {
                        self.analyze_expression(cond, borrow_depth);
                    }
                }
            }
        }
    }

    /// Analyze expression in return context
    fn analyze_expression_for_return(&mut self, expr: &HirExpr) {
        match expr {
            HirExpr::Var(name) => {
                if let Some(usage) = self.param_usage.get_mut(name) {
                    usage.escapes_through_return = true;
                    usage.usage_sites.push(UsageSite {
                        usage_type: UsageType::Return,
                        in_loop: false,
                        in_conditional: false,
                        borrow_depth: 0,
                    });
                }
            }
            HirExpr::Binary { left, right, .. } => {
                // For binary operations in return context, parameters are used but not directly returned
                self.analyze_expression(left, 0);
                self.analyze_expression(right, 0);
                // Mark any parameter used here as escaping since it contributes to the return value
                if let HirExpr::Var(name) = &**left {
                    if let Some(usage) = self.param_usage.get_mut(name) {
                        usage.escapes_through_return = true;
                    }
                }
            }
            _ => self.analyze_expression(expr, 0),
        }
    }

    /// Determine if a function takes ownership of its argument
    fn function_takes_ownership(&self, func_name: &str, _arg_index: usize) -> bool {
        // Known functions that borrow
        let borrowing_functions = [
            "len",
            "str",
            "repr",
            "format",
            "print",
            "isinstance",
            "hasattr",
            "getattr",
            "contains",
            "startswith",
            "endswith",
            "find",
            "index",
            "count",
        ];

        // Known functions that take ownership
        let ownership_functions = ["append", "extend", "insert", "remove", "pop", "sort"];

        if borrowing_functions.contains(&func_name) {
            false
        } else if ownership_functions.contains(&func_name) {
            true
        } else {
            // Conservative default: assume ownership transfer
            true
        }
    }

    /// Check if currently in a loop context
    fn is_in_loop(&self) -> bool {
        self.context_stack
            .iter()
            .any(|ctx| matches!(ctx, AnalysisContext::Loop))
    }

    /// Check if currently in a conditional context
    fn is_in_conditional(&self) -> bool {
        self.context_stack
            .iter()
            .any(|ctx| matches!(ctx, AnalysisContext::Conditional))
    }

    /// Determine optimal borrowing strategies based on usage patterns
    fn determine_strategies(
        &self,
        func: &HirFunction,
        type_mapper: &TypeMapper,
    ) -> BorrowingAnalysisResult {
        let mut strategies = IndexMap::new();
        let mut insights = Vec::new();

        for param in &func.params {
            let usage = self
                .param_usage
                .get(&param.name)
                .cloned()
                .unwrap_or_default();
            let rust_type = type_mapper.map_type(&param.ty);

            let strategy = self.determine_parameter_strategy(
                &param.name,
                &usage,
                &rust_type,
                &param.ty,
                &mut insights,
            );

            strategies.insert(param.name.clone(), strategy);
        }

        BorrowingAnalysisResult {
            param_strategies: strategies,
            insights,
        }
    }

    /// Determine strategy for a single parameter
    fn determine_parameter_strategy(
        &self,
        param_name: &str,
        usage: &ParameterUsagePattern,
        rust_type: &RustType,
        python_type: &PythonType,
        insights: &mut Vec<BorrowingInsight>,
    ) -> BorrowingStrategy {
        // Always check if type is Copy for insights
        if self.is_copy_type(rust_type) {
            insights.push(BorrowingInsight::SuggestCopyDerive(param_name.to_string()));
        }

        // If parameter is moved, we must take ownership
        if usage.is_moved {
            // Check if move is necessary
            if !usage.escapes_through_return && !usage.is_stored {
                insights.push(BorrowingInsight::UnnecessaryMove(param_name.to_string()));
            }
            return BorrowingStrategy::TakeOwnership;
        }

        // If parameter escapes through return and matches return type, take ownership
        // (except for strings which have special handling)
        if usage.escapes_through_return && !matches!(python_type, PythonType::String) {
            if let Some(ref ret_type) = self.return_type {
                if python_type == ret_type {
                    return BorrowingStrategy::TakeOwnership;
                }
            }
        }

        // If parameter is stored in a structure, consider shared ownership
        if usage.is_stored {
            return BorrowingStrategy::UseSharedOwnership {
                is_thread_safe: false,
            };
        }

        // If used in closure, determine capture strategy
        if usage.used_in_closure {
            // Complex analysis needed - for now, be conservative
            return BorrowingStrategy::TakeOwnership;
        }

        // Check if type is Copy - take ownership (cheap)
        if self.is_copy_type(rust_type) {
            return BorrowingStrategy::TakeOwnership; // Cheap to copy
        }

        // String-specific optimizations
        if matches!(python_type, PythonType::String) {
            return self.determine_string_strategy(param_name, usage);
        }

        // Determine mutability needs
        if usage.is_mutated {
            BorrowingStrategy::BorrowMutable { lifetime: None }
        } else if usage.is_read {
            BorrowingStrategy::BorrowImmutable { lifetime: None }
        } else {
            // Parameter unused - take ownership (simplest)
            BorrowingStrategy::TakeOwnership
        }
    }

    /// Determine optimal string handling strategy
    fn determine_string_strategy(
        &self,
        _param_name: &str,
        usage: &ParameterUsagePattern,
    ) -> BorrowingStrategy {
        // For strings that are reassigned (not string mutation itself),
        // we can actually take ownership since we're replacing the entire string
        // This is a Python-specific pattern where `s = s + "!"` creates a new string

        // If string is moved to another function, we need ownership
        if usage.is_moved {
            return BorrowingStrategy::TakeOwnership;
        }

        // If string is reassigned (Python pattern), take ownership
        // This must come before escape check to handle mutation correctly
        if usage.is_mutated {
            return BorrowingStrategy::TakeOwnership;
        }

        // If string escapes, we need to check lifetime requirements
        if usage.escapes_through_return {
            // Use Cow for maximum flexibility
            return BorrowingStrategy::UseCow {
                lifetime: "'static".to_string(),
            };
        }

        // For read-only strings, prefer borrowing
        if usage.is_read && !usage.is_moved && !usage.is_mutated {
            return BorrowingStrategy::BorrowImmutable { lifetime: None };
        }

        // Default to ownership for simplicity
        BorrowingStrategy::TakeOwnership
    }

    /// Check if a type implements Copy
    #[allow(clippy::only_used_in_recursion)]
    fn is_copy_type(&self, rust_type: &RustType) -> bool {
        match rust_type {
            RustType::Primitive(_) => true,
            RustType::Unit => true,
            RustType::Tuple(types) => types.iter().all(|t| self.is_copy_type(t)),
            _ => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hir::{FunctionProperties, HirParam, Literal};
    use depyler_annotations::TranspilationAnnotations;
    use smallvec::smallvec;

    #[test]
    fn test_basic_borrowing_analysis() {
        let mut ctx = BorrowingContext::new(Some(PythonType::Int));
        let type_mapper = TypeMapper::new();

        let func = HirFunction {
            name: "add_one".to_string(),
            params: smallvec![HirParam::new("x".to_string(), PythonType::Int)],
            ret_type: PythonType::Int,
            body: vec![HirStmt::Return(Some(HirExpr::Binary {
                op: crate::hir::BinOp::Add,
                left: Box::new(HirExpr::Var("x".to_string())),
                right: Box::new(HirExpr::Literal(Literal::Int(1))),
            }))],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };

        let result = ctx.analyze_function(&func, &type_mapper);

        // Integer parameter should be taken by value (Copy type)
        let x_strategy = result.param_strategies.get("x").unwrap();
        assert_eq!(*x_strategy, BorrowingStrategy::TakeOwnership);
    }

    #[test]
    fn test_string_borrowing() {
        let mut ctx = BorrowingContext::new(Some(PythonType::Int));
        let type_mapper = TypeMapper::new();

        let func = HirFunction {
            name: "string_len".to_string(),
            params: smallvec![HirParam::new("s".to_string(), PythonType::String)],
            ret_type: PythonType::Int,
            body: vec![HirStmt::Return(Some(HirExpr::Call {
                func: "len".to_string(),
                args: vec![HirExpr::Var("s".to_string())],
            }))],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };

        let result = ctx.analyze_function(&func, &type_mapper);

        // String parameter should be borrowed (not moved)
        let s_strategy = result.param_strategies.get("s").unwrap();
        assert!(matches!(
            s_strategy,
            BorrowingStrategy::BorrowImmutable { .. }
        ));
    }

    #[test]
    fn test_mutation_detection() {
        let mut ctx = BorrowingContext::new(None);
        let type_mapper = TypeMapper::new();

        let func = HirFunction {
            name: "mutate_list".to_string(),
            params: smallvec![HirParam::new(
                "lst".to_string(),
                PythonType::List(Box::new(PythonType::Int))
            )],
            ret_type: PythonType::None,
            body: vec![HirStmt::Expr(HirExpr::Call {
                func: "append".to_string(),
                args: vec![
                    HirExpr::Var("lst".to_string()),
                    HirExpr::Literal(Literal::Int(42)),
                ],
            })],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };

        let result = ctx.analyze_function(&func, &type_mapper);

        // List parameter should be moved (append takes ownership in our analysis)
        let lst_strategy = result.param_strategies.get("lst").unwrap();
        assert_eq!(*lst_strategy, BorrowingStrategy::TakeOwnership);
    }
}