llts_analysis 0.1.0

LLTS analysis — subset validation and type resolution
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
use std::collections::HashMap;

use oxc_ast::ast::*;
use oxc_span::Span;

use crate::types::LltsType;

// ---------------------------------------------------------------------------
// Borrow state
// ---------------------------------------------------------------------------

/// The borrow state of a variable at a program point.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BorrowState {
    /// Not currently borrowed.
    Unborrowed,
    /// Immutably borrowed (multiple readers allowed).
    ImmutableBorrow { count: u32 },
    /// Mutably borrowed (exclusive access).
    MutableBorrow,
    /// Value has been moved out; no access allowed.
    Moved,
}

/// A record of an active borrow.
#[derive(Debug, Clone)]
pub struct BorrowRecord {
    pub variable: String,
    pub borrow_kind: BorrowKind,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BorrowKind {
    Immutable,
    Mutable,
}

// ---------------------------------------------------------------------------
// Borrow errors
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct BorrowError {
    pub span: Span,
    pub kind: BorrowErrorKind,
}

#[derive(Debug, Clone)]
pub enum BorrowErrorKind {
    /// Attempt to mutably borrow a variable that has outstanding immutable borrows.
    MutableBorrowWhileImmutablelyBorrowed { name: String },
    /// Attempt to borrow (immutably) a variable that is mutably borrowed.
    ImmutableBorrowWhileMutablyBorrowed { name: String },
    /// Attempt to mutably borrow a variable that is already mutably borrowed.
    DoubleMutableBorrow { name: String },
    /// Attempt to mutate through a `Readonly<T>` reference.
    MutateReadonly { name: String },
    /// Use of a variable after it has been moved.
    UseAfterMove { name: String },
}

impl std::fmt::Display for BorrowError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            BorrowErrorKind::MutableBorrowWhileImmutablelyBorrowed { name } => {
                write!(f, "cannot mutably borrow `{name}` while it is immutably borrowed")
            }
            BorrowErrorKind::ImmutableBorrowWhileMutablyBorrowed { name } => {
                write!(f, "cannot borrow `{name}` while it is mutably borrowed")
            }
            BorrowErrorKind::DoubleMutableBorrow { name } => {
                write!(f, "cannot mutably borrow `{name}` more than once at a time")
            }
            BorrowErrorKind::MutateReadonly { name } => {
                write!(f, "cannot mutate `{name}` through Readonly reference")
            }
            BorrowErrorKind::UseAfterMove { name } => {
                write!(f, "use of moved variable `{name}`")
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Borrow checker
// ---------------------------------------------------------------------------

pub struct BorrowChecker {
    /// Variable -> borrow state.
    states: HashMap<String, BorrowState>,
    /// Variable -> whether the type is Readonly<T>.
    readonly: HashMap<String, bool>,
    /// Variable -> whether the type is Copy (primitives, enums without payload).
    copy_types: HashMap<String, bool>,
    errors: Vec<BorrowError>,
}

impl BorrowChecker {
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
            readonly: HashMap::new(),
            copy_types: HashMap::new(),
            errors: Vec::new(),
        }
    }

    /// Register a variable for tracking.
    pub fn declare(&mut self, name: String, ty: &LltsType) {
        self.states.insert(name.clone(), BorrowState::Unborrowed);
        let is_readonly = matches!(ty, LltsType::Readonly(_));
        self.readonly.insert(name.clone(), is_readonly);
        self.copy_types.insert(name, ty.is_copy());
    }

    /// Record an immutable borrow of a variable.
    pub fn borrow_immutable(&mut self, name: &str, span: Span) {
        match self.states.get(name).copied() {
            Some(BorrowState::Moved) => {
                self.errors.push(BorrowError {
                    span,
                    kind: BorrowErrorKind::UseAfterMove {
                        name: name.to_string(),
                    },
                });
            }
            Some(BorrowState::MutableBorrow) => {
                self.errors.push(BorrowError {
                    span,
                    kind: BorrowErrorKind::ImmutableBorrowWhileMutablyBorrowed {
                        name: name.to_string(),
                    },
                });
            }
            Some(BorrowState::ImmutableBorrow { count }) => {
                self.states.insert(
                    name.to_string(),
                    BorrowState::ImmutableBorrow { count: count + 1 },
                );
            }
            Some(BorrowState::Unborrowed) => {
                self.states.insert(
                    name.to_string(),
                    BorrowState::ImmutableBorrow { count: 1 },
                );
            }
            None => {
                // Variable not tracked (may be from outer scope)
            }
        }
    }

    /// Record a mutable borrow of a variable.
    pub fn borrow_mutable(&mut self, name: &str, span: Span) {
        // Check Readonly constraint
        if self.readonly.get(name).copied().unwrap_or(false) {
            self.errors.push(BorrowError {
                span,
                kind: BorrowErrorKind::MutateReadonly {
                    name: name.to_string(),
                },
            });
            return;
        }

        match self.states.get(name).copied() {
            Some(BorrowState::Moved) => {
                self.errors.push(BorrowError {
                    span,
                    kind: BorrowErrorKind::UseAfterMove {
                        name: name.to_string(),
                    },
                });
            }
            Some(BorrowState::MutableBorrow) => {
                self.errors.push(BorrowError {
                    span,
                    kind: BorrowErrorKind::DoubleMutableBorrow {
                        name: name.to_string(),
                    },
                });
            }
            Some(BorrowState::ImmutableBorrow { .. }) => {
                self.errors.push(BorrowError {
                    span,
                    kind: BorrowErrorKind::MutableBorrowWhileImmutablelyBorrowed {
                        name: name.to_string(),
                    },
                });
            }
            Some(BorrowState::Unborrowed) => {
                self.states
                    .insert(name.to_string(), BorrowState::MutableBorrow);
            }
            None => {}
        }
    }

    /// Release a borrow (e.g. at end of the borrowing scope).
    pub fn release_borrow(&mut self, name: &str) {
        match self.states.get(name).copied() {
            Some(BorrowState::ImmutableBorrow { count }) if count > 1 => {
                self.states.insert(
                    name.to_string(),
                    BorrowState::ImmutableBorrow { count: count - 1 },
                );
            }
            Some(BorrowState::ImmutableBorrow { .. }) | Some(BorrowState::MutableBorrow) => {
                self.states
                    .insert(name.to_string(), BorrowState::Unborrowed);
            }
            _ => {}
        }
    }

    /// Mark a variable as moved. Copy types are never moved.
    pub fn mark_moved(&mut self, name: &str, span: Span) {
        // Copy types are implicitly copied, never moved.
        if self.copy_types.get(name).copied().unwrap_or(false) {
            return;
        }
        if let Some(state) = self.states.get(name).copied() {
            if state == BorrowState::Moved {
                self.errors.push(BorrowError {
                    span,
                    kind: BorrowErrorKind::UseAfterMove {
                        name: name.to_string(),
                    },
                });
                return;
            }
        }
        self.states.insert(name.to_string(), BorrowState::Moved);
    }

    /// Check that a variable can be used (not moved).
    pub fn check_use(&mut self, name: &str, span: Span) {
        if let Some(BorrowState::Moved) = self.states.get(name) {
            self.errors.push(BorrowError {
                span,
                kind: BorrowErrorKind::UseAfterMove {
                    name: name.to_string(),
                },
            });
        }
    }

    /// Check that a variable can be mutated (not readonly, not immutably borrowed).
    pub fn check_mutation(&mut self, name: &str, span: Span) {
        if self.readonly.get(name).copied().unwrap_or(false) {
            self.errors.push(BorrowError {
                span,
                kind: BorrowErrorKind::MutateReadonly {
                    name: name.to_string(),
                },
            });
        }
    }

    /// Run a basic borrow check pass over a function body.
    pub fn check_function(&mut self, func: &Function<'_>, resolve_type: &dyn Fn(&TSType<'_>) -> LltsType) {
        // Register parameters
        for param in &func.params.items {
            let name = binding_pattern_name(&param.pattern);
            let ty = param
                .type_annotation
                .as_ref()
                .map(|ann| resolve_type(&ann.type_annotation))
                .unwrap_or(LltsType::Unknown);
            self.declare(name, &ty);
        }

        // Walk the body
        if let Some(body) = &func.body {
            for stmt in &body.statements {
                self.check_statement(stmt, resolve_type);
            }
        }
    }

    fn check_statement(&mut self, stmt: &Statement<'_>, resolve_type: &dyn Fn(&TSType<'_>) -> LltsType) {
        match stmt {
            Statement::VariableDeclaration(decl) => {
                for declarator in &decl.declarations {
                    let name = binding_pattern_name(&declarator.id);
                    let ty = declarator
                        .type_annotation
                        .as_ref()
                        .map(|ann| resolve_type(&ann.type_annotation))
                        .unwrap_or(LltsType::Unknown);
                    self.declare(name, &ty);
                    if let Some(init) = &declarator.init {
                        self.check_expression(init);
                    }
                }
            }
            Statement::ExpressionStatement(expr_stmt) => {
                self.check_expression(&expr_stmt.expression);
            }
            Statement::ReturnStatement(ret) => {
                if let Some(arg) = &ret.argument {
                    // Returning a variable moves it
                    if let Expression::Identifier(ident) = arg {
                        self.mark_moved(ident.name.as_str(), ident.span);
                    } else {
                        self.check_expression(arg);
                    }
                }
            }
            Statement::BlockStatement(block) => {
                for s in &block.body {
                    self.check_statement(s, resolve_type);
                }
            }
            Statement::IfStatement(if_stmt) => {
                self.check_expression(&if_stmt.test);
                self.check_statement(&if_stmt.consequent, resolve_type);
                if let Some(alt) = &if_stmt.alternate {
                    self.check_statement(alt, resolve_type);
                }
            }
            _ => {}
        }
    }

    fn check_expression(&mut self, expr: &Expression<'_>) {
        match expr {
            Expression::Identifier(ident) => {
                self.check_use(ident.name.as_str(), ident.span);
            }
            Expression::AssignmentExpression(assign) => {
                // Check mutation on the target
                if let Some(name) = assignment_target_name(&assign.left) {
                    self.check_mutation(&name, assign.span);
                }
                self.check_expression(&assign.right);
            }
            Expression::CallExpression(call) => {
                // Check for mutating method calls
                if let Expression::StaticMemberExpression(member) = &call.callee {
                    let method = member.property.name.as_str();
                    if is_mutating_method(method) {
                        if let Expression::Identifier(ident) = &member.object {
                            self.check_mutation(ident.name.as_str(), call.span);
                            self.borrow_mutable(ident.name.as_str(), call.span);
                        }
                    } else {
                        if let Expression::Identifier(ident) = &member.object {
                            self.borrow_immutable(ident.name.as_str(), call.span);
                        }
                    }
                }
                for arg in &call.arguments {
                    match arg {
                        Argument::SpreadElement(spread) => {
                            self.check_expression(&spread.argument);
                        }
                        _ => {
                            self.check_expression(arg.to_expression());
                        }
                    }
                }
            }
            Expression::StaticMemberExpression(member) => {
                self.check_expression(&member.object);
            }
            Expression::BinaryExpression(binary) => {
                self.check_expression(&binary.left);
                self.check_expression(&binary.right);
            }
            Expression::UnaryExpression(unary) => {
                self.check_expression(&unary.argument);
            }
            _ => {}
        }
    }

    /// Consume the checker and return all errors.
    pub fn finish(self) -> Vec<BorrowError> {
        self.errors
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn binding_pattern_name(pattern: &BindingPattern<'_>) -> String {
    match pattern {
        BindingPattern::BindingIdentifier(id) => id.name.to_string(),
        _ => "_".to_string(),
    }
}

fn assignment_target_name(target: &AssignmentTarget<'_>) -> Option<String> {
    match target {
        AssignmentTarget::AssignmentTargetIdentifier(ident) => Some(ident.name.to_string()),
        AssignmentTarget::StaticMemberExpression(member) => {
            if let Expression::Identifier(ident) = &member.object {
                Some(ident.name.to_string())
            } else {
                None
            }
        }
        _ => None,
    }
}

fn is_mutating_method(method: &str) -> bool {
    matches!(
        method,
        "push"
            | "pop"
            | "shift"
            | "unshift"
            | "splice"
            | "sort"
            | "reverse"
            | "fill"
            | "copyWithin"
            | "set"
            | "delete"
            | "clear"
    )
}