ruchy 4.1.2

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
//! Function Parameter Type Inference
//!
//! This module handles inference of parameter and return types for functions
//! based on how parameters are used in function bodies.
//!
//! **EXTREME TDD Round 70**: Extracted from statements.rs for modularization.

#![allow(clippy::doc_markdown)]

use super::Transpiler;
use crate::frontend::ast::{Expr, ExprKind, Param, TypeKind};
use anyhow::Result;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::collections::HashSet;

impl Transpiler {
    /// Infer return type from parameter types
    /// Delegates to return_type_helpers module
    pub(crate) fn infer_return_type_from_params_impl(
        &self,
        body: &Expr,
        params: &[Param],
    ) -> Result<Option<TokenStream>> {
        super::return_type_helpers::infer_return_type_from_params(body, params, |ty| {
            self.transpile_type(ty)
        })
    }

    /// Infer parameter type based on usage in function body
    /// Complexity: 9 (within Toyota Way limits)
    pub(crate) fn infer_param_type_impl(
        &self,
        param: &Param,
        body: &Expr,
        func_name: &str,
    ) -> TokenStream {
        self.infer_param_type_with_index(param, body, func_name, None)
    }

    /// BOOK-COMPAT-017: Infer parameter type with optional parameter index for call-site lookup
    pub(crate) fn infer_param_type_with_index(
        &self,
        param: &Param,
        body: &Expr,
        func_name: &str,
        param_index: Option<usize>,
    ) -> TokenStream {
        use super::type_inference::{
            infer_param_type_from_builtin_usage, is_param_used_as_array, is_param_used_as_bool,
            is_param_used_as_function, is_param_used_as_index, is_param_used_in_print_macro,
            is_param_used_in_string_concat, is_param_used_numerically, is_param_used_with_len,
        };

        // BOOK-COMPAT-017: Check call-site types first for more accurate inference
        if let Some(idx) = param_index {
            if let Some(call_site_type) = self.get_call_site_param_type(func_name, idx) {
                if call_site_type != "_" {
                    match call_site_type.as_str() {
                        "f64" => return quote! { f64 },
                        "f32" => return quote! { f32 },
                        "i64" => return quote! { i64 },
                        "i32" => return quote! { i32 },
                        "String" => return quote! { String },
                        "bool" => return quote! { bool },
                        t if t.starts_with("Vec<") => {
                            // Parse Vec<T> and generate proper type
                            let inner = &t[4..t.len() - 1];
                            let inner_ident = format_ident!("{}", inner);
                            return quote! { Vec<#inner_ident> };
                        }
                        _ => {} // Fall through to body-based inference
                    }
                }
            }
        }

        // Check for function parameters first (higher-order functions)
        if is_param_used_as_function(&param.name(), body) {
            return quote! { impl Fn(i32) -> i32 };
        }

        // ISSUE-114 FIX: Check if parameter is used as boolean condition
        if is_param_used_as_bool(&param.name(), body) {
            return quote! { bool };
        }

        // Check if parameter is used as an array (indexed)
        if is_param_used_as_array(&param.name(), body) {
            if self.is_nested_array_param_impl(&param.name(), body) {
                return quote! { &Vec<Vec<i32>> };
            }
            return quote! { &Vec<i32> };
        }

        // Check if parameter is used with len()
        if is_param_used_with_len(&param.name(), body) {
            if self.is_nested_array_param_impl(&param.name(), body) {
                return quote! { &Vec<Vec<i32>> };
            }
            return quote! { &Vec<i32> };
        }

        // Check if parameter is used as an index
        if is_param_used_as_index(&param.name(), body) {
            return quote! { i32 };
        }

        // Check if used numerically - but check call-site first for float inference
        if is_param_used_numerically(&param.name(), body)
            || super::function_analysis::looks_like_numeric_function(func_name)
        {
            // BOOK-COMPAT-017: If no call-site type, default to i32
            return quote! { i32 };
        }

        // Check built-in function signatures for string-specific operations
        if let Some(type_hint) = infer_param_type_from_builtin_usage(&param.name(), body) {
            if type_hint == "&str" {
                return quote! { &str };
            }
        }

        // Check if parameter is used in string concatenation
        if is_param_used_in_string_concat(&param.name(), body) {
            return quote! { &str };
        }

        // Check if parameter is used in print/format macros
        if is_param_used_in_print_macro(&param.name(), body) {
            return quote! { &str };
        }

        // Default to i32 for unused/untyped parameters
        quote! { i32 }
    }

    /// Helper to detect nested array access (2D arrays)
    /// Complexity: 1 (within Toyota Way limits)
    pub(crate) fn is_nested_array_param_impl(&self, param_name: &str, expr: &Expr) -> bool {
        Self::find_nested_array_access_impl(param_name, expr, &mut HashSet::new())
    }

    /// Internal helper with visited tracking to prevent infinite recursion
    /// Complexity: 9 (within Toyota Way limits)
    fn find_nested_array_access_impl(
        param_name: &str,
        expr: &Expr,
        visited: &mut HashSet<usize>,
    ) -> bool {
        let expr_addr = std::ptr::from_ref(expr) as usize;
        if visited.contains(&expr_addr) {
            return false;
        }
        visited.insert(expr_addr);

        match &expr.kind {
            // Direct nested indexing: param[i][j]
            ExprKind::IndexAccess { object, .. } => {
                if let ExprKind::IndexAccess { object: inner, .. } = &object.kind {
                    if let ExprKind::Identifier(name) = &inner.kind {
                        if name == param_name {
                            return true;
                        }
                    }
                }
                Self::find_nested_array_access_impl(param_name, object, visited)
            }
            // Recurse into block expressions
            ExprKind::Block(exprs) => exprs
                .iter()
                .any(|e| Self::find_nested_array_access_impl(param_name, e, visited)),
            // Let bindings
            ExprKind::Let { value, body, .. } | ExprKind::LetPattern { value, body, .. } => {
                Self::find_nested_array_access_impl(param_name, value, visited)
                    || Self::find_nested_array_access_impl(param_name, body, visited)
            }
            // Binary operations
            ExprKind::Binary { left, right, .. } => {
                Self::find_nested_array_access_impl(param_name, left, visited)
                    || Self::find_nested_array_access_impl(param_name, right, visited)
            }
            // While loops
            ExprKind::While {
                condition, body, ..
            } => {
                Self::find_nested_array_access_impl(param_name, condition, visited)
                    || Self::find_nested_array_access_impl(param_name, body, visited)
            }
            // If expressions
            ExprKind::If {
                condition,
                then_branch,
                else_branch,
            } => {
                Self::find_nested_array_access_impl(param_name, condition, visited)
                    || Self::find_nested_array_access_impl(param_name, then_branch, visited)
                    || else_branch.as_ref().is_some_and(|e| {
                        Self::find_nested_array_access_impl(param_name, e, visited)
                    })
            }
            // Assignments
            ExprKind::Assign { target, value } | ExprKind::CompoundAssign { target, value, .. } => {
                Self::find_nested_array_access_impl(param_name, target, visited)
                    || Self::find_nested_array_access_impl(param_name, value, visited)
            }
            _ => false,
        }
    }

    /// Generate parameter tokens with proper type inference
    /// Complexity: 5 (within Toyota Way limits)
    pub(crate) fn generate_param_tokens_impl(
        &self,
        params: &[Param],
        body: &Expr,
        func_name: &str,
    ) -> Result<Vec<TokenStream>> {
        params
            .iter()
            .enumerate()
            .map(|(idx, p)| {
                let param_name = format_ident!("{}", p.name());

                // Handle special Rust receiver syntax (&self, &mut self, self)
                if p.name() == "self" {
                    if let TypeKind::Reference { is_mut, .. } = &p.ty.kind {
                        if *is_mut {
                            return Ok(quote! { &mut self });
                        }
                        return Ok(quote! { &self });
                    }
                    return Ok(quote! { self });
                }

                // Regular parameter handling
                // BOOK-COMPAT-017: Pass parameter index for call-site type lookup
                let type_tokens = if let Ok(tokens) = self.transpile_type(&p.ty) {
                    let token_str = tokens.to_string();
                    if token_str == "_" {
                        self.infer_param_type_with_index(p, body, func_name, Some(idx))
                    } else {
                        tokens
                    }
                } else {
                    self.infer_param_type_with_index(p, body, func_name, Some(idx))
                };

                // Preserve mut keyword for mutable parameters
                if p.is_mutable {
                    Ok(quote! { mut #param_name: #type_tokens })
                } else {
                    Ok(quote! { #param_name: #type_tokens })
                }
            })
            .collect()
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::frontend::ast::{Literal, Span, Type};

    fn make_transpiler() -> Transpiler {
        Transpiler::new()
    }

    fn make_expr(kind: ExprKind) -> Expr {
        Expr {
            kind,
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    fn int_expr(n: i64) -> Expr {
        make_expr(ExprKind::Literal(Literal::Integer(n, None)))
    }

    fn ident_expr(name: &str) -> Expr {
        make_expr(ExprKind::Identifier(name.to_string()))
    }

    fn make_param(name: &str) -> Param {
        Param {
            pattern: crate::frontend::ast::Pattern::Identifier(name.to_string()),
            ty: Type {
                kind: crate::frontend::ast::TypeKind::Named("_".to_string()),
                span: Span::default(),
            },
            span: Span::default(),
            is_mutable: false,
            default_value: None,
        }
    }

    fn make_mut_param(name: &str) -> Param {
        Param {
            pattern: crate::frontend::ast::Pattern::Identifier(name.to_string()),
            ty: Type {
                kind: crate::frontend::ast::TypeKind::Named("_".to_string()),
                span: Span::default(),
            },
            span: Span::default(),
            is_mutable: true,
            default_value: None,
        }
    }

    // ========================================================================
    // infer_param_type_impl tests
    // ========================================================================

    #[test]
    fn test_infer_param_type_numeric_function() {
        let transpiler = make_transpiler();
        let param = make_param("x");
        let body = int_expr(42);
        let result = transpiler.infer_param_type_impl(&param, &body, "add");
        assert_eq!(result.to_string(), "i32");
    }

    #[test]
    fn test_infer_param_type_bool_condition() {
        let transpiler = make_transpiler();
        let param = make_param("flag");
        // if flag { 1 } else { 0 }
        let body = make_expr(ExprKind::If {
            condition: Box::new(ident_expr("flag")),
            then_branch: Box::new(int_expr(1)),
            else_branch: Some(Box::new(int_expr(0))),
        });
        let result = transpiler.infer_param_type_impl(&param, &body, "check");
        assert_eq!(result.to_string(), "bool");
    }

    #[test]
    fn test_infer_param_type_array_indexing() {
        let transpiler = make_transpiler();
        let param = make_param("arr");
        // arr[0]
        let body = make_expr(ExprKind::IndexAccess {
            object: Box::new(ident_expr("arr")),
            index: Box::new(int_expr(0)),
        });
        let result = transpiler.infer_param_type_impl(&param, &body, "get_first");
        assert_eq!(result.to_string(), "& Vec < i32 >");
    }

    #[test]
    fn test_infer_param_type_nested_array() {
        let transpiler = make_transpiler();
        let param = make_param("matrix");
        // matrix[0][1]
        let body = make_expr(ExprKind::IndexAccess {
            object: Box::new(make_expr(ExprKind::IndexAccess {
                object: Box::new(ident_expr("matrix")),
                index: Box::new(int_expr(0)),
            })),
            index: Box::new(int_expr(1)),
        });
        let result = transpiler.infer_param_type_impl(&param, &body, "get_element");
        let result_str = result.to_string();
        // Normalize whitespace for comparison
        assert!(result_str.contains("Vec") && result_str.contains("i32"));
        assert!(result_str.starts_with("&"));
    }

    #[test]
    fn test_infer_param_type_index_usage() {
        let transpiler = make_transpiler();
        let param = make_param("i");
        // arr[i]
        let body = make_expr(ExprKind::IndexAccess {
            object: Box::new(ident_expr("arr")),
            index: Box::new(ident_expr("i")),
        });
        let result = transpiler.infer_param_type_impl(&param, &body, "access");
        assert_eq!(result.to_string(), "i32");
    }

    #[test]
    fn test_infer_param_type_default() {
        let transpiler = make_transpiler();
        let param = make_param("unused");
        let body = int_expr(42);
        let result = transpiler.infer_param_type_impl(&param, &body, "foo");
        assert_eq!(result.to_string(), "i32");
    }

    // ========================================================================
    // is_nested_array_param_impl tests
    // ========================================================================

    #[test]
    fn test_is_nested_array_simple() {
        let transpiler = make_transpiler();
        let body = make_expr(ExprKind::IndexAccess {
            object: Box::new(ident_expr("arr")),
            index: Box::new(int_expr(0)),
        });
        assert!(!transpiler.is_nested_array_param_impl("arr", &body));
    }

    #[test]
    fn test_is_nested_array_2d() {
        let transpiler = make_transpiler();
        let body = make_expr(ExprKind::IndexAccess {
            object: Box::new(make_expr(ExprKind::IndexAccess {
                object: Box::new(ident_expr("matrix")),
                index: Box::new(int_expr(0)),
            })),
            index: Box::new(int_expr(1)),
        });
        assert!(transpiler.is_nested_array_param_impl("matrix", &body));
    }

    #[test]
    fn test_is_nested_array_in_block() {
        let transpiler = make_transpiler();
        let body = make_expr(ExprKind::Block(vec![make_expr(ExprKind::IndexAccess {
            object: Box::new(make_expr(ExprKind::IndexAccess {
                object: Box::new(ident_expr("m")),
                index: Box::new(ident_expr("i")),
            })),
            index: Box::new(ident_expr("j")),
        })]));
        assert!(transpiler.is_nested_array_param_impl("m", &body));
    }

    #[test]
    fn test_is_nested_array_in_if() {
        let transpiler = make_transpiler();
        let nested_access = make_expr(ExprKind::IndexAccess {
            object: Box::new(make_expr(ExprKind::IndexAccess {
                object: Box::new(ident_expr("grid")),
                index: Box::new(int_expr(0)),
            })),
            index: Box::new(int_expr(0)),
        });
        let body = make_expr(ExprKind::If {
            condition: Box::new(make_expr(ExprKind::Literal(Literal::Bool(true)))),
            then_branch: Box::new(nested_access),
            else_branch: None,
        });
        assert!(transpiler.is_nested_array_param_impl("grid", &body));
    }

    #[test]
    fn test_is_nested_array_different_param() {
        let transpiler = make_transpiler();
        let body = make_expr(ExprKind::IndexAccess {
            object: Box::new(make_expr(ExprKind::IndexAccess {
                object: Box::new(ident_expr("other")),
                index: Box::new(int_expr(0)),
            })),
            index: Box::new(int_expr(1)),
        });
        assert!(!transpiler.is_nested_array_param_impl("matrix", &body));
    }

    // ========================================================================
    // generate_param_tokens_impl tests
    // ========================================================================

    #[test]
    fn test_generate_param_tokens_simple() {
        let transpiler = make_transpiler();
        let params = vec![make_param("x")];
        let body = int_expr(42);
        let result = transpiler
            .generate_param_tokens_impl(&params, &body, "add")
            .unwrap();
        assert_eq!(result.len(), 1);
        assert!(result[0].to_string().contains("x"));
    }

    #[test]
    fn test_generate_param_tokens_mutable() {
        let transpiler = make_transpiler();
        let params = vec![make_mut_param("count")];
        let body = int_expr(0);
        let result = transpiler
            .generate_param_tokens_impl(&params, &body, "increment")
            .unwrap();
        assert!(result[0].to_string().contains("mut"));
        assert!(result[0].to_string().contains("count"));
    }

    #[test]
    fn test_generate_param_tokens_multiple() {
        let transpiler = make_transpiler();
        let params = vec![make_param("a"), make_param("b")];
        let body = int_expr(0);
        let result = transpiler
            .generate_param_tokens_impl(&params, &body, "add")
            .unwrap();
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_generate_param_tokens_self_ref() {
        let transpiler = make_transpiler();
        let mut param = make_param("self");
        param.ty = Type {
            kind: TypeKind::Reference {
                is_mut: false,
                lifetime: None,
                inner: Box::new(Type {
                    kind: crate::frontend::ast::TypeKind::Named("Self".to_string()),
                    span: Span::default(),
                }),
            },
            span: Span::default(),
        };
        let params = vec![param];
        let body = int_expr(0);
        let result = transpiler
            .generate_param_tokens_impl(&params, &body, "method")
            .unwrap();
        assert_eq!(result[0].to_string(), "& self");
    }

    #[test]
    fn test_generate_param_tokens_self_mut() {
        let transpiler = make_transpiler();
        let mut param = make_param("self");
        param.ty = Type {
            kind: TypeKind::Reference {
                is_mut: true,
                lifetime: None,
                inner: Box::new(Type {
                    kind: crate::frontend::ast::TypeKind::Named("Self".to_string()),
                    span: Span::default(),
                }),
            },
            span: Span::default(),
        };
        let params = vec![param];
        let body = int_expr(0);
        let result = transpiler
            .generate_param_tokens_impl(&params, &body, "method")
            .unwrap();
        assert_eq!(result[0].to_string(), "& mut self");
    }

    #[test]
    fn test_generate_param_tokens_self_owned() {
        let transpiler = make_transpiler();
        let param = make_param("self");
        let params = vec![param];
        let body = int_expr(0);
        let result = transpiler
            .generate_param_tokens_impl(&params, &body, "consume")
            .unwrap();
        assert_eq!(result[0].to_string(), "self");
    }

    // ========================================================================
    // infer_return_type_from_params tests
    // ========================================================================

    #[test]
    fn test_infer_return_type_no_params() {
        let transpiler = make_transpiler();
        let body = int_expr(42);
        let result = transpiler
            .infer_return_type_from_params_impl(&body, &[])
            .unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_infer_return_type_with_params() {
        let transpiler = make_transpiler();
        let params = vec![make_param("x")];
        // Return the parameter directly
        let body = ident_expr("x");
        let result = transpiler
            .infer_return_type_from_params_impl(&body, &params)
            .unwrap();
        // Should infer return type from parameter type if parameter is returned
        // This depends on implementation details in return_type_helpers
        // The test validates the function runs without error
        assert!(result.is_none() || result.is_some());
    }
}