bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]

//! Coverage tests for parser.rs: split_macro_args, parse_format_string,
//! build_format_concat, extract_index_suffix edge cases, convert_field_expr,
//! convert_return_expr, error paths, and type conversions not covered by
//! parser_coverage_tests.rs and parser_coverage_tests2.rs.

use super::parser::parse;
use crate::ast::restricted::{Expr, Literal, Stmt, Type};

// ============================================================================
// Format string parsing: escaped braces, {:fmt} specifiers
// ============================================================================

#[test]
fn test_format_escaped_double_braces() {
    // println!("a {{ b }}") => literal "a { b }" with no placeholders
    let ast = parse(r#"fn main() { println!("a {{ b }}"); }"#).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Expr(Expr::FunctionCall { name, args }) => {
            assert_eq!(name, "rash_println");
            // Single literal arg with literal braces
            assert!(matches!(&args[0], Expr::Literal(Literal::Str(s)) if s.contains('{')));
        }
        _ => panic!("Expected rash_println"),
    }
}

#[test]
fn test_format_with_format_specifier() {
    // println!("{:>10}", x) => format specifier {:>10} treated as placeholder
    // The macro parser uses convert_print_format_args which produces format_concat
    let ast = parse(r#"fn main() { println!("{:>10}", x); }"#).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Expr(Expr::FunctionCall { name, args }) => {
            assert_eq!(name, "rash_println");
            // With {:>10} and one arg, the format concat may collapse to single var
            // since the format spec consumes the only placeholder
            assert_eq!(args.len(), 1);
        }
        _ => panic!("Expected rash_println"),
    }
}

#[test]
fn test_format_multiple_placeholders() {
    // println!("{} + {} = {}", a, b, c)
    let ast = parse(r#"fn main() { println!("{} + {} = {}", a, b, c); }"#).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Expr(Expr::FunctionCall { name, args }) => {
            assert_eq!(name, "rash_println");
            match &args[0] {
                Expr::FunctionCall { name, args } => {
                    assert_eq!(name, "__format_concat");
                    // " + " and " = " literals plus 3 variables = 5 parts
                    assert!(args.len() >= 5);
                }
                _ => panic!("Expected __format_concat"),
            }
        }
        _ => panic!("Expected rash_println"),
    }
}

#[test]
fn test_format_single_placeholder_no_text() {
    // println!("{}", x) => format_concat with one Variable, collapses to Variable
    let ast = parse(r#"fn main() { println!("{}", x); }"#).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Expr(Expr::FunctionCall { name, args }) => {
            assert_eq!(name, "rash_println");
            // Single placeholder, no surrounding text => collapses to variable
            assert!(matches!(&args[0], Expr::Variable(n) if n == "x"));
        }
        _ => panic!("Expected rash_println with variable"),
    }
}

// ============================================================================
// split_macro_args: nested parens, brackets, braces, strings
// ============================================================================

#[test]
fn test_println_nested_parens_in_args() {
    // println!("result: {}", foo(1, 2))
    let ast = parse(r#"fn main() { println!("result: {}", foo(1, 2)); }"#).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Expr(Expr::FunctionCall { name, args }) => {
            assert_eq!(name, "rash_println");
            // The arg should be __format_concat with a function call
            match &args[0] {
                Expr::FunctionCall { name, args } => {
                    assert_eq!(name, "__format_concat");
                    assert_eq!(args.len(), 2); // "result: " + foo(1,2)
                }
                _ => panic!("Expected __format_concat"),
            }
        }
        _ => panic!("Expected rash_println"),
    }
}

#[test]
fn test_println_string_with_comma_inside() {
    // The comma inside the format string should not split
    let ast = parse(r#"fn main() { println!("hello, world"); }"#).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Expr(Expr::FunctionCall { name, args }) => {
            assert_eq!(name, "rash_println");
            assert!(matches!(&args[0], Expr::Literal(Literal::Str(s)) if s == "hello, world"));
        }
        _ => panic!("Expected rash_println"),
    }
}

// ============================================================================
// format! macro expression
// ============================================================================

#[test]
fn test_format_macro_multi_arg() {
    let ast = parse(r#"fn main() { let s = format!("x={}", v); }"#).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Let {
            value: Expr::FunctionCall { name, .. },
            ..
        } => {
            assert_eq!(name, "__format_concat");
        }
        _ => panic!("Expected format! -> __format_concat"),
    }
}

#[test]
fn test_format_macro_single_arg_is_literal() {
    let ast = parse(r#"fn main() { let s = format!("hello"); }"#).unwrap();
    assert!(matches!(
        &ast.functions[0].body[0],
        Stmt::Let { value: Expr::Literal(Literal::Str(s)), .. } if s == "hello"
    ));
}

// ============================================================================
// extract_index_suffix edge cases: paren, method, unary, call
// ============================================================================

#[test]
fn test_index_with_parenthesized_expr() {
    // arr[(i)] — paren delegates to inner
    let ast = parse(r#"fn main() { let arr = [1]; arr[(0)] = 5; }"#).unwrap();
    match &ast.functions[0].body[1] {
        Stmt::Let {
            name, declaration, ..
        } => {
            assert!(!declaration);
            assert_eq!(name, "arr_0");
        }
        _ => panic!("Expected arr_0"),
    }
}

#[test]
fn test_index_with_method_call_suffix() {
    // arr[s.len()] => name contains "s_len"
    let ast = parse(r#"fn main() { let arr = [1, 2]; let s = "hi"; arr[s.len()] = 9; }"#).unwrap();
    match &ast.functions[0].body[2] {
        Stmt::Let {
            name, declaration, ..
        } => {
            assert!(!declaration);
            assert!(
                name.contains("s_len"),
                "Expected s_len in name, got {}",
                name
            );
        }
        _ => panic!("Expected index assignment"),
    }
}

#[test]
fn test_index_with_unary_minus() {
    // arr[-1] is parsed with unary stripped for suffix
    let ast = parse(r#"fn main() { let arr = [1]; let v = arr[-1]; }"#).unwrap();
    match &ast.functions[0].body[1] {
        Stmt::Let {
            value: Expr::Index { .. },
            ..
        } => {}
        _ => panic!("Expected Index expression"),
    }
}

#[test]
fn test_index_with_function_call() {
    // arr[hash(key)] => extract_call_index_suffix => "hash_key"
    let ast = parse(r#"fn main() { let arr = [1]; arr[hash(key)] = 5; }"#).unwrap();
    match &ast.functions[0].body[1] {
        Stmt::Let { name, .. } => {
            assert!(name.contains("hash"), "Expected hash in name, got {}", name);
            assert!(name.contains("key"), "Expected key in name, got {}", name);
        }
        _ => panic!("Expected index assignment"),
    }
}

#[test]
fn test_index_with_function_call_no_args() {
    // arr[f()] => extract_call_index_suffix with no args => just "f"
    let ast = parse(r#"fn main() { let arr = [1]; arr[f()] = 5; }"#).unwrap();
    match &ast.functions[0].body[1] {
        Stmt::Let { name, .. } => {
            assert!(name.contains("f"), "Expected f in name, got {}", name);
        }
        _ => panic!("Expected index assignment"),
    }
}

// ============================================================================
// convert_field_expr: field access becomes Index
// ============================================================================

#[test]
fn test_field_access_expr() {
    let src = r#"
        struct Point { x: u32, y: u32 }
        fn main() { let p = Point { x: 1, y: 2 }; let v = p.x; }
    "#;
    let ast = parse(src).unwrap();
    // Field access becomes Index with I32(0) index
    match &ast.functions[0].body[1] {
        Stmt::Let {
            value: Expr::Index { index, .. },
            ..
        } => {
            assert!(matches!(**index, Expr::Literal(Literal::I32(0))));
        }
        _ => panic!("Expected Index from field access"),
    }
}

// ============================================================================
// convert_return_expr: return without value in expr position
// ============================================================================

#[test]
fn test_return_no_value_in_expr_produces_block() {
    // Bare return in closure body is a block expression
    let ast = parse(r#"fn main() { let f = |x| { return; }; }"#).unwrap();
    // Closure body: Block([Return(None)])
    match &ast.functions[0].body[0] {
        Stmt::Let {
            value: Expr::Block(stmts),
            ..
        } => {
            assert!(matches!(&stmts[0], Stmt::Return(None)));
        }
        _ => panic!("Expected Block with Return(None) from closure"),
    }
}

// ============================================================================
// Type conversions: tuple type, array type, complex unsupported
// ============================================================================

#[test]
fn test_type_tuple_param() {
    let ast = parse(r#"#[bashrs::main] fn f(t: (u32, u32)) { let x = t; }"#).unwrap();
    // Tuple type → Str
    assert!(matches!(ast.functions[0].params[0].param_type, Type::Str));
}

#[test]
fn test_type_array_param() {
    let ast = parse(r#"#[bashrs::main] fn f(a: [u32; 3]) { let x = a; }"#).unwrap();
    assert!(matches!(ast.functions[0].params[0].param_type, Type::Str));
}

#[test]
fn test_type_slice_reference() {
    let ast = parse(r#"#[bashrs::main] fn f(s: &[u32]) { let x = s; }"#).unwrap();
    assert!(matches!(ast.functions[0].params[0].param_type, Type::Str));
}

#[test]
fn test_type_unknown_path_defaults_to_str() {
    let ast = parse(r#"#[bashrs::main] fn f(v: Vec<String>) { let x = v; }"#).unwrap();
    assert!(matches!(ast.functions[0].params[0].param_type, Type::Str));
}

// ============================================================================
// Error paths: let without init, complex patterns, unsupported stmt
// ============================================================================

#[test]
fn test_let_tuple_without_init_is_error() {
    assert!(parse(r#"fn main() { let (a, b); }"#).is_err());
}

#[test]
fn test_complex_param_pattern_is_error() {
    assert!(parse(r#"fn main((a, b): (u32, u32)) { let x = a; }"#).is_err());
}

#[test]
fn test_match_as_expression() {
    // match in expression position => Block([Match{...}])
    let ast = parse(r#"fn main() { let x = match v { 0 => 1, _ => 2 }; }"#).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Let {
            value: Expr::Block(stmts),
            ..
        } => {
            assert!(matches!(&stmts[0], Stmt::Match { .. }));
        }
        _ => panic!("Expected Block with Match from match expression"),
    }
}

// ============================================================================
// Compound assignments on field and deref targets
// ============================================================================

#[test]
fn test_compound_assign_on_field() {
    let src = r#"
        struct S { v: u32 }
        impl S { fn inc(&mut self) { self.v += 1; } }
        fn main() { let x = 0; }
    "#;
    let ast = parse(src).unwrap();
    let inc_fn = ast.functions.iter().find(|f| f.name == "inc").unwrap();
    match &inc_fn.body[0] {
        Stmt::Let {
            name, declaration, ..
        } => {
            assert_eq!(name, "v");
            assert!(!declaration);
        }
        _ => panic!("Expected field compound assignment"),
    }
}

#[test]
fn test_compound_assign_on_index() {
    let ast = parse(r#"fn main() { let mut arr = [1, 2]; arr[0] += 5; }"#).unwrap();
    match &ast.functions[0].body[1] {
        Stmt::Let {
            name, declaration, ..
        } => {
            assert_eq!(name, "arr_0");
            assert!(!declaration);
        }
        _ => panic!("Expected arr_0 compound assignment"),
    }
}

#[test]
fn test_compound_assign_on_deref() {
    let ast = parse(r#"fn main() { let mut x = 0; let p = &mut x; *p += 1; }"#).unwrap();
    match &ast.functions[0].body[2] {
        Stmt::Let {
            name, declaration, ..
        } => {
            assert_eq!(name, "p");
            assert!(!declaration);
        }
        _ => panic!("Expected deref compound assignment"),
    }
}

// ============================================================================
// convert_let_expr: non-literal pattern falls through
// ============================================================================

#[test]
fn test_let_expr_with_variable_pattern() {
    // if let x = opt { ... } where pattern is not a literal
    let ast = parse(r#"fn main() { let opt = 1; if let x = opt { let a = x; } }"#).unwrap();
    // When pattern is a variable (not a literal), convert_let_expr returns rhs
    assert!(matches!(&ast.functions[0].body[1], Stmt::If { .. }));
}

// ============================================================================
// Unsupported literal type in expression
// ============================================================================

#[test]
fn test_char_literal_is_error() {
    assert!(parse(r#"fn main() { let c = 'a'; }"#).is_err());
}

// ============================================================================
// rash::main attribute alias
// ============================================================================

#[test]
fn test_rash_main_attribute() {
    let ast = parse(r#"#[rash::main] fn my_entry() { let x = 1; }"#).unwrap();
    assert_eq!(ast.entry_point, "my_entry");
}

// ============================================================================
// Field assignment with unnamed fields
// ============================================================================

#[test]
fn test_unnamed_field_assignment_in_compound() {
    // self.0 += 1 via unnamed member
    let src = r#"
        struct Wrapper(u32);
        impl Wrapper { fn bump(&mut self) { self.0 += 1; } }
        fn main() { let x = 0; }
    "#;
    let ast = parse(src).unwrap();
    let bump_fn = ast.functions.iter().find(|f| f.name == "bump").unwrap();
    match &bump_fn.body[0] {
        Stmt::Let { name, .. } => {
            assert!(
                name.starts_with("field_"),
                "Expected field_ prefix, got {}",
                name
            );
        }
        _ => panic!("Expected unnamed field compound assignment"),
    }
}

// ============================================================================
// Enum items are gracefully skipped
// ============================================================================

#[test]
fn test_enum_item_skipped() {
    let src = r#"enum Color { Red, Green } fn main() { let x = 0; }"#;
    let ast = parse(src).unwrap();
    assert_eq!(ast.entry_point, "main");
}

// ============================================================================
// Closure with no block: expression body
// ============================================================================

#[test]
fn test_closure_expr_body() {
    let ast = parse(r#"fn main() { let f = |x| x * 2; }"#).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Let {
            value: Expr::Binary { .. },
            ..
        } => {}
        _ => panic!("Expected Binary from closure expression body"),
    }
}

// ============================================================================
// Index expression in expr position (read, not assign)
// ============================================================================

#[test]
fn test_index_read_expr() {
    let ast = parse(r#"fn main() { let arr = [10, 20]; let v = arr[1]; }"#).unwrap();
    match &ast.functions[0].body[1] {
        Stmt::Let {
            value: Expr::Index { object, index },
            ..
        } => {
            assert!(matches!(**object, Expr::Variable(ref n) if n == "arr"));
            assert!(matches!(**index, Expr::Literal(Literal::U32(1))));
        }
        _ => panic!("Expected Index read"),
    }
}

// ============================================================================
// Negative range pattern
// ============================================================================

#[test]
fn test_negative_range_pattern() {
    let src = r#"fn main() { match x { -10..=-1 => { let a = 1; } _ => {} } }"#;
    let ast = parse(src).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Match { arms, .. } => match &arms[0].pattern {
            crate::ast::restricted::Pattern::Range {
                start,
                end,
                inclusive,
            } => {
                assert!(matches!(start, Literal::I32(-10)));
                assert!(matches!(end, Literal::I32(-1)));
                assert!(*inclusive);
            }
            _ => panic!("Expected Range pattern"),
        },
        _ => panic!("Expected Match"),
    }
}