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
//! Rust Leakage Detection
//!
//! Rejects Rust-specific patterns in Windjammer code.
//! These patterns expose Rust internals that the compiler handles automatically.

use crate::error::SourceLocation;
use crate::linter::{LintCategory, LintCollector, LintDiagnostic, LintLevel};
use crate::parser::ast::core::{Expression, FunctionDecl, Item, Parameter, Program, Statement};
use crate::parser::ast::operators::UnaryOp;
use crate::parser::ast::types::Type;
use crate::parser::ast::OwnershipHint;
use crate::source_map::Location;

/// Convert AST location to error SourceLocation
fn to_source_location(loc: Option<Location>, default_file: &str) -> SourceLocation {
    loc.map(|l| SourceLocation {
        file: l.file.to_string_lossy().to_string(),
        line: l.line,
        column: l.column,
    })
    .unwrap_or_else(|| SourceLocation::new(default_file, 1, 1))
}

/// Rust Leakage Linter - detects Rust-specific patterns in Windjammer source
pub struct RustLeakageLinter<'ast> {
    collector: LintCollector,
    default_file: String,
    /// When true, we're inside a trait impl - don't warn on &self/&mut self (trait requires it)
    in_trait_impl: bool,
    _phantom: std::marker::PhantomData<&'ast ()>,
}

impl<'ast> RustLeakageLinter<'ast> {
    pub fn new(default_file: impl Into<String>) -> Self {
        Self {
            collector: LintCollector::new(),
            default_file: default_file.into(),
            in_trait_impl: false,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Run all Rust leakage checks on a program
    pub fn lint_program(&mut self, program: &Program<'ast>) {
        for item in &program.items {
            self.check_item(item);
        }
    }

    fn check_item(&mut self, item: &Item<'ast>) {
        match item {
            Item::Function { decl, location } => {
                self.in_trait_impl = false;
                self.check_function(decl, location.clone());
            }
            Item::Impl { block, location } => {
                let prev = self.in_trait_impl;
                self.in_trait_impl = block.trait_name.is_some();
                for func in &block.functions {
                    self.check_function(func, location.clone());
                }
                self.in_trait_impl = prev;
            }
            Item::Mod { items, .. } => {
                for sub_item in items {
                    self.check_item(sub_item);
                }
            }
            _ => {}
        }
    }

    fn check_function(&mut self, func: &FunctionDecl<'ast>, fallback_loc: Option<Location>) {
        let file = fallback_loc
            .as_ref()
            .map(|l| l.file.to_string_lossy().to_string())
            .unwrap_or_else(|| self.default_file.clone());

        // W0001: Explicit ownership annotations - skip for extern fn and trait impls
        if !func.is_extern && !self.in_trait_impl {
            for param in &func.parameters {
                self.check_explicit_ownership(param, &file);
                self.check_param_type(param, &file);
            }
        }

        // Check body for unwrap, iter, explicit borrows
        for stmt in &func.body {
            self.check_statement(stmt, &file);
        }
    }

    /// W0001: Explicit ownership annotations (&self, &mut self, &T in params)
    fn check_explicit_ownership(&mut self, param: &Parameter<'ast>, file: &str) {
        let (hint, param_name) = match &param.ownership {
            OwnershipHint::Ref => ("&", param.name.as_str()),
            OwnershipHint::Mut => ("&mut", param.name.as_str()),
            _ => return,
        };

        // For self parameter, suggest "self"
        let suggestion = if param_name == "self" {
            "use inferred ownership: `self`".to_string()
        } else {
            format!("use inferred ownership: `{}`", param_name)
        };

        let note = if param_name == "self" {
            if hint == "&mut" {
                "the compiler will add `&mut` based on usage".to_string()
            } else {
                "Windjammer infers ownership automatically".to_string()
            }
        } else {
            "Windjammer infers borrowing automatically".to_string()
        };

        // Parameter doesn't have its own location - use a default
        let location = SourceLocation::new(file, 1, 1);

        self.collector.add(LintDiagnostic {
            lint_name: "W0001".to_string(),
            category: LintCategory::Style,
            level: LintLevel::Error,
            message: "explicit ownership annotation".to_string(),
            location,
            help: Some(suggestion.clone()),
            note: Some(note),
            suggestion: Some(suggestion),
        });
    }

    fn check_statement(&mut self, stmt: &Statement<'ast>, file: &str) {
        match stmt {
            Statement::Let {
                value, else_block, ..
            } => {
                self.check_expression(value, file);
                if let Some(block) = else_block {
                    for s in block {
                        self.check_statement(s, file);
                    }
                }
            }
            Statement::Expression { expr, .. } => self.check_expression(expr, file),
            Statement::Assignment { target, value, .. } => {
                self.check_expression(target, file);
                self.check_expression(value, file);
            }
            Statement::Return {
                value: Some(expr), ..
            } => self.check_expression(expr, file),
            Statement::If {
                condition,
                then_block,
                else_block,
                ..
            } => {
                self.check_expression(condition, file);
                for s in then_block {
                    self.check_statement(s, file);
                }
                if let Some(block) = else_block {
                    for s in block {
                        self.check_statement(s, file);
                    }
                }
            }
            Statement::Match { value, arms, .. } => {
                self.check_expression(value, file);
                for arm in arms {
                    self.check_expression(arm.body, file);
                }
            }
            Statement::For { iterable, body, .. } => {
                self.check_expression(iterable, file);
                for s in body {
                    self.check_statement(s, file);
                }
            }
            Statement::While {
                condition, body, ..
            } => {
                self.check_expression(condition, file);
                for s in body {
                    self.check_statement(s, file);
                }
            }
            Statement::Loop { body, .. }
            | Statement::Thread { body, .. }
            | Statement::Async { body, .. } => {
                for s in body {
                    self.check_statement(s, file);
                }
            }
            Statement::Defer { statement, .. } => self.check_statement(statement, file),
            _ => {}
        }
    }

    fn check_expression(&mut self, expr: &Expression<'ast>, file: &str) {
        match expr {
            Expression::MethodCall {
                method,
                object,
                arguments,
                location,
                ..
            } => {
                // W0002: .unwrap() and .expect()
                if method == "unwrap" || method == "expect" {
                    let loc = to_source_location(location.clone(), &self.default_file);
                    self.collector.add(LintDiagnostic {
                        lint_name: "W0002".to_string(),
                        category: LintCategory::Correctness,
                        level: LintLevel::Error,
                        message: format!("explicit .{}() call", method),
                        location: loc,
                        help: Some(
                            "use explicit error handling: `if let Some(...)` or `match`"
                                .to_string(),
                        ),
                        note: Some(".unwrap() is a Rust-specific panic convention".to_string()),
                        suggestion: Some(
                            "prefer explicit error handling in Windjammer".to_string(),
                        ),
                    });
                }

                // W0003: .iter() and .iter_mut()
                if method == "iter" || method == "iter_mut" {
                    let loc = to_source_location(location.clone(), &self.default_file);
                    self.collector.add(LintDiagnostic {
                        lint_name: "W0003".to_string(),
                        category: LintCategory::Style,
                        level: LintLevel::Error,
                        message: "explicit .iter() call".to_string(),
                        location: loc,
                        help: Some("use direct iteration: `for x in collection`".to_string()),
                        note: Some("Windjammer supports direct iteration".to_string()),
                        suggestion: Some(
                            "remove .iter() - use the collection directly".to_string(),
                        ),
                    });
                }

                // W0005: .clone() - Rust leakage, compiler handles cloning automatically
                if method == "clone" && arguments.is_empty() {
                    let loc = to_source_location(location.clone(), &self.default_file);
                    self.collector.add(LintDiagnostic {
                        lint_name: "W0005".to_string(),
                        category: LintCategory::Style,
                        level: LintLevel::Error,
                        message: "explicit .clone() call".to_string(),
                        location: loc,
                        help: Some(
                            "remove .clone() - the compiler infers cloning automatically"
                                .to_string(),
                        ),
                        note: Some(
                            "Windjammer auto-clones values when moved and used again".to_string(),
                        ),
                        suggestion: Some(
                            "remove .clone() and let the compiler handle it".to_string(),
                        ),
                    });
                }

                self.check_expression(object, file);
                for (_, arg) in arguments {
                    self.check_expression(arg, file);
                }
            }
            Expression::Call {
                function,
                arguments,
                location: _,
                ..
            } => {
                // W0004: Explicit borrowing in function call args (&expr)
                for (_, arg) in arguments {
                    self.check_call_argument(arg, file);
                }
                self.check_expression(function, file);
            }
            Expression::Unary {
                op: _,
                operand,
                location: _,
                ..
            } => {
                // W0004: &expr or &mut expr used as standalone (e.g. in assignment)
                // We check this in check_call_argument for Call args
                // For Unary in other contexts, we could warn - but &x in let y = &x might be intentional
                // The design says to check "explicit borrow in function call" - so we focus on Call args
                self.check_expression(operand, file);
            }
            Expression::Binary { left, right, .. } => {
                self.check_expression(left, file);
                self.check_expression(right, file);
            }
            Expression::FieldAccess { object, .. } => self.check_expression(object, file),
            Expression::Index { object, index, .. } => {
                self.check_expression(object, file);
                self.check_expression(index, file);
            }
            Expression::StructLiteral { fields, .. } => {
                for (_, value) in fields {
                    self.check_expression(value, file);
                }
            }
            Expression::Array { elements, .. } => {
                for elem in elements {
                    self.check_expression(elem, file);
                }
            }
            Expression::Tuple { elements, .. } => {
                for elem in elements {
                    self.check_expression(elem, file);
                }
            }
            Expression::Block { statements, .. } => {
                for stmt in statements {
                    self.check_statement(stmt, file);
                }
            }
            Expression::Closure { body, .. } => self.check_expression(body, file),
            Expression::Cast { expr, .. } => self.check_expression(expr, file),
            Expression::Range { start, end, .. } => {
                self.check_expression(start, file);
                self.check_expression(end, file);
            }
            Expression::MapLiteral { pairs, .. } => {
                for (k, v) in pairs {
                    self.check_expression(k, file);
                    self.check_expression(v, file);
                }
            }
            Expression::TryOp { expr, .. } => self.check_expression(expr, file),
            Expression::Await { expr, .. } => self.check_expression(expr, file),
            Expression::ChannelSend { channel, value, .. } => {
                self.check_expression(channel, file);
                self.check_expression(value, file);
            }
            Expression::ChannelRecv { channel, .. } => self.check_expression(channel, file),
            Expression::MacroInvocation { args, .. } => {
                for arg in args {
                    self.check_expression(arg, file);
                }
            }
            Expression::Literal { .. } | Expression::Identifier { .. } => {}
        }
    }

    /// W0004: Check if a function call argument is an explicit borrow (&expr)
    fn check_call_argument(&mut self, arg: &Expression<'ast>, file: &str) {
        if let Expression::Unary {
            op: UnaryOp::Ref | UnaryOp::MutRef,
            operand: _,
            location,
            ..
        } = arg
        {
            let loc = to_source_location(location.clone(), &self.default_file);
            self.collector.add(LintDiagnostic {
                lint_name: "W0004".to_string(),
                category: LintCategory::Style,
                level: LintLevel::Error,
                message: "explicit borrow in function call".to_string(),
                location: loc,
                help: Some("remove explicit borrow - pass value directly".to_string()),
                note: Some("Windjammer infers borrowing automatically".to_string()),
                suggestion: Some("remove `&` or `&mut`".to_string()),
            });
        } else {
            self.check_expression(arg, file);
        }
    }

    /// Check for explicit reference types in parameters (e.g. &str, &Camera)
    fn check_param_type(&mut self, param: &Parameter<'ast>, file: &str) {
        match &param.type_ {
            Type::Reference(_) | Type::MutableReference(_) => {
                let location = SourceLocation::new(file, 1, 1);
                self.collector.add(LintDiagnostic {
                    lint_name: "W0001".to_string(),
                    category: LintCategory::Style,
                    level: LintLevel::Error,
                    message: "explicit reference type in parameter".to_string(),
                    location,
                    help: Some(format!(
                        "use inferred type: remove `&` from `{}`",
                        param.name
                    )),
                    note: Some("Windjammer infers borrowing automatically".to_string()),
                    suggestion: Some(format!("use `{}` without reference type", param.name)),
                });
            }
            _ => {}
        }
    }

    pub fn into_diagnostics(self) -> Vec<LintDiagnostic> {
        self.collector.into_diagnostics()
    }

    pub fn diagnostics(&self) -> &[LintDiagnostic] {
        self.collector.diagnostics()
    }
}

/// Run the Rust leakage linter on a program and emit diagnostics to stderr.
/// Returns `Err` if any diagnostic is an error-level lint.
pub fn run_lint_if_enabled(
    enable_lint: bool,
    file: &std::path::Path,
    program: &Program,
) -> Result<(), String> {
    if !enable_lint {
        return Ok(());
    }
    let file_name = file.to_string_lossy().to_string();
    let mut linter = RustLeakageLinter::new(&file_name);
    linter.lint_program(program);
    let mut has_errors = false;
    for diag in linter.diagnostics() {
        eprintln!("{}", diag);
        if diag.level == LintLevel::Error {
            has_errors = true;
        }
    }
    if has_errors {
        Err(format!(
            "Rust leakage errors in {} -- fix or use `extern fn` for FFI",
            file_name
        ))
    } else {
        Ok(())
    }
}