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
//! Signature-driven method behavior queries.
//!
//! Every query first attempts a type-qualified lookup in `SignatureRegistry`
//! (e.g. `Vec::push`), deriving the answer from `FunctionSignature` fields.
//! For non-derivable behaviors (strip-redundant, desugar, ambiguity guards),
//! small const tables live in `rust_stdlib_annotations`.

use crate::analyzer::{FunctionSignature, OwnershipMode, SignatureRegistry};
use crate::parser::Type;

// ── Helpers ──────────────────────────────────────────────────────────────

/// Attempt a type-qualified signature lookup, trying multiple receiver type
/// representations (e.g. `Vec`, `Vec<T>`, bare generic base).
fn lookup_sig<'a>(
    method: &str,
    receiver_type: Option<&str>,
    registry: &'a SignatureRegistry,
) -> Option<&'a FunctionSignature> {
    if let Some(ty) = receiver_type {
        let base = ty.split('<').next().unwrap_or(ty);
        let qualified = format!("{}::{}", base, method);
        if let Some(sig) = registry.get_signature(&qualified) {
            return Some(sig);
        }
        if base != ty {
            let qualified_full = format!("{}::{}", ty, method);
            if let Some(sig) = registry.get_signature(&qualified_full) {
                return Some(sig);
            }
        }
    }
    None
}

/// Suffix lookup fallback: finds signatures registered as `Type::method`
/// when the receiver type is unknown. Only returns a result when there is
/// exactly one candidate — if multiple types define the same method name,
/// we cannot disambiguate and must use default behavior.
fn lookup_suffix<'a>(
    method: &str,
    registry: &'a SignatureRegistry,
) -> Option<&'a FunctionSignature> {
    let pattern = format!("::{}", method);
    let mut matches = registry
        .signatures
        .iter()
        .filter(|(key, _)| key.ends_with(&pattern));
    let first = matches.next();
    if matches.next().is_some() {
        return None; // ambiguous
    }
    first.map(|(_, sig)| sig)
}

/// Get the first non-self parameter ownership mode.
fn first_arg_ownership(sig: &FunctionSignature) -> Option<OwnershipMode> {
    let start = if sig.has_self_receiver { 1 } else { 0 };
    sig.param_ownership.get(start).copied()
}

/// Get the first non-self parameter type.
fn first_arg_type(sig: &FunctionSignature) -> Option<&Type> {
    let start = if sig.has_self_receiver { 1 } else { 0 };
    sig.param_types.get(start)
}

/// Number of non-self parameters.
fn arg_count(sig: &FunctionSignature) -> usize {
    if sig.has_self_receiver {
        sig.param_ownership.len().saturating_sub(1)
    } else {
        sig.param_ownership.len()
    }
}

fn is_reference_type(ty: &Type) -> bool {
    matches!(ty, Type::Reference(_))
}

fn is_str_reference(ty: &Type) -> bool {
    matches!(ty, Type::Reference(inner) if matches!(&**inner, Type::Custom(n) if n == "str"))
}

fn is_closure_type(ty: &Type) -> bool {
    matches!(ty, Type::Custom(n) if n == "Fn" || n == "FnMut" || n == "FnOnce")
        || matches!(ty, Type::FunctionPointer { .. })
}

fn is_usize_type(ty: &Type) -> bool {
    matches!(ty, Type::Custom(n) if n == "usize")
}

fn return_type_is(sig: &FunctionSignature, pred: impl Fn(&Type) -> bool) -> bool {
    sig.return_type.as_ref().is_some_and(&pred)
}

// ── Map type constants ───────────────────────────────────────────────────

const MAP_TYPES: &[&str] = &["HashMap", "BTreeMap", "Map", "IndexMap"];

fn is_map_receiver(receiver_type: Option<&str>) -> bool {
    receiver_type.is_some_and(|ty| {
        let base = ty.split('<').next().unwrap_or(ty);
        MAP_TYPES.contains(&base)
    })
}

// ── Primary query functions ──────────────────────────────────────────────

/// Does this method mutate its receiver (`&mut self`)?
pub fn method_mutates_receiver_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    if let Some(sig) = lookup_sig(method, receiver_type, registry) {
        if sig.has_self_receiver && !sig.param_ownership.is_empty() {
            return sig.param_ownership[0] == OwnershipMode::MutBorrowed;
        }
    }
    if let Some(sig) = lookup_suffix(method, registry) {
        if sig.has_self_receiver && !sig.param_ownership.is_empty() {
            return sig.param_ownership[0] == OwnershipMode::MutBorrowed;
        }
    }
    false
}

/// Is this method definitely read-only (`&self`)?
pub fn is_known_readonly_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    if let Some(sig) = lookup_sig(method, receiver_type, registry) {
        if sig.has_self_receiver && !sig.param_ownership.is_empty() {
            return sig.param_ownership[0] != OwnershipMode::MutBorrowed;
        }
    }
    if let Some(sig) = lookup_suffix(method, registry) {
        if sig.has_self_receiver && !sig.param_ownership.is_empty() {
            return sig.param_ownership[0] != OwnershipMode::MutBorrowed;
        }
    }
    false
}

/// Is this a known method in the stdlib signatures?
pub fn is_known_stdlib_method_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    lookup_sig(method, receiver_type, registry).is_some()
        || lookup_suffix(method, registry).is_some()
}

/// Does this method return `usize`?
pub fn method_returns_usize_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    let sig =
        lookup_sig(method, receiver_type, registry).or_else(|| lookup_suffix(method, registry));
    sig.is_some_and(|s| return_type_is(s, is_usize_type))
}

/// Does this method return an iterator?
pub fn method_returns_iterator_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    let sig =
        lookup_sig(method, receiver_type, registry).or_else(|| lookup_suffix(method, registry));
    sig.is_some_and(|s| return_type_is(s, |ty| matches!(ty, Type::Custom(n) if n == "Iterator")))
}

/// Is this method type-preserving (return type == `Self`)?
/// e.g. clone, to_owned, to_vec, into_iter
pub fn method_is_type_preserving_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    let sig =
        lookup_sig(method, receiver_type, registry).or_else(|| lookup_suffix(method, registry));
    sig.is_some_and(|s| return_type_is(s, |ty| matches!(ty, Type::Custom(n) if n == "Self")))
}

/// Is this a storage method that moves a parameter into a collection?
/// Derived from: non-self param is `Owned` (not borrowed) for the stored value.
pub fn method_is_storage_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    let sig =
        lookup_sig(method, receiver_type, registry).or_else(|| lookup_suffix(method, registry));
    if let Some(s) = sig {
        if !s.has_self_receiver || arg_count(s) == 0 {
            return false;
        }
        if s.param_ownership[0] != OwnershipMode::MutBorrowed {
            return false;
        }
        let start = 1; // skip self
        for i in start..s.param_ownership.len() {
            if s.param_ownership[i] == OwnershipMode::Owned {
                if let Some(ty) = s.param_types.get(i) {
                    if !is_usize_type(ty) && !is_closure_type(ty) {
                        return true;
                    }
                }
            }
        }
    }
    false
}

/// Does the first non-self argument need auto-borrowing (`&str` / `&[T]`)?
pub fn method_auto_borrows_arg_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    let sig =
        lookup_sig(method, receiver_type, registry).or_else(|| lookup_suffix(method, registry));
    sig.is_some_and(|s| {
        first_arg_type(s).is_some_and(is_reference_type)
            && first_arg_ownership(s) == Some(OwnershipMode::Borrowed)
    })
}

/// Is this a HashMap/BTreeMap key method whose first arg is a key reference?
pub fn method_is_map_key_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    if !is_map_receiver(receiver_type) {
        // For unknown receiver type, check if method exists on any map type
        if receiver_type.is_some() {
            return false;
        }
        for map_ty in MAP_TYPES {
            if let Some(sig) = lookup_sig(method, Some(map_ty), registry) {
                if sig.has_self_receiver
                    && first_arg_ownership(sig) == Some(OwnershipMode::Borrowed)
                    && first_arg_type(sig).is_some_and(is_reference_type)
                {
                    return true;
                }
            }
        }
        return false;
    }
    let sig = lookup_sig(method, receiver_type, registry);
    sig.is_some_and(|s| {
        s.has_self_receiver
            && first_arg_ownership(s) == Some(OwnershipMode::Borrowed)
            && first_arg_type(s).is_some_and(is_reference_type)
    })
}

/// Is this an option accessor that may need `.cloned()` on borrowed receivers?
/// e.g. first, last, unwrap on borrowed Option/Vec
pub fn method_is_option_accessor_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    let sig =
        lookup_sig(method, receiver_type, registry).or_else(|| lookup_suffix(method, registry));
    sig.is_some_and(|s| {
        s.has_self_receiver
            && return_type_is(s, |ty| {
                matches!(ty,
                    Type::Option(inner) if matches!(&**inner, Type::Reference(_))
                ) || matches!(ty, Type::Custom(n) if n == "T")
            })
    })
}

/// Is the first non-self param a capacity/index arg that should be cast to `usize`?
pub fn method_is_capacity_cast_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    let sig =
        lookup_sig(method, receiver_type, registry).or_else(|| lookup_suffix(method, registry));
    sig.is_some_and(|s| first_arg_type(s).is_some_and(is_usize_type))
}

/// Does the first non-self param take a usize index?
pub fn method_is_index_taking_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    method_is_capacity_cast_qualified(method, receiver_type, registry)
}

/// Does this method take a closure/predicate as its first non-self argument?
pub fn method_is_closure_taking_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    let sig =
        lookup_sig(method, receiver_type, registry).or_else(|| lookup_suffix(method, registry));
    sig.is_some_and(|s| first_arg_type(s).is_some_and(is_closure_type))
}

/// Is this a slice search method (`contains`, `binary_search`) whose first arg
/// needs `&T`?
pub fn method_is_slice_search_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    let sig =
        lookup_sig(method, receiver_type, registry).or_else(|| lookup_suffix(method, registry));
    sig.is_some_and(|s| {
        s.has_self_receiver
            && first_arg_ownership(s) == Some(OwnershipMode::Borrowed)
            && first_arg_type(s).is_some_and(|ty| is_reference_type(ty) && !is_str_reference(ty))
    })
}

/// Is this a string search method (`starts_with`, `ends_with`, `contains`)
/// whose first arg needs `&str`?
pub fn method_is_string_search_qualified(
    method: &str,
    receiver_type: Option<&str>,
    registry: &SignatureRegistry,
) -> bool {
    let sig =
        lookup_sig(method, receiver_type, registry).or_else(|| lookup_suffix(method, registry));
    sig.is_some_and(|s| {
        s.has_self_receiver
            && first_arg_ownership(s) == Some(OwnershipMode::Borrowed)
            && first_arg_type(s).is_some_and(is_str_reference)
    })
}

// ── Convenience wrappers (no receiver type) ──────────────────────────────
// Used at call sites that lack receiver type context.

pub fn method_mutates_receiver(method: &str) -> bool {
    matches!(
        method,
        "push"
            | "pop"
            | "insert"
            | "remove"
            | "clear"
            | "append"
            | "extend"
            | "drain"
            | "truncate"
            | "resize"
            | "retain"
            | "sort"
            | "sort_by"
            | "sort_by_key"
            | "sort_unstable"
            | "sort_unstable_by"
            | "dedup"
            | "reverse"
            | "swap"
            | "swap_remove"
            | "reserve"
            | "shrink_to_fit"
            | "split_off"
            | "fill"
            | "set"
            | "rotate_left"
            | "rotate_right"
            | "set_len"
            | "push_str"
            | "push_front"
            | "push_back"
            | "pop_front"
            | "pop_back"
            | "make_ascii_lowercase"
            | "make_ascii_uppercase"
            | "add"
            | "take"
            | "replace"
            | "get_or_insert"
            | "get_or_insert_with"
            | "entry"
            | "get_mut"
            | "iter_mut"
            | "values_mut"
    )
}

pub fn method_returns_iterator(method: &str) -> bool {
    matches!(
        method,
        "iter"
            | "iter_mut"
            | "into_iter"
            | "keys"
            | "values"
            | "values_mut"
            | "drain"
            | "lines"
            | "chars"
            | "bytes"
            | "split"
            | "split_whitespace"
            | "enumerate"
            | "windows"
            | "chunks"
            | "match_indices"
            | "rsplit"
            | "splitn"
    )
}

pub fn is_map_key_method(method: &str) -> bool {
    matches!(
        method,
        "get" | "get_mut" | "contains_key" | "remove" | "get_key_value"
    )
}

pub fn is_index_taking_method(method: &str) -> bool {
    matches!(
        method,
        "insert" | "remove" | "swap" | "swap_remove" | "drain" | "split_off"
    )
}

pub fn is_closure_taking_method(method: &str) -> bool {
    matches!(
        method,
        "filter"
            | "any"
            | "all"
            | "find"
            | "find_map"
            | "position"
            | "take_while"
            | "skip_while"
            | "map_while"
            | "partition"
            | "rposition"
            | "retain"
            | "sort_by"
            | "sort_by_key"
            | "sort_unstable_by"
    )
}

/// Module names from `use std::…` that map to `windjammer_runtime::*` imports.
pub fn is_runtime_std_module(name: &str) -> bool {
    matches!(
        name,
        "strings"
            | "json"
            | "time"
            | "math"
            | "random"
            | "http"
            | "mime"
            | "subprocess"
            | "async_runtime"
            | "async"
            | "cli"
            | "crypto"
            | "csv"
            | "db"
            | "regex"
            | "testing"
            | "game"
            | "env"
    )
}

/// Runtime std modules whose Rust implementations take `AsRef<str>` for Windjammer `string` params.
pub fn runtime_std_module_uses_asref_str(module: &str) -> bool {
    matches!(
        module,
        "strings" | "json" | "regex" | "csv" | "mime" | "http" | "env"
    )
}

/// Check if a runtime std module function parameter should be auto-borrowed.
///
/// In `windjammer_runtime`, some modules take non-Copy struct parameters by reference
/// (`&T`) in the Rust implementation, but WJ stdlib declarations use owned types
/// (since WJ infers ownership). This function identifies those params so the codegen
/// can insert `&` at call sites.
///
/// This is module-specific because conventions differ:
/// - `json::get(value: &Value, ...)` — Rust takes `&Value`
/// - `subprocess::wait(handle: SubprocessHandle)` — Rust takes owned
pub fn runtime_std_param_needs_auto_borrow(
    module: &str,
    _func: &str,
    param_type: &crate::parser::Type,
) -> bool {
    use crate::parser::Type;
    match module {
        "json" => {
            // All json functions take Value params by reference (&Value) in Rust,
            // except constructors (object/array/null/boolean/number_*/json_string)
            // which don't take Value params at all.
            matches!(param_type, Type::Custom(name) if name == "Value")
        }
        _ => false,
    }
}