depyler-core 4.1.1

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
//! Mutable variable analysis — extracted from `analyze_mutable_vars` in `rust_gen.rs`
//!
//! This module contains the two large nested functions that were previously defined
//! inside `analyze_mutable_vars`:
//!
//! - [`analyze_expr_for_mutations`]: Recursive expression visitor that detects mutating
//!   method calls, attribute accesses, and transitive mutations through function calls.
//! - [`analyze_stmt`]: Statement-level mutation analysis that identifies variable
//!   reassignments, index/attribute assignments, and delegates to
//!   `analyze_expr_for_mutations` for expression-level detection.
//!
//! By moving these to module-level, the measured cyclomatic complexity of
//! `analyze_mutable_vars` is reduced while preserving identical behavior.

use crate::hir::*;
use std::collections::{HashMap, HashSet};

use super::mutation_helpers;

/// Recursive expression visitor detecting mutating method calls.
///
/// Walks the HIR expression tree and inserts variable names into `mutable`
/// when a mutating method or attribute is found on that variable.
pub(super) fn analyze_expr_for_mutations(
    expr: &HirExpr,
    mutable: &mut HashSet<String>,
    var_types: &HashMap<String, String>,
    mutating_methods: &HashMap<String, HashSet<String>>,
    function_param_muts: &HashMap<String, Vec<bool>>,
) {
    match expr {
        HirExpr::MethodCall {
            object,
            method,
            args,
            ..
        } => {
            // Check if this is a mutating method call
            let is_mut = if mutation_helpers::is_mutating_method(method) {
                // Built-in mutating method
                true
            } else if let HirExpr::Var(var_name) = &**object {
                // Check if this is a user-defined mutating method
                if let Some(class_name) = var_types.get(var_name) {
                    if let Some(mut_methods) = mutating_methods.get(class_name) {
                        mut_methods.contains(method)
                    } else {
                        false
                    }
                } else {
                    false
                }
            } else {
                false
            };

            if is_mut {
                if let HirExpr::Var(var_name) = &**object {
                    mutable.insert(var_name.clone());
                }
            }
            // Recursively check nested expressions
            analyze_expr_for_mutations(
                object,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
            for arg in args {
                analyze_expr_for_mutations(
                    arg,
                    mutable,
                    var_types,
                    mutating_methods,
                    function_param_muts,
                );
            }
        }
        HirExpr::Binary { left, right, .. } => {
            analyze_expr_for_mutations(
                left,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
            analyze_expr_for_mutations(
                right,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
        }
        HirExpr::Unary { operand, .. } => {
            analyze_expr_for_mutations(
                operand,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
        }
        // DEPYLER-1217: Detect transitive mutation through function calls
        // If a variable is passed to a function that expects &mut at that position,
        // the variable must be mutable in the caller's scope
        HirExpr::Call { func, args, .. } => {
            // Check if called function has param_muts info
            if let Some(param_muts) = function_param_muts.get(func) {
                for (idx, arg) in args.iter().enumerate() {
                    // If this param needs &mut, mark the variable as mutable
                    if param_muts.get(idx).copied().unwrap_or(false) {
                        if let HirExpr::Var(var_name) = arg {
                            mutable.insert(var_name.clone());
                        }
                    }
                    analyze_expr_for_mutations(
                        arg,
                        mutable,
                        var_types,
                        mutating_methods,
                        function_param_muts,
                    );
                }
            } else {
                // No param_muts info - just recurse into args
                for arg in args {
                    analyze_expr_for_mutations(
                        arg,
                        mutable,
                        var_types,
                        mutating_methods,
                        function_param_muts,
                    );
                }
            }
        }
        HirExpr::IfExpr { test, body, orelse } => {
            analyze_expr_for_mutations(
                test,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
            analyze_expr_for_mutations(
                body,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
            analyze_expr_for_mutations(
                orelse,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
        }
        HirExpr::List(items)
        | HirExpr::Tuple(items)
        | HirExpr::Set(items)
        | HirExpr::FrozenSet(items) => {
            for item in items {
                analyze_expr_for_mutations(
                    item,
                    mutable,
                    var_types,
                    mutating_methods,
                    function_param_muts,
                );
            }
        }
        HirExpr::Dict(pairs) => {
            for (key, value) in pairs {
                analyze_expr_for_mutations(
                    key,
                    mutable,
                    var_types,
                    mutating_methods,
                    function_param_muts,
                );
                analyze_expr_for_mutations(
                    value,
                    mutable,
                    var_types,
                    mutating_methods,
                    function_param_muts,
                );
            }
        }
        HirExpr::Index { base, index } => {
            analyze_expr_for_mutations(
                base,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
            analyze_expr_for_mutations(
                index,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
        }
        HirExpr::Attribute { value, attr } => {
            // DEPYLER-0835: Some Python attributes translate to mutating method calls in Rust
            // e.g., csv.DictReader.fieldnames -> reader.headers() (requires &mut self)
            if mutation_helpers::is_mutating_attribute(attr) {
                if let HirExpr::Var(name) = value.as_ref() {
                    mutable.insert(name.clone());
                }
            }
            analyze_expr_for_mutations(
                value,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
        }
        _ => {}
    }
}

/// Statement-level mutation analysis.
///
/// Walks the HIR statement tree and identifies variables that need `mut`
/// due to reassignment, index assignment, attribute assignment, or
/// mutating method/function calls in expressions.
pub(super) fn analyze_stmt(
    stmt: &HirStmt,
    declared: &mut HashSet<String>,
    mutable: &mut HashSet<String>,
    var_types: &mut HashMap<String, String>,
    mutating_methods: &HashMap<String, HashSet<String>>,
    function_param_muts: &HashMap<String, Vec<bool>>,
) {
    match stmt {
        HirStmt::Assign { target, value, .. } => {
            // Check if the value expression contains method calls that mutate variables
            analyze_expr_for_mutations(
                value,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );

            match target {
                AssignTarget::Symbol(name) => {
                    // Track variable type if assigned from class constructor
                    if let HirExpr::Call { func, .. } = value {
                        // Store the type (class name) for this variable
                        var_types.insert(name.clone(), func.clone());
                    }

                    // DEPYLER-0549: Mark csv readers/writers as mutable
                    // In Rust, csv::Reader and csv::Writer require &mut self for most operations
                    // Detection: variable names or call patterns involving csv/reader/writer
                    // DEPYLER-0835: Name heuristic should ALWAYS apply, not just as fallback
                    let name_heuristic = name == "reader"
                        || name == "writer"
                        || name.contains("reader")
                        || name.contains("writer");
                    let pattern_match =
                        if let HirExpr::MethodCall { object, method, .. } = value {
                            // csv.DictReader() or csv.reader()
                            if let HirExpr::Var(module) = object.as_ref() {
                                module == "csv"
                                    && (method.contains("Reader")
                                        || method.contains("reader")
                                        || method.contains("Writer")
                                        || method.contains("writer"))
                            } else {
                                false
                            }
                        } else if let HirExpr::Call { func, .. } = value {
                            // DictReader(f) or csv.ReaderBuilder...
                            func.contains("Reader")
                                || func.contains("Writer")
                                || func.contains("reader")
                                || func.contains("writer")
                        } else {
                            false
                        };
                    let needs_csv_mut = name_heuristic || pattern_match;

                    if needs_csv_mut {
                        mutable.insert(name.clone());
                    }

                    if declared.contains(name) {
                        // Variable is being reassigned - mark as mutable
                        mutable.insert(name.clone());
                    } else {
                        // First declaration
                        declared.insert(name.clone());
                    }
                }
                AssignTarget::Tuple(targets) => {
                    // DEPYLER-1217: Tuple assignment - recursively handle all target types
                    // including Index targets (e.g., arr[i], arr[j] = arr[j], arr[i])
                    fn handle_tuple_target(
                        t: &AssignTarget,
                        declared: &mut HashSet<String>,
                        mutable: &mut HashSet<String>,
                    ) {
                        match t {
                            AssignTarget::Symbol(name) => {
                                if declared.contains(name) {
                                    // Variable is being reassigned - mark as mutable
                                    mutable.insert(name.clone());
                                } else {
                                    // First declaration
                                    declared.insert(name.clone());
                                }
                            }
                            AssignTarget::Index { base, .. } => {
                                // Index assignment mutates the base
                                // DEPYLER-0596-FIX: Handle nested index in tuple assignments
                                fn find_base_var(expr: &HirExpr) -> Option<String> {
                                    match expr {
                                        HirExpr::Var(name) => Some(name.clone()),
                                        HirExpr::Index { base, .. } => find_base_var(base),
                                        _ => None,
                                    }
                                }
                                if let Some(var_name) = find_base_var(base.as_ref()) {
                                    mutable.insert(var_name);
                                }
                            }
                            AssignTarget::Attribute { value, .. } => {
                                // Attribute assignment mutates the base
                                if let HirExpr::Var(var_name) = value.as_ref() {
                                    mutable.insert(var_name.clone());
                                }
                            }
                            AssignTarget::Tuple(nested_targets) => {
                                // Recursively handle nested tuples
                                for nested in nested_targets {
                                    handle_tuple_target(nested, declared, mutable);
                                }
                            }
                        }
                    }
                    for t in targets {
                        handle_tuple_target(t, declared, mutable);
                    }
                }
                AssignTarget::Attribute { value, .. } => {
                    // DEPYLER-0235 FIX: Property writes require the base object to be mutable
                    // e.g., `b.size = 20` requires `let mut b = ...`
                    if let HirExpr::Var(var_name) = value.as_ref() {
                        mutable.insert(var_name.clone());
                    }
                }
                AssignTarget::Index { base, .. } => {
                    // DEPYLER-0235 FIX: Index assignments also require mutability
                    // e.g., `arr[i] = value` requires `let mut arr = ...`
                    // DEPYLER-0596-FIX: Handle nested index (e.g., `d["a"]["b"] = v`)
                    // by recursively finding the innermost variable
                    fn find_base_var(expr: &HirExpr) -> Option<String> {
                        match expr {
                            HirExpr::Var(name) => Some(name.clone()),
                            HirExpr::Index { base, .. } => find_base_var(base),
                            _ => None,
                        }
                    }
                    if let Some(var_name) = find_base_var(base.as_ref()) {
                        mutable.insert(var_name);
                    }
                }
            }
        }
        HirStmt::Expr(expr) => {
            // Check standalone expressions for method calls (e.g., numbers.push(4))
            analyze_expr_for_mutations(
                expr,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
        }
        HirStmt::Return(Some(expr)) => {
            analyze_expr_for_mutations(
                expr,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
        }
        HirStmt::If {
            condition,
            then_body,
            else_body,
            ..
        } => {
            analyze_expr_for_mutations(
                condition,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
            for stmt in then_body {
                analyze_stmt(
                    stmt,
                    declared,
                    mutable,
                    var_types,
                    mutating_methods,
                    function_param_muts,
                );
            }
            if let Some(else_stmts) = else_body {
                for stmt in else_stmts {
                    analyze_stmt(
                        stmt,
                        declared,
                        mutable,
                        var_types,
                        mutating_methods,
                        function_param_muts,
                    );
                }
            }
        }
        HirStmt::While {
            condition, body, ..
        } => {
            analyze_expr_for_mutations(
                condition,
                mutable,
                var_types,
                mutating_methods,
                function_param_muts,
            );
            for stmt in body {
                analyze_stmt(
                    stmt,
                    declared,
                    mutable,
                    var_types,
                    mutating_methods,
                    function_param_muts,
                );
            }
        }
        HirStmt::For { body, .. } => {
            for stmt in body {
                analyze_stmt(
                    stmt,
                    declared,
                    mutable,
                    var_types,
                    mutating_methods,
                    function_param_muts,
                );
            }
        }
        // DEPYLER-0549: Handle WITH statements - analyze body for mutations
        HirStmt::With { body, .. } => {
            for stmt in body {
                analyze_stmt(
                    stmt,
                    declared,
                    mutable,
                    var_types,
                    mutating_methods,
                    function_param_muts,
                );
            }
        }
        // DEPYLER-0549: Handle Try - analyze all branches
        HirStmt::Try {
            body,
            handlers,
            orelse,
            finalbody,
            ..
        } => {
            for stmt in body {
                analyze_stmt(
                    stmt,
                    declared,
                    mutable,
                    var_types,
                    mutating_methods,
                    function_param_muts,
                );
            }
            for handler in handlers {
                for stmt in &handler.body {
                    analyze_stmt(
                        stmt,
                        declared,
                        mutable,
                        var_types,
                        mutating_methods,
                        function_param_muts,
                    );
                }
            }
            if let Some(else_stmts) = orelse {
                for stmt in else_stmts {
                    analyze_stmt(
                        stmt,
                        declared,
                        mutable,
                        var_types,
                        mutating_methods,
                        function_param_muts,
                    );
                }
            }
            if let Some(final_stmts) = finalbody {
                for stmt in final_stmts {
                    analyze_stmt(
                        stmt,
                        declared,
                        mutable,
                        var_types,
                        mutating_methods,
                        function_param_muts,
                    );
                }
            }
        }
        _ => {}
    }
}