submilli-engine 0.3.0

Compiles Submilli programs, a TypeScript subset, to WebAssembly and runs them under capability policy
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
//! Step 2 — top-level let/const + non-decl statements (source order).

use crate::compiler_error::CompilerFailure;

use crate::{ClassMember, StmtKind, TypedStmt, TypedStmtKind, ValueKind};

use std::collections::BTreeSet;

use super::Inferer;
use super::narrowing::BindingId;

impl<'a> Inferer<'a> {
    pub(super) fn infer_global_variables(&mut self) -> Result<(), CompilerFailure> {
        let top_level: Vec<_> = self.ast.top_level.clone();
        self.collect_later_globals()?;
        // Assignments to module `let`s narrow them for later top-level
        // statements. Function bodies are inferred after this frame is gone.
        self.push_narrow_frame(super::narrowing::NarrowEnv::new());
        for stmt_id in top_level {
            self.keep_only_global_narrowings();
            let stmt = self
                .ast
                .try_stmt(stmt_id)
                .map_err(super::arena_failure)?
                .clone();
            let span = stmt.span;
            match stmt.kind {
                // Functions are deferred to step 3 so their bodies see the
                // complete top-level symbol table.
                StmtKind::Function { .. } => {}
                StmtKind::InterfaceDecl { .. }
                | StmtKind::EnumDecl { .. }
                | StmtKind::TypeAliasDecl { .. } => {}
                // Static fields are module globals: their initializers
                // evaluate here, in source order interleaved with `let`/`const` —
                // the TS module-initialization order.
                StmtKind::ClassDecl { name, members, .. } => {
                    self.record_class_declaration_point(&name.name);
                    self.infer_static_field_globals(&name, &members)?;
                }
                StmtKind::Import { .. } => {}
                StmtKind::Let {
                    name,
                    ty,
                    value,
                    doc,
                } => {
                    if self.reject_intrinsic_name(&name) {
                        self.forget_later_global(&name.name);
                        continue;
                    }
                    let hint = ty.as_ref().map(|a| self.resolve_type(a)).transpose()?;
                    let (typed_value, value_ty) =
                        self.infer_expr_keeping_literals(value, hint.as_ref(), true)?;
                    // Reassignable, so a fresh literal widens; see the block-scoped
                    // `Let` arm in `stmt.rs`.
                    let bound = match hint {
                        Some(hint) => hint,
                        None => self.widen_fresh_literals(typed_value, &value_ty)?,
                    };
                    let bound = self.global_storage_ty(&name.name, bound);
                    self.finish_later_global(&name, &bound)?;
                    self.bind_top(
                        &name,
                        ValueKind::Let {
                            ty: bound.clone(),
                            doc: doc.clone(),
                        },
                    )?;
                    let mangled = self.mangle_top_symbol(&name.name)?;
                    let origin =
                        self.initializer_literal_origin(ty.is_some(), typed_value, &bound)?;
                    self.record_global_literal_origin(mangled.clone(), origin);
                    self.typed_ast
                        .rebindable_globals
                        .insert(mangled.clone(), name.name.clone());
                    self.add_typed_global(crate::TypedGlobal {
                        name: name.clone(),
                        mangled_name: mangled.clone(),
                        ty: bound.clone(),
                        kind: crate::GlobalKind::Let,
                        doc,
                        span,
                    })?;
                    let annotated = ty.is_some();
                    self.narrow_global_initializer(
                        &name,
                        &mangled,
                        &bound,
                        annotated,
                        typed_value,
                        value_ty,
                    )?;
                    let assign_id = self
                        .typed_ast
                        .try_push_stmt(TypedStmt {
                            kind: TypedStmtKind::AssignGlobal {
                                ident: name,
                                mangled,
                                target_ty: bound,
                                value: typed_value,
                            },
                            span,
                        })
                        .map_err(crate::typechecker::arena_failure)?;
                    self.typed_ast.top_level_statements.push(assign_id);
                }
                StmtKind::Const {
                    name,
                    ty,
                    value,
                    doc,
                } => {
                    if self.reject_intrinsic_name(&name) {
                        self.forget_later_global(&name.name);
                        continue;
                    }
                    // Keeps the literal types its value passes through; see the
                    // block-scoped `Const` arm in `stmt.rs`.
                    let hint = ty.as_ref().map(|a| self.resolve_type(a)).transpose()?;
                    let (typed_value, value_ty) =
                        self.infer_expr_keeping_literals(value, hint.as_ref(), hint.is_none())?;
                    let origin =
                        self.initializer_literal_origin(ty.is_some(), typed_value, &value_ty)?;
                    let bound = hint.unwrap_or(value_ty);
                    self.finish_later_global(&name, &bound)?;
                    self.bind_top(
                        &name,
                        ValueKind::Const {
                            ty: bound.clone(),
                            doc: doc.clone(),
                        },
                    )?;
                    let mangled = self.mangle_top_symbol(&name.name)?;
                    self.record_global_literal_origin(mangled.clone(), origin);
                    if ty.is_none() {
                        self.record_global_aliased_condition(mangled.clone(), typed_value);
                    }
                    self.add_typed_global(crate::TypedGlobal {
                        name: name.clone(),
                        mangled_name: mangled.clone(),
                        ty: bound.clone(),
                        kind: crate::GlobalKind::Const,
                        doc,
                        span,
                    })?;
                    let assign_id = self
                        .typed_ast
                        .try_push_stmt(TypedStmt {
                            kind: TypedStmtKind::AssignGlobal {
                                ident: name,
                                mangled,
                                target_ty: bound,
                                value: typed_value,
                            },
                            span,
                        })
                        .map_err(crate::typechecker::arena_failure)?;
                    self.typed_ast.top_level_statements.push(assign_id);
                }
                _ => {
                    if let Some(typed_id) = self.infer_stmt(stmt_id)? {
                        self.typed_ast.top_level_statements.push(typed_id);
                    }
                }
            }
        }
        self.pop_narrow_frame()?;
        Ok(())
    }

    /// A module `let` declared as a union starts narrowed to its initializer,
    /// as a local one does, unless the initializer was rejected. `annotated`
    /// tells whether `declared` was written or inferred.
    fn narrow_global_initializer(
        &mut self,
        name: &crate::Ident,
        mangled: &crate::MangledName,
        declared: &crate::Type,
        annotated: bool,
        value: crate::ExprId,
        value_ty: crate::Type,
    ) -> Result<(), CompilerFailure> {
        if !super::stmt::initializer_may_narrow(declared, &value_ty)
            || !super::assignable(&value_ty, declared, self.resolver())
        {
            return Ok(());
        }
        let flow_ty = self.assigned_flow_type(declared, annotated, value, value_ty)?;
        let narrowed = self.initializer_narrowed_ty(declared, flow_ty);
        self.renarrow_global_after_write(name, mangled, declared, narrowed)
    }

    /// Top-level statements are not wrapped in narrowing regions, so only a
    /// module variable's own narrowing, read live, carries to the next one,
    /// and only for a variable no function writes: a statement between may
    /// call that function.
    fn keep_only_global_narrowings(&mut self) {
        let written = self.function_written_global_names();
        let is_unwritten_global = |path: &super::narrowing::ReferencePath| {
            path.chain.is_empty()
                && matches!(&path.root, BindingId::Global(mangled) if !written.contains(mangled))
        };
        if let Some(env) = self.narrow_scopes.last_mut() {
            env.retain(|path, _| is_unwritten_global(path));
        }
    }

    /// The mangled names of the module variables some function assigns.
    fn function_written_global_names(&self) -> BTreeSet<crate::MangledName> {
        self.top_symbols
            .iter()
            .filter(|(name, _)| self.function_written_globals.contains(*name))
            .map(|(_, entry)| entry.mangled_name.clone())
            .collect()
    }

    fn record_class_declaration_point(&mut self, class_name: &str) {
        let Some(sym) = self.types.lookup(class_name) else {
            return;
        };
        let point = self.typed_ast.top_level_statements.len();
        self.typed_ast
            .class_declaration_points
            .insert(sym.mangled_name.clone(), point);
    }

    /// One module global per static field, keyed `Class#static#name`. Fields
    /// whose signature was rejected in `bind_class` (uninitialized) are absent
    /// from `static_fields` and skipped here.
    fn infer_static_field_globals(
        &mut self,
        class_name: &crate::Ident,
        members: &[ClassMember],
    ) -> Result<(), CompilerFailure> {
        let Some(sym) = self.types.lookup(&class_name.name) else {
            return Ok(());
        };
        let crate::TypeKind::Class { static_fields, .. } = &sym.kind else {
            return Ok(());
        };
        let static_fields = static_fields.clone();
        let class_mangled = sym.mangled_name.clone();
        for member in members {
            let ClassMember::Field {
                name,
                modifiers,
                initializer,
                doc,
                span,
                ..
            } = member
            else {
                continue;
            };
            if modifiers.static_span.is_none() {
                continue;
            }
            let (Some(sig), Some(init)) = (static_fields.get(&name.name), initializer) else {
                continue;
            };
            let prev_static = self
                .current_static
                .replace((class_name.name.clone(), name.name.clone()));
            let (typed_value, value_ty) = self.infer_expr(*init, Some(&sig.ty))?;
            self.current_static = prev_static;
            if !super::assignable(&value_ty, &sig.ty, self.resolver())
                && !matches!(value_ty, crate::Type::Error)
            {
                let init_span = self.ast.try_expr(*init).map_err(super::arena_failure)?.span;
                self.drop_contextual_mismatch(init_span, &sig.ty, &value_ty);
                self.error(
                    init_span,
                    format!(
                        "static field `{}` initializer is `{value_ty}`, expected `{}`",
                        name.name, sig.ty
                    ),
                );
            }
            let ident = crate::Ident {
                name: format!("{}.{}", class_name.name, name.name),
                span: name.span,
            };
            let mangled = crate::mangle::static_member(&class_mangled, &name.name);
            if !sig.readonly {
                self.typed_ast
                    .rebindable_globals
                    .insert(mangled.clone(), ident.name.clone());
            }
            self.add_typed_global(crate::TypedGlobal {
                name: ident.clone(),
                mangled_name: mangled.clone(),
                ty: sig.ty.clone(),
                kind: crate::GlobalKind::Const,
                doc: doc.clone(),
                span: *span,
            })?;
            let assign_id = self
                .typed_ast
                .try_push_stmt(TypedStmt {
                    kind: TypedStmtKind::AssignGlobal {
                        ident,
                        mangled,
                        target_ty: sig.ty.clone(),
                        value: typed_value,
                    },
                    span: *span,
                })
                .map_err(crate::typechecker::arena_failure)?;
            self.typed_ast.top_level_statements.push(assign_id);
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::super::test_support::{nth_decl_value_ty, run, run_clean};
    use crate::{PackageDeclaration, Type, ValueKind};

    #[test]
    fn let_without_annotation_infers_initializer_type() {
        let ta = run_clean("let x = 1;");
        // The initializer keeps its literal type; the binding widens it.
        assert_eq!(
            nth_decl_value_ty(&ta, 0),
            Type::NumberLiteral(crate::types::LiteralF64(1.0))
        );
        let reg = PackageDeclaration::from_typed_ast(&ta);
        match &reg.values.get("x").unwrap().kind {
            ValueKind::Let { ty, .. } => assert_eq!(*ty, Type::Number),
            _ => panic!("expected Let"),
        }
    }

    /// A `const` cannot be reassigned, so the literal type stays true for the whole
    /// program and is kept — as in TypeScript, where this is what lets a `const` be
    /// passed to a literal-union parameter. `let` widens; see
    /// `let_without_annotation_widens_to_the_base_primitive`.
    #[test]
    fn const_without_annotation_infers_the_literal_type() {
        let ta = run_clean(r#"const y = "hi";"#);
        assert_eq!(
            nth_decl_value_ty(&ta, 0),
            Type::StringLiteral("hi".to_string())
        );
        let reg = PackageDeclaration::from_typed_ast(&ta);
        match &reg.values.get("y").unwrap().kind {
            ValueKind::Const { ty, .. } => {
                assert_eq!(*ty, Type::StringLiteral("hi".to_string()));
            }
            _ => panic!("expected Const"),
        }
    }

    /// The reason the literal is kept: a `const` reaches a literal-union parameter,
    /// which is what `spec.md` lists literal types as being for.
    #[test]
    fn a_const_literal_is_accepted_by_a_literal_union_parameter() {
        let (_ta, diags) = run(
            "function f(t: \"a\" | \"b\"): number { return t === \"a\" ? 1 : 2; } \
             const t = \"a\"; function main(): void { const n: number = f(t); }",
        );
        assert!(diags.is_empty(), "unexpected diagnostics: {diags:?}");
    }

    /// A literal still reaches everything its base type does: arithmetic, methods,
    /// template interpolation, and a base-typed parameter.
    #[test]
    fn a_const_literal_behaves_as_its_base_type() {
        let (_ta, diags) = run("function g(s: string): number { return s.length; } \
             function main(): void { const n = 1; const m = n + 1; \
             const s = \"hi\"; const r = s.repeat(2); const t = `${n}`; \
             const l: number = g(s); }");
        assert!(diags.is_empty(), "unexpected diagnostics: {diags:?}");
    }

    #[test]
    fn let_without_annotation_widens_to_the_base_primitive() {
        let ta = run_clean(r#"let y = "hi";"#);
        let reg = PackageDeclaration::from_typed_ast(&ta);
        match &reg.values.get("y").unwrap().kind {
            ValueKind::Let { ty, .. } => assert_eq!(*ty, Type::String),
            _ => panic!("expected Let"),
        }
    }

    /// Only a bare literal keeps its type; a computed initializer widens even under
    /// `const`, matching TypeScript (`const a = 1 + 1` is `number`, not `2`).
    #[test]
    fn const_with_a_computed_initializer_widens() {
        let ta = run_clean("const y = 1 + 1;");
        assert_eq!(nth_decl_value_ty(&ta, 0), Type::Number);
    }

    /// The literal survives the `const` but not the `let`: a reassignable binding
    /// takes the base type, or its first write would be a type error. Asserted on the
    /// binding rather than the initializer, since the initializer expression `a` is
    /// still `1` — it is the binding that widens.
    #[test]
    fn let_initialized_from_a_const_literal_widens() {
        let ta = run_clean("const a = 1; let b = a;");
        let reg = PackageDeclaration::from_typed_ast(&ta);
        match &reg.values.get("b").unwrap().kind {
            ValueKind::Let { ty, .. } => assert_eq!(*ty, Type::Number),
            other => panic!("expected Let, got {other:?}"),
        }
    }

    /// Parentheses group, they do not compute, so the literal survives them.
    #[test]
    fn const_bound_to_a_parenthesized_literal_keeps_the_literal_type() {
        let ta = run_clean("const y = (1);");
        assert_eq!(
            nth_decl_value_ty(&ta, 0),
            Type::NumberLiteral(crate::types::LiteralF64(1.0))
        );
    }

    /// Reassigning a top-level `const` reports the reassignment and nothing else.
    /// Inferring the new value against the const's own literal type would always
    /// mismatch, stacking a spurious `expected 1, got 2` beneath the real error.
    #[test]
    fn reassigning_a_top_level_const_reports_only_the_reassignment() {
        let (_, diags) = run("const a = 1; function main(): void { a = 2; }");
        assert_eq!(diags.len(), 1, "unexpected diagnostics: {diags:?}");
        assert!(
            diags[0].message.contains("cannot assign to const binding"),
            "got: {}",
            diags[0].message
        );
    }

    #[test]
    fn top_level_forward_reference_in_initializer_diagnoses() {
        let (ta, diags) = run("let a: number = b; let b: number = 1;");
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].message, "unresolved identifier `b`");
        assert_eq!(nth_decl_value_ty(&ta, 0), Type::Error);
    }

    #[test]
    fn top_level_self_reference_diagnoses() {
        let (_, diags) = run("let a: number = a;");
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].message, "unresolved identifier `a`");
    }

    #[test]
    fn function_body_can_reference_later_let() {
        let _ = run_clean("function f(): number { return r; } let r: number = 1;");
    }

    #[test]
    fn let_initializer_can_call_function_declared_later() {
        let _ = run_clean("let a: number = f(); function f(): number { return 1; }");
    }

    #[test]
    fn let_inferred_from_call_propagates() {
        let ta = run_clean(r#"function f(): string { return "x"; } let s = f();"#);
        let reg = PackageDeclaration::from_typed_ast(&ta);
        match &reg.values.get("s").unwrap().kind {
            ValueKind::Let { ty, .. } => assert_eq!(*ty, Type::String),
            _ => panic!("expected Let"),
        }
    }

    #[test]
    fn let_inferred_used_later() {
        let _ = run_clean("let a = 1; let b: number = a;");
    }

    #[test]
    fn let_inferred_then_misused() {
        let (_, diags) = run("let a = 1; let b: string = a;");
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].message, "expected `string`, got `number`");
    }

    #[test]
    fn let_annotation_mismatch_inferred_initializer() {
        let (ta, diags) = run("let a: string = 1;");
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].message, "expected `string`, got `number`");
        let reg = PackageDeclaration::from_typed_ast(&ta);
        match &reg.values.get("a").unwrap().kind {
            ValueKind::Let { ty, .. } => assert_eq!(*ty, Type::String),
            _ => panic!("expected Let"),
        }
    }
}