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
#![cfg(any(
    not(any(
        feature = "parser_tests",
        feature = "analyzer_tests",
        feature = "codegen_tests",
        feature = "interpreter_tests",
        feature = "conformance_tests",
        feature = "integration_tests",
    )),
    feature = "analyzer_tests",
))]

/// TDD Test: No .clone() on intermediate Vec when using nested indexing
///
/// Bug: `self.tiles[row][col]` generates `self.tiles[row as usize].clone()[col as usize]`
/// which clones the ENTIRE inner Vec just to access one element.
/// In Rust, `vec[i][j]` auto-derefs through the reference returned by the first index.
///
/// Root Cause: The auto-clone for Vec indexing fired on the inner Index expression
/// because `in_field_access_object` wasn't set when generating the object of an Index.
///
/// Fix: Set `in_field_access_object = true` before generating the object of an Index,
/// same as we do for FieldAccess objects.
#[path = "common/test_utils.rs"]
mod test_utils;

#[test]
fn test_no_clone_on_2d_vec_nested_index() {
    // Pattern from windjammer-game world/tilemap.wj:
    // self.tiles[row][col] should NOT clone the entire row Vec
    let source = r#"
pub struct Grid {
    pub cells: Vec<Vec<i32>>,
}

impl Grid {
    pub fn get(self, row: i32, col: i32) -> i32 {
        self.cells[row][col]
    }
}
"#;

    let generated = test_utils::compile_single(source);
    println!("Generated:\n{}", generated);

    // Should NOT clone the inner Vec: cells[row].clone()[col] is WRONG
    assert!(
        !generated.contains(".clone()["),
        "Should not clone intermediate Vec in nested indexing.\nGenerated:\n{}",
        generated
    );
}

#[test]
fn test_no_clone_on_2d_bool_nested_index() {
    // Pattern from windjammer-game pathfinding/grid.wj:
    // self.walkable[x][y] — bool is Copy, no clone needed at all
    let source = r#"
pub struct PathGrid {
    pub walkable: Vec<Vec<bool>>,
}

impl PathGrid {
    pub fn is_walkable(self, x: i32, y: i32) -> bool {
        self.walkable[x][y]
    }
}
"#;

    let generated = test_utils::compile_single(source);
    println!("Generated:\n{}", generated);

    assert!(
        !generated.contains(".clone()["),
        "Should not clone intermediate Vec for bool (Copy) nested index.\nGenerated:\n{}",
        generated
    );
    assert!(
        !generated.contains(".clone()"),
        "Bool is Copy — no clone needed at all.\nGenerated:\n{}",
        generated
    );
}

#[test]
fn test_2d_vec_non_copy_final_element_clone_ok() {
    // When the final element is non-Copy and consumed, .clone() on the element IS ok
    // But the intermediate Vec should NEVER be cloned
    let source = r#"
pub struct Grid {
    pub names: Vec<Vec<string>>,
}

impl Grid {
    pub fn get_name(self, row: i32, col: i32) -> string {
        self.names[row][col]
    }
}
"#;

    let generated = test_utils::compile_single(source);
    println!("Generated:\n{}", generated);

    // The final element clone is OK (String is not Copy)
    // But the intermediate Vec clone is NOT OK
    assert!(
        !generated.contains("].clone()["),
        "Should not clone intermediate Vec even when final element needs clone.\nGenerated:\n{}",
        generated
    );
}