windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
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
#![cfg(any(
    not(any(
        feature = "parser_tests",
        feature = "analyzer_tests",
        feature = "codegen_tests",
        feature = "interpreter_tests",
        feature = "conformance_tests",
        feature = "integration_tests",
    )),
    feature = "parser_tests",
))]

//! Comprehensive Parser Type Tests
//!
//! These tests verify that the parser correctly parses all type annotations.
//! Types appear in function parameters, return types, let bindings, struct fields, etc.
//!
//! Note: Some types like i32, f32 may be parsed as Custom("i32") rather than
//! specific Type variants - both are valid depending on parser implementation.

use windjammer::lexer::Lexer;
use windjammer::parser::ast::*;
use windjammer::parser_impl::Parser;

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

fn parse_program(input: &str) -> Program<'_> {
    let mut lexer = Lexer::new(input);
    let tokens = lexer.tokenize_with_locations();
    let mut parser = Parser::new(tokens);
    parser.parse().expect("Failed to parse program")
}

fn get_fn_param_type(input: &str) -> Type {
    let program = parse_program(input);
    if let Some(Item::Function { decl, .. }) = program.items.first() {
        if let Some(param) = decl.parameters.first() {
            return param.type_.clone();
        }
    }
    panic!("Failed to extract parameter type from: {}", input);
}

fn get_fn_return_type(input: &str) -> Option<Type> {
    let program = parse_program(input);
    if let Some(Item::Function { decl, .. }) = program.items.first() {
        return decl.return_type.clone();
    }
    panic!("Failed to extract return type from: {}", input);
}

fn get_struct_field_type(input: &str) -> Type {
    let program = parse_program(input);
    if let Some(Item::Struct { decl, .. }) = program.items.first() {
        if let Some(field) = decl.fields.first() {
            return field.field_type.clone();
        }
    }
    panic!("Failed to extract field type from: {}", input);
}

/// Check if a type is a numeric type (either specific variant or Custom)
#[allow(dead_code)]
fn is_numeric_type(ty: &Type, expected_name: &str) -> bool {
    match ty {
        Type::Int | Type::Int32 | Type::Uint | Type::Float => true,
        Type::Custom(name) => name == expected_name,
        _ => false,
    }
}

// ============================================================================
// PRIMITIVE TYPES
// ============================================================================

#[test]
fn test_type_i32() {
    let ty = get_fn_param_type("fn foo(x: i32) { }");
    assert!(
        matches!(ty, Type::Int32 | Type::Custom(_)),
        "Expected Int32 or Custom, got {:?}",
        ty
    );
}

#[test]
fn test_type_int() {
    let ty = get_fn_param_type("fn foo(x: int) { }");
    assert!(
        matches!(ty, Type::Int | Type::Custom(_)),
        "Expected Int or Custom, got {:?}",
        ty
    );
}

#[test]
fn test_type_float() {
    let ty = get_fn_param_type("fn foo(x: float) { }");
    assert!(
        matches!(ty, Type::Float | Type::Custom(_)),
        "Expected Float or Custom, got {:?}",
        ty
    );
}

#[test]
fn test_type_bool() {
    let ty = get_fn_param_type("fn foo(x: bool) { }");
    assert!(matches!(ty, Type::Bool), "Expected Bool, got {:?}", ty);
}

#[test]
fn test_type_string() {
    let ty = get_fn_param_type("fn foo(x: String) { }");
    assert!(matches!(ty, Type::String), "Expected String, got {:?}", ty);
}

// ============================================================================
// CUSTOM/NAMED TYPES
// ============================================================================

#[test]
fn test_type_custom() {
    let ty = get_fn_param_type("fn foo(x: Point) { }");
    if let Type::Custom(name) = ty {
        assert_eq!(name, "Point");
    } else {
        panic!("Expected Custom type, got {:?}", ty);
    }
}

#[test]
fn test_type_custom_camelcase() {
    let ty = get_fn_param_type("fn foo(x: MyCustomType) { }");
    if let Type::Custom(name) = ty {
        assert_eq!(name, "MyCustomType");
    } else {
        panic!("Expected Custom type");
    }
}

// ============================================================================
// PARAMETERIZED (GENERIC) TYPES
// ============================================================================

#[test]
fn test_type_vec() {
    let ty = get_fn_param_type("fn foo(x: Vec<i32>) { }");
    match ty {
        Type::Vec(inner) => {
            // Inner can be Int32 or Custom("i32")
            assert!(matches!(*inner, Type::Int32 | Type::Custom(_)));
        }
        Type::Parameterized(name, args) => {
            assert_eq!(name, "Vec");
            assert_eq!(args.len(), 1);
        }
        _ => panic!("Expected Vec or Parameterized type, got {:?}", ty),
    }
}

#[test]
fn test_type_option() {
    let ty = get_fn_param_type("fn foo(x: Option<String>) { }");
    match ty {
        Type::Option(inner) => {
            // inner could be String or Custom("String")
            assert!(matches!(*inner, Type::String | Type::Custom(_)));
        }
        Type::Parameterized(name, args) => {
            assert_eq!(name, "Option");
            assert_eq!(args.len(), 1);
        }
        _ => panic!("Expected Option or Parameterized type, got {:?}", ty),
    }
}

#[test]
fn test_type_result() {
    let ty = get_fn_param_type("fn foo(x: Result<i32, Error>) { }");
    match ty {
        Type::Result(ok, _err) => {
            // ok can be Int32 or Custom("i32")
            assert!(matches!(*ok, Type::Int32 | Type::Custom(_)));
        }
        Type::Parameterized(name, args) => {
            assert_eq!(name, "Result");
            assert_eq!(args.len(), 2);
        }
        _ => panic!("Expected Result or Parameterized type, got {:?}", ty),
    }
}

#[test]
fn test_type_parameterized() {
    let ty = get_fn_param_type("fn foo(x: HashMap<String, i32>) { }");
    if let Type::Parameterized(name, args) = ty {
        assert_eq!(name, "HashMap");
        assert_eq!(args.len(), 2);
    } else {
        panic!("Expected Parameterized type, got {:?}", ty);
    }
}

#[test]
fn test_type_nested_generic() {
    // Note: Nested generics with >> may require space: Vec<Option<i32> >
    // This is a known parser limitation with >> being parsed as shift operator
    let ty = get_fn_param_type("fn foo(x: Vec<Option<i32> >) { }");
    match ty {
        Type::Vec(inner) => {
            // Inner type should be Option<i32>
            assert!(matches!(
                *inner,
                Type::Option(_) | Type::Parameterized(_, _)
            ));
        }
        Type::Parameterized(name, args) => {
            assert_eq!(name, "Vec");
            assert_eq!(args.len(), 1);
        }
        _ => panic!("Expected Vec or Parameterized type, got {:?}", ty),
    }
}

// ============================================================================
// REFERENCE TYPES
// ============================================================================

#[test]
fn test_type_ref() {
    let ty = get_fn_param_type("fn foo(x: &i32) { }");
    if let Type::Reference(inner) = ty {
        // inner can be Int32 or Custom("i32")
        assert!(matches!(*inner, Type::Int32 | Type::Custom(_)));
    } else {
        panic!("Expected Reference type, got {:?}", ty);
    }
}

#[test]
fn test_type_mut_ref() {
    let ty = get_fn_param_type("fn foo(x: &mut i32) { }");
    if let Type::MutableReference(inner) = ty {
        // inner can be Int32 or Custom("i32")
        assert!(matches!(*inner, Type::Int32 | Type::Custom(_)));
    } else {
        panic!("Expected MutableReference type, got {:?}", ty);
    }
}

#[test]
fn test_type_ref_string() {
    let ty = get_fn_param_type("fn foo(x: &string) { }");
    if let Type::Reference(inner) = ty {
        assert!(matches!(*inner, Type::String));
    } else {
        panic!("Expected Reference type");
    }
}

#[test]
fn test_type_ref_custom() {
    let ty = get_fn_param_type("fn foo(x: &Point) { }");
    if let Type::Reference(inner) = ty {
        if let Type::Custom(name) = *inner {
            assert_eq!(name, "Point");
        } else {
            panic!("Expected Custom inner type");
        }
    } else {
        panic!("Expected Reference type");
    }
}

// ============================================================================
// ARRAY TYPES
// ============================================================================

#[test]
fn test_type_array() {
    let ty = get_fn_param_type("fn foo(x: [i32; 10]) { }");
    if let Type::Array(element, size) = ty {
        // element can be Int32 or Custom("i32")
        assert!(matches!(*element, Type::Int32 | Type::Custom(_)));
        assert_eq!(size, 10);
    } else {
        panic!("Expected Array type, got {:?}", ty);
    }
}

// ============================================================================
// TUPLE TYPES
// ============================================================================

#[test]
fn test_type_tuple_pair() {
    let ty = get_fn_param_type("fn foo(x: (i32, string)) { }");
    if let Type::Tuple(elements) = ty {
        assert_eq!(elements.len(), 2);
    } else {
        panic!("Expected Tuple type, got {:?}", ty);
    }
}

#[test]
fn test_type_tuple_triple() {
    let ty = get_fn_param_type("fn foo(x: (i32, float, bool)) { }");
    if let Type::Tuple(elements) = ty {
        assert_eq!(elements.len(), 3);
    } else {
        panic!("Expected Tuple type");
    }
}

#[test]
fn test_type_unit() {
    // Empty tuple () is the unit type
    let ret = get_fn_return_type("fn foo() -> () { }");
    if let Some(Type::Tuple(elements)) = ret {
        assert!(elements.is_empty());
    } else {
        panic!("Expected unit type (empty tuple), got {:?}", ret);
    }
}

#[test]
fn test_fn_param_str_type_is_custom_str() {
    let t = get_fn_param_type("fn f(key: str) { }");
    // Parser may normalize `str` to the canonical string type
    let ok = matches!(&t, Type::String) || matches!(&t, Type::Custom(s) if s == "str");
    assert!(
        ok,
        "expected `key: String` → Type:: String or Custom(\"str\"), got {:?}",
        t
    );
}

// ============================================================================
// FUNCTION POINTER TYPES
// ============================================================================

#[test]
fn test_type_fn_pointer() {
    let ty = get_fn_param_type("fn foo(f: fn(i32) -> i32) { }");
    if let Type::FunctionPointer {
        params,
        return_type,
    } = ty
    {
        assert_eq!(params.len(), 1);
        assert!(return_type.is_some());
    } else {
        panic!("Expected FunctionPointer type, got {:?}", ty);
    }
}

#[test]
fn test_type_fn_pointer_no_return() {
    let ty = get_fn_param_type("fn foo(f: fn(i32)) { }");
    if let Type::FunctionPointer {
        params,
        return_type,
    } = ty
    {
        assert_eq!(params.len(), 1);
        assert!(return_type.is_none());
    } else {
        panic!("Expected FunctionPointer type");
    }
}

#[test]
fn test_type_fn_pointer_multiple_params() {
    let ty = get_fn_param_type("fn foo(f: fn(i32, string, bool) -> float) { }");
    if let Type::FunctionPointer { params, .. } = ty {
        assert_eq!(params.len(), 3);
    } else {
        panic!("Expected FunctionPointer type");
    }
}

// ============================================================================
// RETURN TYPES
// ============================================================================

#[test]
fn test_return_type_simple() {
    let ret = get_fn_return_type("fn foo() -> i32 { 42 }");
    assert!(ret.is_some());
    if let Some(ty) = ret {
        assert!(matches!(ty, Type::Int32 | Type::Custom(_)));
    }
}

#[test]
fn test_return_type_none() {
    let ret = get_fn_return_type("fn foo() { }");
    assert!(ret.is_none());
}

#[test]
fn test_return_type_vec() {
    let ret = get_fn_return_type("fn foo() -> Vec<i32> { Vec::new() }");
    match ret {
        Some(Type::Vec(_)) | Some(Type::Parameterized(_, _)) => {}
        _ => panic!("Expected Vec or Parameterized return type, got {:?}", ret),
    }
}

#[test]
fn test_return_type_option() {
    let ret = get_fn_return_type("fn foo() -> Option<String> { None }");
    match ret {
        Some(Type::Option(_)) | Some(Type::Parameterized(_, _)) => {}
        _ => panic!(
            "Expected Option or Parameterized return type, got {:?}",
            ret
        ),
    }
}

// ============================================================================
// STRUCT FIELD TYPES
// ============================================================================

#[test]
fn test_field_type_primitive() {
    let ty = get_struct_field_type("struct Point { x: i32 }");
    assert!(matches!(ty, Type::Int32 | Type::Custom(_)));
}

#[test]
fn test_field_type_string() {
    let ty = get_struct_field_type("struct Person { name: String }");
    assert!(matches!(ty, Type:: String));
}

#[test]
fn test_field_type_vec() {
    let ty = get_struct_field_type("struct Container { items: Vec<Item> }");
    match ty {
        Type::Vec(_) | Type::Parameterized(_, _) => {}
        _ => panic!("Expected Vec or Parameterized field type, got {:?}", ty),
    }
}

#[test]
fn test_field_type_option() {
    let ty = get_struct_field_type("struct Node { parent: Option<Node> }");
    match ty {
        Type::Option(_) | Type::Parameterized(_, _) => {}
        _ => panic!("Expected Option or Parameterized field type, got {:?}", ty),
    }
}

// ============================================================================
// COMPLEX TYPE COMBINATIONS
// ============================================================================

#[test]
fn test_type_ref_to_vec() {
    let ty = get_fn_param_type("fn foo(x: &Vec<i32>) { }");
    if let Type::Reference(inner) = ty {
        match *inner {
            Type::Vec(_) | Type::Parameterized(_, _) => {}
            _ => panic!("Expected Vec or Parameterized inside Reference"),
        }
    } else {
        panic!("Expected Reference type");
    }
}

#[test]
fn test_type_mut_ref_to_parameterized() {
    let program = parse_program("fn foo(mut x: HashMap<string, i32>) { }");
    if let Some(Item::Function { decl, .. }) = program.items.first() {
        let param = &decl.parameters[0];
        assert!(param.is_mutable, "mut param should set is_mutable");
        if let Type::Parameterized(name, _) = &param.type_ {
            assert_eq!(name, "HashMap");
        } else {
            panic!("Expected Parameterized HashMap type, got {:?}", param.type_);
        }
    } else {
        panic!("Expected function item");
    }
}

// ============================================================================
// TYPE IN GENERICS CONTEXT
// ============================================================================

#[test]
fn test_generic_fn_type_param() {
    let code = "fn foo<T>(x: T) -> T { x }";
    let program = parse_program(code);
    if let Some(Item::Function { decl, .. }) = program.items.first() {
        assert!(!decl.type_params.is_empty());
        // Parameter type should be a type variable T (parsed as Generic or Custom)
        if let Some(param) = decl.parameters.first() {
            match &param.type_ {
                Type::Generic(name) | Type::Custom(name) => assert_eq!(name, "T"),
                _ => panic!(
                    "Expected Generic or Custom type for param, got {:?}",
                    param.type_
                ),
            }
        }
    } else {
        panic!("Expected Function");
    }
}

#[test]
fn test_generic_fn_multiple_type_params() {
    let code = "fn foo<T, U>(x: T, y: U) -> T { x }";
    let program = parse_program(code);
    if let Some(Item::Function { decl, .. }) = program.items.first() {
        assert_eq!(decl.type_params.len(), 2);
    } else {
        panic!("Expected Function");
    }
}

#[test]
fn test_generic_struct_type_param() {
    let code = "struct Container<T> { value: T }";
    let program = parse_program(code);
    if let Some(Item::Struct { decl, .. }) = program.items.first() {
        assert!(!decl.type_params.is_empty());
    } else {
        panic!("Expected Struct");
    }
}

// ============================================================================
// TYPE BOUNDS (WHERE CLAUSES)
// ============================================================================

#[test]
fn test_type_with_bound() {
    let code = "struct Container<T: Clone> { value: T }";
    let program = parse_program(code);
    if let Some(Item::Struct { decl, .. }) = program.items.first() {
        // Type params should have bounds
        assert!(!decl.type_params.is_empty());
        if let Some(tp) = decl.type_params.first() {
            assert!(!tp.bounds.is_empty());
        }
    } else {
        panic!("Expected Struct");
    }
}

#[test]
fn test_where_clause() {
    let code = r#"
    impl<T> Container<T> where T: Clone {
        fn clone_value(self) -> T { self.value }
    }
    "#;
    let program = parse_program(code);
    if let Some(Item::Impl { block, .. }) = program.items.first() {
        assert!(!block.where_clause.is_empty());
    } else {
        panic!("Expected Impl");
    }
}

// ============================================================================
// INFER TYPE
// ============================================================================

#[test]
fn test_type_infer() {
    // The _ type placeholder for inference
    let ty = get_fn_param_type("fn foo(x: _) { }");
    assert!(matches!(ty, Type::Infer));
}

// ============================================================================
// TRAIT OBJECTS
// ============================================================================

#[test]
fn test_type_trait_object() {
    let ty = get_fn_param_type("fn foo(x: dyn Display) { }");
    if let Type::TraitObject(trait_name) = ty {
        assert_eq!(trait_name, "Display");
    } else {
        panic!("Expected TraitObject type, got {:?}", ty);
    }
}

// ============================================================================
// MODULE PATH VS ASSOCIATED TYPE (nested generics / FFI qualification)
// ============================================================================

#[test]
fn test_vec_of_lowercase_module_path_is_custom_not_associated() {
    let ty = get_fn_return_type("pub fn f() -> Vec<ffi::GpuVertex> { }").expect("return type");
    match ty {
        Type::Vec(inner) => match *inner {
            Type::Custom(name) => assert_eq!(name, "ffi::GpuVertex"),
            other => panic!("expected Vec<Custom(ffi::GpuVertex)>, got Vec<{:?}>", other),
        },
        other => panic!("expected Vec, got {:?}", other),
    }
}

#[test]
fn test_result_ok_uses_module_path_under_comma_delimiter() {
    let ty = get_fn_return_type("pub fn f() -> Result<ffi::GpuVertex, i32> { }").expect("return");
    match ty {
        Type::Result(ok, _err) => match *ok {
            Type::Custom(name) => assert_eq!(name, "ffi::GpuVertex"),
            other => panic!("expected Custom(ffi::GpuVertex), got {:?}", other),
        },
        other => panic!("expected Result, got {:?}", other),
    }
}

#[test]
fn test_associated_type_t_output_still_parsed() {
    let ty = get_fn_param_type("pub fn g(x: T::Output) { }");
    match ty {
        Type::Associated(base, assoc) => {
            assert_eq!(base, "T");
            assert_eq!(assoc, "Output");
        }
        other => panic!("expected Associated(T, Output), got {:?}", other),
    }
}

#[test]
fn test_associated_type_self_item_still_parsed() {
    let ty = get_fn_param_type("pub fn g(x: Self::Item) { }");
    match ty {
        Type::Associated(base, assoc) => {
            assert_eq!(base, "Self");
            assert_eq!(assoc, "Item");
        }
        other => panic!("expected Associated(Self, Item), got {:?}", other),
    }
}

#[test]
fn test_impl_method_return_vec_ffi_gpuvertex_is_custom_not_associated() {
    let code = r#"
pub struct VoxelRenderer {}
impl VoxelRenderer {
    fn convert_vertices(self, vertices: Vec<Vertex>) -> Vec<ffi::GpuVertex> {
        Vec::new()
    }
}
"#;
    let program = parse_program(code);
    let block = program
        .items
        .iter()
        .find_map(|item| match item {
            Item::Impl { block, .. } => Some(block),
            _ => None,
        })
        .expect("impl block");
    let func = block
        .functions
        .iter()
        .find(|f| f.name == "convert_vertices")
        .expect("convert_vertices");
    let ret = func.return_type.as_ref().expect("return type");
    match ret {
        Type::Vec(inner) => match **inner {
            Type::Custom(ref n) => assert_eq!(n, "ffi::GpuVertex"),
            ref other => panic!("expected Vec<Custom(ffi::GpuVertex)>, got Vec<{:?}>", other),
        },
        ref other => panic!("expected Vec return, got {:?}", other),
    }
}