submilli-engine 0.6.2

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
//! Upper bounds preserve a parameter's identity while exposing its members.

use std::collections::BTreeSet;

use crate::compiler_error::CompilerFailure;
use crate::typechecker::type_param_substitution::TypeParamSubstitution;
use crate::{ExprId, GenericParameters, Span, Type, TypeKind, TypedExprKind};

use super::{Inferer, assignable, type_limit_at};

pub(in crate::typechecker) struct PendingConstraintCheck {
    pub ty: Type,
    pub span: Span,
    pub scopes: Vec<GenericParameters>,
}

impl Inferer<'_> {
    pub(super) fn apparent_type(&self, ty: &Type) -> Type {
        self.apparent_type_at_depth(ty, 0)
    }

    fn apparent_type_at_depth(&self, ty: &Type, depth: u32) -> Type {
        if !contains_body_parameter(ty) {
            return ty.clone();
        }
        if depth >= crate::compiler_limits::MAX_TYPE_RESOLUTION_DEPTH {
            self.type_limits
                .record(crate::type_size::TypeTooLarge::Depth);
            return Type::Error;
        }
        let mut current = ty;
        let mut visited = BTreeSet::new();
        while let Type::GenericParam { id, .. } = current.without_aliases() {
            if !self.type_limits.spend_work(1) {
                return Type::Error;
            }
            if !visited.insert(*id) {
                return Type::Error;
            }
            let Some(bound) = self.generic_constraints.get(id) else {
                break;
            };
            current = bound;
        }
        // A bound kept lazy over a declaration bound later, or over the
        // class a static method belongs to (`P extends Partial<Own>`),
        // evaluates where it is read.
        let evaluated;
        if super::utility_types::has_evaluable_application(current) {
            evaluated = self
                .type_limits
                .type_or_error(super::utility_types::normalize(current, self.resolver()));
            current = &evaluated;
        }
        if let Type::Refined {
            original,
            ty: shape,
        } = current.without_aliases()
        {
            let bound = self.apparent_type_at_depth(original, depth + 1);
            if let Type::Union(members) = bound.peel() {
                let members: Vec<_> = members
                    .iter()
                    .filter(|member| assignable(member, shape, self.resolver()))
                    .cloned()
                    .collect();
                if !members.is_empty() {
                    return Type::union(members);
                }
            } else if assignable(&bound, shape, self.resolver()) {
                return bound;
            }
            return shape.as_ref().clone();
        }
        // A form reads through its base constraint: `p.a` on `Partial<T>`
        // with `T extends { a: number }` is `number | undefined` (tsc keeps
        // `T["a"] | undefined`).
        if current.without_aliases().is_type_operator() {
            return super::assignable::base_constraint(current, self.resolver());
        }
        if let Type::Union(members) = current.without_aliases() {
            let mut budget = self.type_limits.budget();
            let mut apparent_members = Vec::new();
            for member in members {
                let apparent = self.apparent_type_at_depth(member, depth + 1);
                if let Err(exceeded) = budget.charge_copy(&apparent, 1) {
                    self.type_limits.record(exceeded);
                    return Type::Error;
                }
                apparent_members.push(apparent);
            }
            return Type::union(apparent_members);
        }
        current.clone()
    }

    /// The static proof permits a representation-only cast. The original
    /// expression still has its erased slot, including for primitive bounds.
    /// A form with no members to read is reported here.
    pub(super) fn constraint_receiver(
        &mut self,
        value: ExprId,
        ty: Type,
        span: Span,
    ) -> Result<(ExprId, Type), CompilerFailure> {
        let apparent = self.constraint_receiver_type(&ty, span)?;
        if apparent == ty {
            return Ok((value, ty));
        }
        self.report_unreadable_form(&ty, &apparent, span);
        let value = self.push_synthetic_expr(
            TypedExprKind::Cast {
                value,
                target_ty: apparent.clone(),
                check: None,
            },
            apparent.clone(),
            span,
        )?;
        Ok((value, apparent))
    }

    /// Report a receiver that is a form whose bound has no evaluation
    /// (`Pick<T, "length">` with `T extends number[]`): its apparent type is
    /// `Error`, so it has no members to read. An `Error` from anything else
    /// (an unknown bound, a type limit) was reported where it arose.
    pub(super) fn report_unreadable_form(&mut self, ty: &Type, apparent: &Type, span: Span) {
        let has_form = super::utility_types::members(ty)
            .iter()
            .any(|member| member.without_aliases().is_type_operator());
        let unreadable = super::utility_types::members(apparent)
            .iter()
            .any(|member| matches!(member.peel(), Type::Error));
        if has_form && unreadable {
            self.error(
                span,
                format!("`{ty}` has no members to read: its bound has no evaluation"),
            );
        }
    }

    /// Both direct member reads and optional chains expose the bound's shape
    /// while retaining the source identity for later discriminant narrowing.
    pub(super) fn constraint_receiver_type(
        &self,
        ty: &Type,
        span: Span,
    ) -> Result<Type, CompilerFailure> {
        let apparent = self.apparent_type(ty);
        if &apparent == ty {
            return Ok(apparent);
        }
        let original = match ty.without_aliases() {
            Type::Refined { original, .. } => original.as_ref(),
            _ => ty,
        };
        let mut budget = self.type_limits.budget();
        budget
            .charge_copy(original, 1)
            .map_err(type_limit_at(span))?;
        budget
            .charge_copy(&apparent, 1)
            .map_err(type_limit_at(span))?;
        budget.charge(1).map_err(type_limit_at(span))?;
        Ok(super::narrowing::preserve_refinement(original, apparent))
    }

    pub(super) fn check_generic_constraints(
        &mut self,
        parameters: &GenericParameters,
        substitution: &TypeParamSubstitution,
        inferred_fallbacks: &[String],
        span: Span,
    ) -> Result<(), CompilerFailure> {
        if parameters.constraints.is_empty() {
            return Ok(());
        }
        // Signature TypeVars are inference wildcards. Turn active parameters
        // into rigid identities for this proof, just as at body entry.
        let rigid = self.rigid_signature_bindings()?;
        for (name, bound) in parameters.constraints.iter() {
            if inferred_fallbacks.contains(name) {
                continue;
            }
            let actual = substitution
                .apply(&Type::TypeVar(name.clone()), &self.type_limits)
                .map_err(type_limit_at(span))?;
            let expected = substitution
                .apply(bound, &self.type_limits)
                .map_err(type_limit_at(span))?;
            let actual = super::generic::substitute_typevars(&actual, &rigid, &self.type_limits)
                .map_err(type_limit_at(span))?;
            let expected =
                super::generic::substitute_typevars(&expected, &rigid, &self.type_limits)
                    .map_err(type_limit_at(span))?;
            // `keyof T` and utilities in a bound evaluate once `T` is known,
            // though the message names the bound as written (`keyof User`).
            let evaluated = super::utility_types::normalize(&expected, self.resolver())
                .map_err(type_limit_at(span))?;
            if !self.satisfies_generic_constraint(&actual, &evaluated) {
                self.error_with_help(
                    span,
                    format!("Type `{actual}` does not satisfy the constraint `{expected}`"),
                    vec![format!(
                        "choose a type argument for `{name}` assignable to `{expected}`"
                    )],
                );
            }
        }
        Ok(())
    }

    pub(super) fn satisfies_generic_constraint(&self, actual: &Type, expected: &Type) -> bool {
        if let Type::Union(members) = actual.peel() {
            return members
                .iter()
                .all(|member| self.satisfies_generic_constraint(member, expected));
        }
        // This is a type-level proof, not an assignment of one closure ABI to
        // another. A `never[]` rest accepts every fixed parameter type by
        // contravariance, as used by ReturnType/Parameters without `any`.
        if let Type::Function {
            params,
            ret,
            has_rest: true,
            predicate: None,
            ..
        } = expected.peel()
            && matches!(params.as_slice(), [Type::Array(element)] if element.peel() == &Type::Never)
            && let Type::Function {
                ret: actual_ret, ..
            } = self.apparent_type(actual).peel()
        {
            return ret.peel() == &Type::Void || assignable(actual_ret, ret, self.resolver());
        }
        assignable(actual, expected, self.resolver())
    }

    /// An invalid inference candidate falls back to its bound. Arguments are
    /// then checked against the resulting parameter type at their own spans;
    /// explicit type arguments instead retain the constraint diagnostic.
    pub(super) fn constrain_inferred_arguments(
        &self,
        parameters: &GenericParameters,
        substitution: &mut TypeParamSubstitution,
        inferred_fallbacks: &mut Vec<String>,
        span: Span,
    ) -> Result<(), CompilerFailure> {
        for name in &parameters.names {
            if inferred_fallbacks.contains(name) {
                continue;
            }
            let Some(bound) = parameters.constraints.get(name) else {
                continue;
            };
            let actual = substitution
                .apply(&Type::TypeVar(name.clone()), &self.type_limits)
                .map_err(type_limit_at(span))?;
            let bound = substitution
                .apply(bound, &self.type_limits)
                .map_err(type_limit_at(span))?;
            let bound = super::utility_types::normalize(&bound, self.resolver())
                .map_err(type_limit_at(span))?;
            if !self.satisfies_generic_constraint(&actual, &bound) {
                substitution.insert(name.clone(), bound);
                inferred_fallbacks.push(name.clone());
            }
        }
        Ok(())
    }

    /// Check a written named type's arguments against its parameters'
    /// bounds. A utility application is checked once, where it is resolved
    /// ([`Self::check_application_constraints`]), so it is skipped here.
    pub(super) fn check_named_type_constraints(
        &mut self,
        ty: &Type,
        span: Span,
    ) -> Result<(), CompilerFailure> {
        if ty.is_utility_application()
            || matches!(ty, Type::Alias { mangled, .. } if super::utility_types::utility(mangled).is_some())
        {
            return Ok(());
        }
        self.check_application_constraints(ty, span)
    }

    /// Check an application's arguments against its parameters' bounds, and
    /// a `T[K]` form's key against `keyof T`. Inside a declaration the check
    /// waits until every bound is known.
    pub(super) fn check_application_constraints(
        &mut self,
        ty: &Type,
        span: Span,
    ) -> Result<(), CompilerFailure> {
        match ty {
            // Only a generic form: a concrete one (over a later declaration)
            // reports a missing key where it evaluates.
            Type::IndexedAccess { object, index }
                if super::utility_types::holds_type_parameter(ty) =>
            {
                if !self.defer_constraint_check(ty, span) {
                    self.check_index_key(object, index, span)?;
                }
                Ok(())
            }
            Type::InterfaceRef {
                mangled,
                name,
                args,
                ..
            }
            | Type::ClassRef {
                mangled,
                name,
                args,
                ..
            }
            | Type::Alias {
                mangled,
                name,
                args,
                ..
            }
            | Type::AliasRef {
                mangled,
                name,
                args,
                ..
            } => {
                if !self.defer_constraint_check(ty, span) {
                    self.check_argument_bounds(mangled, name, args, span)?;
                }
                Ok(())
            }
            _ => Ok(()),
        }
    }

    /// Queue the check of `ty` while a declaration binds, so every bound is
    /// known when it runs. Whether it was queued.
    fn defer_constraint_check(&mut self, ty: &Type, span: Span) -> bool {
        let Some(pending) = &mut self.pending_constraint_checks else {
            return false;
        };
        pending.push(PendingConstraintCheck {
            ty: ty.clone(),
            span,
            scopes: self.generics_in_scope.clone(),
        });
        true
    }

    /// Check the arguments of an interface, class or alias application
    /// against its parameters' bounds.
    fn check_argument_bounds(
        &mut self,
        mangled: &crate::MangledName,
        name: &str,
        args: &[Type],
        span: Span,
    ) -> Result<(), CompilerFailure> {
        let Some(symbol) = self.lookup_structural_type(mangled, name) else {
            return Ok(());
        };
        let parameters = match &symbol.kind {
            TypeKind::Interface { generics, .. }
            | TypeKind::Class { generics, .. }
            | TypeKind::Alias { generics, .. } => generics.clone(),
            _ => return Ok(()),
        };
        // The arguments' own type parameters become rigid first, so one named
        // like a parameter of the application (`Pick<K, "a">` with the user's
        // `K`) is never chased into that parameter's binding.
        let rigid = self.rigid_signature_bindings()?;
        let args = args
            .iter()
            .map(|arg| super::generic::substitute_typevars(arg, &rigid, &self.type_limits))
            .collect::<Result<Vec<_>, _>>()
            .map_err(type_limit_at(span))?;
        let substitution = TypeParamSubstitution::from_pairs(&parameters, &args);
        self.check_generic_constraints(&parameters, &substitution, &[], span)
    }

    /// tsc's TS2536: `T["a"]` needs `"a"` among the keys of `T`'s bound.
    fn check_index_key(
        &mut self,
        object: &Type,
        index: &Type,
        span: Span,
    ) -> Result<(), CompilerFailure> {
        let rigid = self.rigid_signature_bindings()?;
        let rigid_object = super::generic::substitute_typevars(object, &rigid, &self.type_limits)
            .map_err(type_limit_at(span))?;
        let rigid_index = super::generic::substitute_typevars(index, &rigid, &self.type_limits)
            .map_err(type_limit_at(span))?;
        let keys =
            super::utility_types::normalize(&Type::KeyOf(Box::new(rigid_object)), self.resolver())
                .map_err(type_limit_at(span))?;
        if !self.satisfies_generic_constraint(&rigid_index, &keys) {
            self.error(
                span,
                format!("Type `{index}` cannot be used to index type `{object}`"),
            );
        }
        Ok(())
    }

    pub(super) fn check_pending_constraints(&mut self) -> Result<(), CompilerFailure> {
        let pending = self.pending_constraint_checks.take().unwrap_or_default();
        let scopes = std::mem::take(&mut self.generics_in_scope);
        let result = (|| {
            for check in pending {
                self.generics_in_scope = check.scopes;
                self.check_application_constraints(&check.ty, check.span)?;
            }
            Ok(())
        })();
        self.generics_in_scope = scopes;
        result
    }
}

fn contains_body_parameter(ty: &Type) -> bool {
    let mut pending = vec![ty];
    while let Some(ty) = pending.pop() {
        if matches!(ty, Type::GenericParam { .. }) {
            return true;
        }
        crate::type_size::for_each_child(ty, |child| pending.push(child));
    }
    false
}