tsz-solver 0.1.8

TypeScript type solver for the tsz compiler
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
521
522
523
524
//! Tests for type predicate compatibility in function subtyping.
//!
//! Type predicates (`x is T` and `asserts x is T`) make functions more specific.
//! A function with a type predicate can only be assigned to a function with
//! a compatible (or more general) predicate.

use super::*;
use crate::TypeInterner;

// =============================================================================
// Helper Functions
// =============================================================================

/// Create a type predicate: `paramName is Type`
fn type_predicate(interner: &TypeInterner, param_name: &str, type_id: TypeId) -> TypePredicate {
    TypePredicate {
        asserts: false,
        target: TypePredicateTarget::Identifier(interner.intern_string(param_name)),
        type_id: Some(type_id),
        parameter_index: None,
    }
}

/// Create an assertion predicate: `asserts paramName is Type`
fn asserts_predicate(interner: &TypeInterner, param_name: &str, type_id: TypeId) -> TypePredicate {
    TypePredicate {
        asserts: true,
        target: TypePredicateTarget::Identifier(interner.intern_string(param_name)),
        type_id: Some(type_id),
        parameter_index: None,
    }
}

/// Create a bare assertion: `asserts paramName`
fn bare_asserts(interner: &TypeInterner, param_name: &str) -> TypePredicate {
    TypePredicate {
        asserts: true,
        target: TypePredicateTarget::Identifier(interner.intern_string(param_name)),
        type_id: None,
        parameter_index: None,
    }
}

/// Create a function shape with the given params, return type, and type predicate
fn fn_with_predicate(
    interner: &TypeInterner,
    param_name: &str,
    param_type: TypeId,
    return_type: TypeId,
    type_predicate: Option<TypePredicate>,
) -> FunctionShape {
    let param_name_atom = interner.intern_string(param_name);
    FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_name_atom, param_type)],
        this_type: None,
        return_type,
        type_predicate,
        is_constructor: false,
        is_method: false,
    }
}

// =============================================================================
// Type Guard Tests (`x is T`)
// =============================================================================

#[test]
fn test_type_guard_more_specific_than_no_predicate() {
    // A function with a type guard is MORE specific than one without
    // (x: string) => x is string cannot be assigned to (x: string) => boolean
    let interner = TypeInterner::new();
    let mut checker = SubtypeChecker::new(&interner);

    let string_param = interner.intern_string("x");
    let helper = type_predicate(&interner, "x", TypeId::STRING);
    let helper_asserts = asserts_predicate(&interner, "x", TypeId::STRING);
    let helper_bare_asserts = bare_asserts(&interner, "x");
    let helper_fn_with_predicate = fn_with_predicate(
        &interner,
        "x",
        TypeId::STRING,
        TypeId::STRING,
        Some(helper.clone()),
    );

    assert_eq!(helper.type_id, Some(TypeId::STRING));
    assert!(helper_asserts.asserts);
    assert!(helper_bare_asserts.asserts);
    assert!(helper_fn_with_predicate.type_predicate.is_some());

    // Source: (x: string) => x is string
    let source_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(string_param, TypeId::STRING)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: false,
            target: TypePredicateTarget::Identifier(string_param),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Target: (x: string) => boolean (no predicate)
    let target_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(string_param, TypeId::STRING)],
        this_type: None,
        return_type: TypeId::BOOLEAN,
        type_predicate: None,
        is_constructor: false,
        is_method: false,
    });

    // Source (with predicate) should NOT be subtype of target (without predicate)
    assert!(
        !checker.is_subtype_of(source_fn, target_fn),
        "Function with type guard should NOT be assignable to function without guard"
    );
}

#[test]
fn test_no_predicate_not_compatible_with_type_guard() {
    // A function WITHOUT a type guard is NOT compatible with one that has a guard
    // if return types don't match
    // (x: string) => boolean CANNOT be assigned to (x: string) => x is string
    let interner = TypeInterner::new();
    let mut checker = SubtypeChecker::new(&interner);

    let string_param = interner.intern_string("x");

    // Source: (x: string) => boolean (no predicate)
    let source_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(string_param, TypeId::STRING)],
        this_type: None,
        return_type: TypeId::BOOLEAN,
        type_predicate: None,
        is_constructor: false,
        is_method: false,
    });

    // Target: (x: string) => x is string
    let target_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(string_param, TypeId::STRING)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: false,
            target: TypePredicateTarget::Identifier(string_param),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Source (without predicate, returns boolean) should NOT be subtype of target (with predicate, returns string)
    // because boolean is not a subtype of string
    assert!(
        !checker.is_subtype_of(source_fn, target_fn),
        "Function returning boolean should NOT be assignable to function with type guard returning string"
    );
}

#[test]
fn test_no_predicate_compatible_with_type_guard_matching_return() {
    // A function WITHOUT a type guard IS compatible if return types match
    // (x: unknown) => string can be assigned to (x: unknown) => x is string
    let interner = TypeInterner::new();
    let mut checker = SubtypeChecker::new(&interner);

    let param_x = interner.intern_string("x");

    // Source: (x: unknown) => string (no predicate)
    let source_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, TypeId::UNKNOWN)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: None,
        is_constructor: false,
        is_method: false,
    });

    // Target: (x: unknown) => x is string
    let target_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, TypeId::UNKNOWN)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: false,
            target: TypePredicateTarget::Identifier(param_x),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Source (without predicate, returns string) should be subtype of target (with predicate, returns string)
    assert!(
        checker.is_subtype_of(source_fn, target_fn),
        "Function returning string should be assignable to function with type guard returning string"
    );
}

#[test]
fn test_type_guard_narrowing_is_compatible() {
    // (x: Animal) => x is Dog can be assigned to (x: Animal) => x is Animal
    // where Dog extends Animal (Dog <: Animal)
    let interner = TypeInterner::new();
    let mut checker = SubtypeChecker::new(&interner);

    // Create Animal and Dog types (Dog <: Animal)
    let animal = interner.object(vec![]);
    let dog = interner.object(vec![]);

    let param_x = interner.intern_string("x");

    // Source: (x: Animal) => x is Dog (narrower type guard)
    let source_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, animal)],
        this_type: None,
        return_type: dog,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: false,
            target: TypePredicateTarget::Identifier(param_x),
            type_id: Some(dog),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Target: (x: Animal) => x is Animal (wider type guard)
    let target_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, animal)],
        this_type: None,
        return_type: animal,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: false,
            target: TypePredicateTarget::Identifier(param_x),
            type_id: Some(animal),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Source (narrower guard) should be subtype of target (wider guard)
    // Note: This tests the predicate narrowing, though with identical object types
    // they will be equal. In real usage with distinct types, Dog <: Animal would allow this.
    assert!(
        checker.is_subtype_of(source_fn, target_fn),
        "Narrower type guard should be assignable to wider type guard"
    );
}

#[test]
fn test_type_guard_different_parameters_incompatible() {
    // (x: string) => x is string cannot be assigned to (y: string) => y is string
    // The predicates target different parameters
    let interner = TypeInterner::new();
    let mut checker = SubtypeChecker::new(&interner);

    let param_x = interner.intern_string("x");
    let param_y = interner.intern_string("y");

    // Source: (x: string) => x is string
    let source_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, TypeId::STRING)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: false,
            target: TypePredicateTarget::Identifier(param_x),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Target: (y: string) => y is string
    let target_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_y, TypeId::STRING)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: false,
            target: TypePredicateTarget::Identifier(param_y),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Source should NOT be subtype of target (different predicate targets)
    assert!(
        !checker.is_subtype_of(source_fn, target_fn),
        "Type guards on different parameters should be incompatible"
    );
}

// =============================================================================
// Assertion Tests (`asserts x is T`)
// =============================================================================

#[test]
fn test_asserts_more_specific_than_type_guard() {
    // (asserts x is T) is MORE specific than (x is T) due to assertion semantics
    // (x: unknown) => asserts x is string cannot be assigned to (x: unknown) => x is string
    let interner = TypeInterner::new();
    let mut checker = SubtypeChecker::new(&interner);

    let param_x = interner.intern_string("x");

    // Source: (x: unknown) => asserts x is string
    let source_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, TypeId::UNKNOWN)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: true,
            target: TypePredicateTarget::Identifier(param_x),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Target: (x: unknown) => x is string
    let target_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, TypeId::UNKNOWN)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: false,
            target: TypePredicateTarget::Identifier(param_x),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Source (asserts) should NOT be subtype of target (type guard)
    // because assertions and type guards are incompatible
    assert!(
        !checker.is_subtype_of(source_fn, target_fn),
        "Assertion should NOT be assignable to type guard (incompatible kinds)"
    );
}

#[test]
fn test_type_guard_not_compatible_with_asserts() {
    // (x is T) is NOT compatible with (asserts x is T) due to assertion semantics
    // (x: unknown) => x is string cannot be assigned to (x: unknown) => asserts x is string
    let interner = TypeInterner::new();
    let mut checker = SubtypeChecker::new(&interner);

    let param_x = interner.intern_string("x");

    // Source: (x: unknown) => x is string
    let source_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, TypeId::UNKNOWN)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: false,
            target: TypePredicateTarget::Identifier(param_x),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Target: (x: unknown) => asserts x is string
    let target_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, TypeId::UNKNOWN)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: true,
            target: TypePredicateTarget::Identifier(param_x),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Source (type guard) should NOT be subtype of target (asserts)
    assert!(
        !checker.is_subtype_of(source_fn, target_fn),
        "Type guard should NOT be assignable to assertion (incompatible kinds)"
    );
}

#[test]
fn test_bare_asserts_compatibility() {
    // `asserts x` (bare assertion) is less specific than `asserts x is T`
    // Bare assertion cannot be assigned to typed assertion
    // Typed assertion can be assigned to bare assertion
    let interner = TypeInterner::new();
    let mut checker = SubtypeChecker::new(&interner);

    let param_x = interner.intern_string("x");

    // Source: (x: unknown) => asserts x (bare assertion)
    let source_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, TypeId::UNKNOWN)],
        this_type: None,
        return_type: TypeId::VOID,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: true,
            target: TypePredicateTarget::Identifier(param_x),
            type_id: None, // Bare assertion, no type
        }),
        is_constructor: false,
        is_method: false,
    });

    // Target: (x: unknown) => asserts x is string (typed assertion)
    let target_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_x, TypeId::UNKNOWN)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: true,
            target: TypePredicateTarget::Identifier(param_x),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Source (bare asserts) should NOT be subtype of target (asserts with type)
    // because bare assertion (no type) is less specific than typed assertion
    assert!(
        !checker.is_subtype_of(source_fn, target_fn),
        "Bare assertion should NOT be assignable to typed assertion"
    );

    // Note: The reverse direction (typed assertion to bare assertion) would also
    // fail in this specific test case due to return type mismatch (STRING vs VOID),
    // not because of the predicate logic. With matching return types, typed assertion
    // would be assignable to bare assertion since typed is more specific.
}

// =============================================================================
// Integration Tests
// =============================================================================

#[test]
#[ignore = "Type guard in overloads not fully implemented"]
fn test_type_guard_in_overloads() {
    // Test type predicate behavior with callable overloads
    let interner = TypeInterner::new();
    let mut checker = SubtypeChecker::new(&interner);

    let param_value = interner.intern_string("value");

    // Create a function with type guard
    let type_guard_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_value, TypeId::UNKNOWN)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: Some(TypePredicate {
            parameter_index: None,
            asserts: false,
            target: TypePredicateTarget::Identifier(param_value),
            type_id: Some(TypeId::STRING),
        }),
        is_constructor: false,
        is_method: false,
    });

    // Create a function without type guard
    let regular_fn = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::required(param_value, TypeId::UNKNOWN)],
        this_type: None,
        return_type: TypeId::STRING,
        type_predicate: None,
        is_constructor: false,
        is_method: false,
    });

    // Type guard function is NOT assignable to regular function
    assert!(
        !checker.is_subtype_of(type_guard_fn, regular_fn),
        "Type guard function should NOT be assignable to regular function"
    );

    // Regular function IS assignable to type guard function
    assert!(
        checker.is_subtype_of(regular_fn, type_guard_fn),
        "Regular function should be assignable to type guard function"
    );
}