ruchy 4.2.1

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
//! Function Call Helper Transpilation
//!
//! This module handles transpilation of function call helpers:
//! - Result/Option call handling (Ok, Err, Some)
//! - Regular function call transpilation with string coercion
//! - String type coercion for function arguments
//!
//! **EXTREME TDD Round 63**: Extracted from statements.rs for modularization.

use super::Transpiler;
use crate::frontend::ast::Expr;
use anyhow::Result;
use proc_macro2::TokenStream;
use quote::quote;

impl Transpiler {
    /// Transpile Ok/Err/Some calls with automatic string conversion
    ///
    /// DEFECT-STRING-RESULT FIX: When Ok/Err/Some are parsed as Call expressions
    /// (e.g., in return positions), convert string literals to String.
    ///
    /// This complements the `ExprKind::Ok/Err/Some` handlers in dispatcher.rs.
    /// Complexity: 5 (within Toyota Way limits)
    pub fn try_transpile_result_call(
        &self,
        base_name: &str,
        args: &[Expr],
    ) -> Result<Option<TokenStream>> {
        use crate::frontend::ast::{ExprKind, Literal};

        // Only handle Ok, Err, and Some constructors
        if base_name != "Ok" && base_name != "Err" && base_name != "Some" {
            return Ok(None);
        }

        // Transpile all arguments, converting string literals to String
        let arg_tokens: Result<Vec<_>> = args
            .iter()
            .map(|arg| {
                let base_tokens = self.transpile_expr(arg)?;
                // Convert string literals to String for Result/Option type compatibility
                match &arg.kind {
                    ExprKind::Literal(Literal::String(_)) => {
                        Ok(quote! { #base_tokens.to_string() })
                    }
                    _ => Ok(base_tokens),
                }
            })
            .collect();

        let arg_tokens = arg_tokens?;
        let func_ident = proc_macro2::Ident::new(base_name, proc_macro2::Span::call_site());

        Ok(Some(quote! { #func_ident(#(#arg_tokens),*) }))
    }

    /// Handle regular function calls with string literal conversion
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::{Transpiler, Parser};
    ///
    /// let mut transpiler = Transpiler::new();
    /// let mut parser = Parser::new(r#"my_func("test")"#);
    /// let ast = parser.parse().expect("Failed to parse");
    /// let result = transpiler.transpile(&ast).expect("transpile should succeed in test").to_string();
    /// assert!(result.contains("my_func"));
    /// ```
    /// Complexity: 8 (within Toyota Way limits)
    pub fn transpile_regular_function_call(
        &self,
        func_tokens: &TokenStream,
        args: &[Expr],
    ) -> Result<TokenStream> {
        // Get function name for signature lookup
        let func_name = func_tokens.to_string().trim().to_string();
        // Apply type coercion based on function signature
        let arg_tokens: Result<Vec<_>> = if let Some(signature) =
            self.function_signatures.get(&func_name)
        {
            args.iter()
                .enumerate()
                .map(|(i, arg)| {
                    let mut base_tokens = self.transpile_expr(arg)?;

                    // Apply String/&str coercion if needed
                    if let Some(expected_type) = signature.param_types.get(i) {
                        // DEFECT-018 FIX: For Identifier args in loops, use .to_string()
                        // for String params (handles &str->String), or .clone() for others
                        if self.in_loop_context.get()
                            && matches!(&arg.kind, crate::frontend::ast::ExprKind::Identifier(_))
                        {
                            if expected_type == "String" {
                                // Use .to_string() which handles both &str and String
                                return Ok(quote! { #base_tokens.to_string() });
                            }
                            base_tokens = quote! { #base_tokens.clone() };
                        }
                        self.apply_string_coercion(arg, &base_tokens, expected_type)
                    } else {
                        // DEFECT-018 FIX: Auto-clone Identifier arguments in loop contexts
                        // to prevent "use of moved value" errors on subsequent iterations
                        if self.in_loop_context.get()
                            && matches!(&arg.kind, crate::frontend::ast::ExprKind::Identifier(_))
                        {
                            base_tokens = quote! { #base_tokens.clone() };
                        }
                        Ok(base_tokens)
                    }
                })
                .collect()
        } else {
            // No signature info - transpile with type conversions
            args.iter()
                .map(|arg| {
                    let mut base_tokens = self.transpile_expr(arg)?;

                    // BOOK-COMPAT-017: String literals passed to functions should convert to String
                    // Most functions with string params need String not &str
                    if matches!(&arg.kind, crate::frontend::ast::ExprKind::Literal(
                        crate::frontend::ast::Literal::String(_)
                    )) {
                        return Ok(quote! { #base_tokens.to_string() });
                    }

                    // BOOK-COMPAT-017: Array literals passed to functions should convert to Vec
                    // Most functions with untyped params expect Vec<T> semantics
                    if let crate::frontend::ast::ExprKind::List(elements) = &arg.kind {
                        if !elements.is_empty() {
                            return Ok(quote! { #base_tokens.to_vec() });
                        }
                    }

                    // DEFECT-018 FIX: Auto-clone Identifier arguments in loop contexts
                    if self.in_loop_context.get()
                        && matches!(&arg.kind, crate::frontend::ast::ExprKind::Identifier(_))
                    {
                        base_tokens = quote! { #base_tokens.clone() };
                    }

                    Ok(base_tokens)
                })
                .collect()
        };
        let arg_tokens = arg_tokens?;
        Ok(quote! { #func_tokens(#(#arg_tokens),*) })
    }

    /// Apply type coercion based on expected type
    /// Complexity: 6 (within Toyota Way limits)
    pub fn apply_string_coercion(
        &self,
        arg: &Expr,
        tokens: &TokenStream,
        expected_type: &str,
    ) -> Result<TokenStream> {
        use crate::frontend::ast::{ExprKind, Literal};
        match (&arg.kind, expected_type) {
            // String literal to String parameter: add .to_string()
            (ExprKind::Literal(Literal::String(_)), "String") => Ok(quote! { #tokens.to_string() }),
            // BOOK-COMPAT-017: String literal to Any/Unknown parameter (inferred as String from body)
            // String operations in function body typically infer String type, so convert
            (ExprKind::Literal(Literal::String(_)), "Any" | "Unknown") => {
                Ok(quote! { #tokens.to_string() })
            }
            // BOOK-COMPAT-017: Array literal to Any parameter (function with inferred Vec<T> type)
            // Convert array [1, 2, 3] to vec via .to_vec() for functions expecting Vec
            // But NOT when expected type is explicit array type [T; N]
            (ExprKind::List(elements), "Any" | "Unknown") if !elements.is_empty() => {
                Ok(quote! { #tokens.to_vec() })
            }
            // Array literal to explicit array type [T; N]: keep as-is
            (ExprKind::List(_), expected) if expected.starts_with('[') && expected.contains(';') => {
                Ok(tokens.clone())
            }
            // String literal to &str parameter: keep as-is
            (ExprKind::Literal(Literal::String(_)), expected) if expected.starts_with('&') => {
                Ok(tokens.clone())
            }
            // Variable that might be &str to String parameter
            (ExprKind::Identifier(_), "String") => {
                // DEFECT-018 FIX: Use .to_string() which handles both:
                // - &str -> String (converts)
                // - String -> String (via Display trait, allocates but is correct)
                // This is needed because Ruchy string literals default to &str
                Ok(quote! { #tokens.to_string() })
            }
            // No coercion needed
            _ => Ok(tokens.clone()),
        }
    }
}

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

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

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

    fn string_expr(s: &str) -> Expr {
        make_expr(ExprKind::Literal(Literal::String(s.to_string())))
    }

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

    // ========================================================================
    // try_transpile_result_call tests
    // ========================================================================

    #[test]
    fn test_result_call_ok() {
        let transpiler = Transpiler::new();
        let args = vec![int_expr(42)];
        let result = transpiler.try_transpile_result_call("Ok", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        assert!(tokens_str.contains("Ok"));
        assert!(tokens_str.contains("42"));
    }

    #[test]
    fn test_result_call_ok_with_string() {
        let transpiler = Transpiler::new();
        let args = vec![string_expr("success")];
        let result = transpiler.try_transpile_result_call("Ok", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        assert!(tokens_str.contains("Ok"));
        assert!(tokens_str.contains("to_string"));
    }

    #[test]
    fn test_result_call_err() {
        let transpiler = Transpiler::new();
        let args = vec![string_expr("error message")];
        let result = transpiler.try_transpile_result_call("Err", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        assert!(tokens_str.contains("Err"));
        assert!(tokens_str.contains("to_string"));
    }

    #[test]
    fn test_result_call_some() {
        let transpiler = Transpiler::new();
        let args = vec![int_expr(100)];
        let result = transpiler.try_transpile_result_call("Some", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        assert!(tokens_str.contains("Some"));
    }

    #[test]
    fn test_result_call_some_with_string() {
        let transpiler = Transpiler::new();
        let args = vec![string_expr("value")];
        let result = transpiler.try_transpile_result_call("Some", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        assert!(tokens_str.contains("Some"));
        assert!(tokens_str.contains("to_string"));
    }

    #[test]
    fn test_result_call_unknown() {
        let transpiler = Transpiler::new();
        let args = vec![int_expr(1)];
        let result = transpiler.try_transpile_result_call("Unknown", &args);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    #[test]
    fn test_result_call_ok_empty() {
        let transpiler = Transpiler::new();
        let args: Vec<Expr> = vec![];
        let result = transpiler.try_transpile_result_call("Ok", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        assert!(tokens_str.contains("Ok"));
    }

    #[test]
    fn test_result_call_ok_multiple_args() {
        let transpiler = Transpiler::new();
        let args = vec![int_expr(1), int_expr(2)];
        let result = transpiler.try_transpile_result_call("Ok", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
    }

    // ========================================================================
    // transpile_regular_function_call tests
    // ========================================================================

    #[test]
    fn test_regular_function_call_simple() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { my_func };
        let args = vec![int_expr(42)];
        let result = transpiler.transpile_regular_function_call(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("my_func"));
        assert!(tokens_str.contains("42"));
    }

    #[test]
    fn test_regular_function_call_with_string() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { greet };
        let args = vec![string_expr("hello")];
        let result = transpiler.transpile_regular_function_call(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("greet"));
        assert!(tokens_str.contains("hello"));
    }

    #[test]
    fn test_regular_function_call_no_args() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { get_value };
        let args: Vec<Expr> = vec![];
        let result = transpiler.transpile_regular_function_call(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("get_value"));
    }

    #[test]
    fn test_regular_function_call_multiple_args() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { add };
        let args = vec![int_expr(1), int_expr(2), int_expr(3)];
        let result = transpiler.transpile_regular_function_call(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("add"));
    }

    #[test]
    fn test_regular_function_call_with_identifier() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { process };
        let args = vec![ident_expr("data")];
        let result = transpiler.transpile_regular_function_call(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("process"));
        assert!(tokens_str.contains("data"));
    }

    // ========================================================================
    // apply_string_coercion tests
    // ========================================================================

    #[test]
    fn test_apply_string_coercion_string_literal_to_string() {
        let transpiler = Transpiler::new();
        let arg = string_expr("hello");
        let tokens = quote! { "hello" };
        let result = transpiler.apply_string_coercion(&arg, &tokens, "String");
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("to_string"));
    }

    #[test]
    fn test_apply_string_coercion_string_literal_to_str_ref() {
        let transpiler = Transpiler::new();
        let arg = string_expr("hello");
        let tokens = quote! { "hello" };
        let result = transpiler.apply_string_coercion(&arg, &tokens, "&str");
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        // Should keep as-is, no to_string
        assert!(!tokens_str.contains("to_string"));
    }

    #[test]
    fn test_apply_string_coercion_identifier_to_string() {
        let transpiler = Transpiler::new();
        let arg = ident_expr("name");
        let tokens = quote! { name };
        let result = transpiler.apply_string_coercion(&arg, &tokens, "String");
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("to_string"));
    }

    #[test]
    fn test_apply_string_coercion_int_no_change() {
        let transpiler = Transpiler::new();
        let arg = int_expr(42);
        let tokens = quote! { 42 };
        let result = transpiler.apply_string_coercion(&arg, &tokens, "i32");
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(!tokens_str.contains("to_string"));
    }

    #[test]
    fn test_apply_string_coercion_identifier_to_int() {
        let transpiler = Transpiler::new();
        let arg = ident_expr("x");
        let tokens = quote! { x };
        let result = transpiler.apply_string_coercion(&arg, &tokens, "i32");
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(!tokens_str.contains("to_string"));
    }
}