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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
//! Type inference engine using Union-Find.
//!
//! This module implements type inference for generic functions using
//! the `ena` crate's Union-Find data structure.
//!
//! Key features:
//! - Inference variables for generic type parameters
//! - Constraint collection during type checking
//! - Bounds checking (L <: α <: U)
//! - Best common type calculation
//! - Efficient unification with path compression
use crate::TypeDatabase;
#[cfg(test)]
use crate::types::*;
use crate::types::{
CallableShapeId, FunctionShapeId, InferencePriority, IntrinsicKind, LiteralValue, MappedTypeId,
ObjectShapeId, TemplateLiteralId, TemplateSpan, TupleElement, TupleListId, TypeApplicationId,
TypeData, TypeId, TypeListId,
};
use crate::visitor::is_literal_type;
use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue};
use rustc_hash::{FxHashMap, FxHashSet};
use std::cell::RefCell;
use tsz_common::interner::Atom;
/// Helper function to extend a vector with deduplicated items.
/// Uses a `HashSet` for O(1) lookups instead of O(n) contains checks.
fn extend_dedup<T>(target: &mut Vec<T>, items: &[T])
where
T: Clone + Eq + std::hash::Hash,
{
if items.is_empty() {
return;
}
// Hot path for inference: most merges add a single item.
// Avoid allocating/hash-building a set for that case.
if items.len() == 1 {
let item = &items[0];
if !target.contains(item) {
target.push(item.clone());
}
return;
}
let mut existing: FxHashSet<_> = target.iter().cloned().collect();
for item in items {
if existing.insert(item.clone()) {
target.push(item.clone());
}
}
}
/// An inference variable representing an unknown type.
/// These are created when instantiating generic functions.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct InferenceVar(pub u32);
// Uses TypeScript-standard InferencePriority from types.rs
/// A candidate type for an inference variable.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct InferenceCandidate {
pub type_id: TypeId,
pub priority: InferencePriority,
pub is_fresh_literal: bool,
}
/// Value stored for each inference variable root.
#[derive(Clone, Debug, Default)]
pub struct InferenceInfo {
pub candidates: Vec<InferenceCandidate>,
pub upper_bounds: Vec<TypeId>,
pub resolved: Option<TypeId>,
}
impl InferenceInfo {
pub const fn is_empty(&self) -> bool {
self.candidates.is_empty() && self.upper_bounds.is_empty()
}
}
impl UnifyKey for InferenceVar {
type Value = InferenceInfo;
fn index(&self) -> u32 {
self.0
}
fn from_index(u: u32) -> Self {
Self(u)
}
fn tag() -> &'static str {
"InferenceVar"
}
}
impl UnifyValue for InferenceInfo {
type Error = NoError;
fn unify_values(a: &Self, b: &Self) -> Result<Self, Self::Error> {
let mut merged = a.clone();
// Deduplicate candidates using helper
extend_dedup(&mut merged.candidates, &b.candidates);
// Deduplicate upper bounds using helper
extend_dedup(&mut merged.upper_bounds, &b.upper_bounds);
if merged.resolved.is_none() {
merged.resolved = b.resolved;
}
Ok(merged)
}
}
/// Inference error
#[derive(Clone, Debug)]
pub enum InferenceError {
/// Two incompatible types were unified
Conflict(TypeId, TypeId),
/// Inference variable was not resolved
Unresolved(InferenceVar),
/// Circular unification detected (occurs-check)
OccursCheck { var: InferenceVar, ty: TypeId },
/// Lower bound is not subtype of upper bound
BoundsViolation {
var: InferenceVar,
lower: TypeId,
upper: TypeId,
},
/// Variance violation detected
VarianceViolation {
var: InferenceVar,
expected_variance: &'static str,
position: TypeId,
},
}
/// Constraint set for an inference variable.
/// Tracks both lower bounds (L <: α) and upper bounds (α <: U).
#[derive(Clone, Debug, Default)]
pub struct ConstraintSet {
/// Lower bounds: types that must be subtypes of this variable
/// e.g., from argument types being assigned to a parameter
pub lower_bounds: Vec<TypeId>,
/// Upper bounds: types that this variable must be a subtype of
/// e.g., from `extends` constraints on type parameters
pub upper_bounds: Vec<TypeId>,
}
impl ConstraintSet {
pub const fn new() -> Self {
Self {
lower_bounds: Vec::new(),
upper_bounds: Vec::new(),
}
}
pub fn from_info(info: &InferenceInfo) -> Self {
let mut lower_bounds = Vec::new();
let mut upper_bounds = Vec::new();
let mut seen_lower = FxHashSet::default();
let mut seen_upper = FxHashSet::default();
for candidate in &info.candidates {
if seen_lower.insert(candidate.type_id) {
lower_bounds.push(candidate.type_id);
}
}
for &upper in &info.upper_bounds {
if seen_upper.insert(upper) {
upper_bounds.push(upper);
}
}
Self {
lower_bounds,
upper_bounds,
}
}
/// Add a lower bound constraint: L <: α
pub fn add_lower_bound(&mut self, ty: TypeId) {
if !self.lower_bounds.contains(&ty) {
self.lower_bounds.push(ty);
}
}
/// Add an upper bound constraint: α <: U
pub fn add_upper_bound(&mut self, ty: TypeId) {
if !self.upper_bounds.contains(&ty) {
self.upper_bounds.push(ty);
}
}
/// Check if there are any constraints
pub const fn is_empty(&self) -> bool {
self.lower_bounds.is_empty() && self.upper_bounds.is_empty()
}
pub fn merge_from(&mut self, other: Self) {
for ty in other.lower_bounds {
self.add_lower_bound(ty);
}
for ty in other.upper_bounds {
self.add_upper_bound(ty);
}
}
/// Perform transitive reduction on upper bounds to remove redundant constraints.
///
/// If we have constraints (T <: A) and (T <: B) and we know (A <: B),
/// then (T <: B) is redundant and can be removed.
///
/// This reduces N² pairwise checks in `detect_conflicts` to O(N * `reduced_N`).
pub fn transitive_reduction(&mut self, interner: &dyn TypeDatabase) {
if self.upper_bounds.len() < 2 {
return;
}
let mut redundant = FxHashSet::default();
let bounds = &self.upper_bounds;
for (i, &u1) in bounds.iter().enumerate() {
for (j, &u2) in bounds.iter().enumerate() {
if i == j || redundant.contains(&u1) || redundant.contains(&u2) {
continue;
}
// If u1 <: u2, then u2 is redundant (u1 is a stricter constraint)
if crate::subtype::is_subtype_of(interner, u1, u2) {
redundant.insert(u2);
}
}
}
if !redundant.is_empty() {
self.upper_bounds.retain(|ty| !redundant.contains(ty));
}
}
/// Detect early conflicts between collected constraints.
/// This allows failing fast before full resolution.
pub fn detect_conflicts(&self, interner: &dyn TypeDatabase) -> Option<ConstraintConflict> {
// PERF: Transitive reduction of upper bounds to minimize N² checks.
let mut reduced_upper = self.upper_bounds.clone();
if reduced_upper.len() >= 2 {
let mut redundant = FxHashSet::default();
for (i, &u1) in reduced_upper.iter().enumerate() {
for (j, &u2) in reduced_upper.iter().enumerate() {
if i == j || redundant.contains(&u1) || redundant.contains(&u2) {
continue;
}
if crate::subtype::is_subtype_of(interner, u1, u2) {
redundant.insert(u2);
}
}
}
if !redundant.is_empty() {
reduced_upper.retain(|ty| !redundant.contains(ty));
}
}
// 1. Check for mutually exclusive upper bounds
for (i, &u1) in reduced_upper.iter().enumerate() {
for &u2 in &reduced_upper[i + 1..] {
if are_disjoint(interner, u1, u2) {
return Some(ConstraintConflict::DisjointUpperBounds(u1, u2));
}
}
}
// 2. Check if any lower bound is incompatible with any upper bound
for &lower in &self.lower_bounds {
for &upper in &reduced_upper {
// Ignore ERROR and ANY for conflict detection
if lower == TypeId::ERROR
|| upper == TypeId::ERROR
|| lower == TypeId::ANY
|| upper == TypeId::ANY
{
continue;
}
if !crate::subtype::is_subtype_of(interner, lower, upper) {
return Some(ConstraintConflict::LowerExceedsUpper(lower, upper));
}
}
}
None
}
}
/// Conflict detected between constraints on an inference variable.
#[derive(Clone, Debug)]
pub enum ConstraintConflict {
/// Mutually exclusive upper bounds (e.g., string AND number)
DisjointUpperBounds(TypeId, TypeId),
/// A lower bound is not a subtype of an upper bound
LowerExceedsUpper(TypeId, TypeId),
}
/// Helper to determine if two types are definitely disjoint (no common inhabitants).
fn are_disjoint(interner: &dyn TypeDatabase, a: TypeId, b: TypeId) -> bool {
if a == b {
return false;
}
if a.is_any_or_unknown() || b.is_any_or_unknown() {
return false;
}
let key_a = interner.lookup(a);
let key_b = interner.lookup(b);
match (key_a, key_b) {
(Some(TypeData::Intrinsic(k1)), Some(TypeData::Intrinsic(k2))) => {
use IntrinsicKind::*;
// Basic primitives are disjoint (ignoring object/Function which are more complex)
k1 != k2 && !matches!((k1, k2), (Object | Function, _) | (_, Object | Function))
}
(Some(TypeData::Literal(l1)), Some(TypeData::Literal(l2))) => l1 != l2,
(Some(TypeData::Literal(l1)), Some(TypeData::Intrinsic(k2))) => {
!is_literal_compatible_with_intrinsic(&l1, k2)
}
(Some(TypeData::Intrinsic(k1)), Some(TypeData::Literal(l2))) => {
!is_literal_compatible_with_intrinsic(&l2, k1)
}
_ => false,
}
}
fn is_literal_compatible_with_intrinsic(lit: &LiteralValue, kind: IntrinsicKind) -> bool {
match lit {
LiteralValue::String(_) => kind == IntrinsicKind::String,
LiteralValue::Number(_) => kind == IntrinsicKind::Number,
LiteralValue::BigInt(_) => kind == IntrinsicKind::Bigint,
LiteralValue::Boolean(_) => kind == IntrinsicKind::Boolean,
}
}
/// Maximum iterations for constraint strengthening loops to prevent infinite loops.
pub const MAX_CONSTRAINT_ITERATIONS: usize = 100;
/// Maximum recursion depth for type containment checks.
pub const MAX_TYPE_RECURSION_DEPTH: usize = 100;
/// Type inference context for a single function call or expression.
pub struct InferenceContext<'a> {
pub(crate) interner: &'a dyn TypeDatabase,
/// Type resolver for semantic lookups (e.g., base class queries)
pub(crate) resolver: Option<&'a dyn crate::TypeResolver>,
/// Memoized subtype checks used by BCT and bound validation.
pub(crate) subtype_cache: RefCell<FxHashMap<(TypeId, TypeId), bool>>,
/// Unification table for inference variables
pub(crate) table: InPlaceUnificationTable<InferenceVar>,
/// Map from type parameter names to inference variables, with const flag
pub(crate) type_params: Vec<(Atom, InferenceVar, bool)>,
}
impl<'a> InferenceContext<'a> {
pub(crate) const UPPER_BOUND_INTERSECTION_FAST_PATH_LIMIT: usize = 8;
pub(crate) const UPPER_BOUND_INTERSECTION_LARGE_SET_THRESHOLD: usize = 64;
pub fn new(interner: &'a dyn TypeDatabase) -> Self {
InferenceContext {
interner,
resolver: None,
subtype_cache: RefCell::new(FxHashMap::default()),
table: InPlaceUnificationTable::new(),
type_params: Vec::new(),
}
}
pub fn with_resolver(
interner: &'a dyn TypeDatabase,
resolver: &'a dyn crate::TypeResolver,
) -> Self {
InferenceContext {
interner,
resolver: Some(resolver),
subtype_cache: RefCell::new(FxHashMap::default()),
table: InPlaceUnificationTable::new(),
type_params: Vec::new(),
}
}
/// Create a fresh inference variable
pub fn fresh_var(&mut self) -> InferenceVar {
self.table.new_key(InferenceInfo::default())
}
/// Create an inference variable for a type parameter
pub fn fresh_type_param(&mut self, name: Atom, is_const: bool) -> InferenceVar {
let var = self.fresh_var();
self.type_params.push((name, var, is_const));
var
}
/// Register an existing inference variable as representing a type parameter.
///
/// This is useful when the caller needs to compute a unique placeholder name
/// (and corresponding placeholder `TypeId`) after allocating the inference variable.
pub fn register_type_param(&mut self, name: Atom, var: InferenceVar, is_const: bool) {
self.type_params.push((name, var, is_const));
}
/// Look up an inference variable by type parameter name
pub fn find_type_param(&self, name: Atom) -> Option<InferenceVar> {
self.type_params
.iter()
.find(|(n, _, _)| *n == name)
.map(|(_, v, _)| *v)
}
/// Check if an inference variable is a const type parameter
pub fn is_var_const(&mut self, var: InferenceVar) -> bool {
let root = self.table.find(var);
self.type_params
.iter()
.any(|(_, v, is_const)| self.table.find(*v) == root && *is_const)
}
/// Probe the current value of an inference variable
pub fn probe(&mut self, var: InferenceVar) -> Option<TypeId> {
self.table.probe_value(var).resolved
}
/// Unify an inference variable with a concrete type
pub fn unify_var_type(&mut self, var: InferenceVar, ty: TypeId) -> Result<(), InferenceError> {
// Get the root variable
let root = self.table.find(var);
if self.occurs_in(root, ty) {
return Err(InferenceError::OccursCheck { var: root, ty });
}
// Check current value
match self.table.probe_value(root).resolved {
None => {
self.table.union_value(
root,
InferenceInfo {
resolved: Some(ty),
..InferenceInfo::default()
},
);
Ok(())
}
Some(existing) => {
if self.types_compatible(existing, ty) {
Ok(())
} else {
Err(InferenceError::Conflict(existing, ty))
}
}
}
}
/// Unify two inference variables
pub fn unify_vars(&mut self, a: InferenceVar, b: InferenceVar) -> Result<(), InferenceError> {
let root_a = self.table.find(a);
let root_b = self.table.find(b);
if root_a == root_b {
return Ok(());
}
let value_a = self.table.probe_value(root_a).resolved;
let value_b = self.table.probe_value(root_b).resolved;
if let (Some(a_ty), Some(b_ty)) = (value_a, value_b)
&& !self.types_compatible(a_ty, b_ty)
{
return Err(InferenceError::Conflict(a_ty, b_ty));
}
self.table
.unify_var_var(root_a, root_b)
.map_err(|_| InferenceError::Conflict(TypeId::ERROR, TypeId::ERROR))?;
Ok(())
}
/// Check if two types are compatible for unification
fn types_compatible(&self, a: TypeId, b: TypeId) -> bool {
if a == b {
return true;
}
// Any is compatible with everything
if a == TypeId::ANY || b == TypeId::ANY {
return true;
}
// Unknown is compatible with everything
if a == TypeId::UNKNOWN || b == TypeId::UNKNOWN {
return true;
}
// Never is compatible with everything
if a == TypeId::NEVER || b == TypeId::NEVER {
return true;
}
false
}
pub(crate) fn occurs_in(&mut self, var: InferenceVar, ty: TypeId) -> bool {
let root = self.table.find(var);
if self.type_params.is_empty() {
return false;
}
let mut visited = FxHashSet::default();
for &(atom, param_var, _) in &self.type_params {
if self.table.find(param_var) == root
&& self.type_contains_param(ty, atom, &mut visited)
{
return true;
}
}
false
}
pub(crate) fn type_param_names_for_root(&mut self, root: InferenceVar) -> Vec<Atom> {
self.type_params
.iter()
.filter(|&(_name, var, _)| self.table.find(*var) == root)
.map(|(name, _var, _)| *name)
.collect()
}
pub(crate) fn upper_bound_cycles_param(&mut self, bound: TypeId, targets: &[Atom]) -> bool {
let mut params = FxHashSet::default();
let mut visited = FxHashSet::default();
self.collect_type_params(bound, &mut params, &mut visited);
for name in params {
let mut seen = FxHashSet::default();
if self.param_depends_on_targets(name, targets, &mut seen) {
return true;
}
}
false
}
pub(crate) fn expand_cyclic_upper_bound(
&mut self,
root: InferenceVar,
bound: TypeId,
target_names: &[Atom],
candidates: &mut Vec<InferenceCandidate>,
upper_bounds: &mut Vec<TypeId>,
) {
let name = match self.interner.lookup(bound) {
Some(TypeData::TypeParameter(info) | TypeData::Infer(info)) => info.name,
_ => return,
};
let Some(var) = self.find_type_param(name) else {
return;
};
if let Some(resolved) = self.probe(var) {
if !upper_bounds.contains(&resolved) {
upper_bounds.push(resolved);
}
return;
}
let bound_root = self.table.find(var);
let info = self.table.probe_value(bound_root);
for candidate in info.candidates {
if self.occurs_in(root, candidate.type_id) {
continue;
}
candidates.push(InferenceCandidate {
type_id: candidate.type_id,
priority: InferencePriority::Circular,
is_fresh_literal: candidate.is_fresh_literal,
});
}
for ty in info.upper_bounds {
if self.occurs_in(root, ty) {
continue;
}
if !target_names.is_empty() && self.upper_bound_cycles_param(ty, target_names) {
continue;
}
if !upper_bounds.contains(&ty) {
upper_bounds.push(ty);
}
}
}
fn collect_type_params(
&self,
ty: TypeId,
params: &mut FxHashSet<Atom>,
visited: &mut FxHashSet<TypeId>,
) {
if !visited.insert(ty) {
return;
}
let Some(key) = self.interner.lookup(ty) else {
return;
};
match key {
TypeData::TypeParameter(info) | TypeData::Infer(info) => {
params.insert(info.name);
}
TypeData::Array(elem) => {
self.collect_type_params(elem, params, visited);
}
TypeData::Tuple(elements) => {
let elements = self.interner.tuple_list(elements);
for element in elements.iter() {
self.collect_type_params(element.type_id, params, visited);
}
}
TypeData::Union(members) | TypeData::Intersection(members) => {
let members = self.interner.type_list(members);
for &member in members.iter() {
self.collect_type_params(member, params, visited);
}
}
TypeData::Object(shape_id) => {
let shape = self.interner.object_shape(shape_id);
for prop in &shape.properties {
self.collect_type_params(prop.type_id, params, visited);
}
}
TypeData::ObjectWithIndex(shape_id) => {
let shape = self.interner.object_shape(shape_id);
for prop in &shape.properties {
self.collect_type_params(prop.type_id, params, visited);
}
if let Some(index) = shape.string_index.as_ref() {
self.collect_type_params(index.key_type, params, visited);
self.collect_type_params(index.value_type, params, visited);
}
if let Some(index) = shape.number_index.as_ref() {
self.collect_type_params(index.key_type, params, visited);
self.collect_type_params(index.value_type, params, visited);
}
}
TypeData::Application(app_id) => {
let app = self.interner.type_application(app_id);
self.collect_type_params(app.base, params, visited);
for &arg in &app.args {
self.collect_type_params(arg, params, visited);
}
}
TypeData::Function(shape_id) => {
let shape = self.interner.function_shape(shape_id);
for param in &shape.params {
self.collect_type_params(param.type_id, params, visited);
}
if let Some(this_type) = shape.this_type {
self.collect_type_params(this_type, params, visited);
}
self.collect_type_params(shape.return_type, params, visited);
}
TypeData::Callable(shape_id) => {
let shape = self.interner.callable_shape(shape_id);
for sig in &shape.call_signatures {
for param in &sig.params {
self.collect_type_params(param.type_id, params, visited);
}
if let Some(this_type) = sig.this_type {
self.collect_type_params(this_type, params, visited);
}
self.collect_type_params(sig.return_type, params, visited);
}
for sig in &shape.construct_signatures {
for param in &sig.params {
self.collect_type_params(param.type_id, params, visited);
}
if let Some(this_type) = sig.this_type {
self.collect_type_params(this_type, params, visited);
}
self.collect_type_params(sig.return_type, params, visited);
}
for prop in &shape.properties {
self.collect_type_params(prop.type_id, params, visited);
}
}
TypeData::Conditional(cond_id) => {
let cond = self.interner.conditional_type(cond_id);
self.collect_type_params(cond.check_type, params, visited);
self.collect_type_params(cond.extends_type, params, visited);
self.collect_type_params(cond.true_type, params, visited);
self.collect_type_params(cond.false_type, params, visited);
}
TypeData::Mapped(mapped_id) => {
let mapped = self.interner.mapped_type(mapped_id);
self.collect_type_params(mapped.constraint, params, visited);
if let Some(name_type) = mapped.name_type {
self.collect_type_params(name_type, params, visited);
}
self.collect_type_params(mapped.template, params, visited);
}
TypeData::IndexAccess(obj, idx) => {
self.collect_type_params(obj, params, visited);
self.collect_type_params(idx, params, visited);
}
TypeData::KeyOf(operand) | TypeData::ReadonlyType(operand) => {
self.collect_type_params(operand, params, visited);
}
TypeData::TemplateLiteral(spans) => {
let spans = self.interner.template_list(spans);
for span in spans.iter() {
if let TemplateSpan::Type(inner) = span {
self.collect_type_params(*inner, params, visited);
}
}
}
TypeData::StringIntrinsic { type_arg, .. } => {
self.collect_type_params(type_arg, params, visited);
}
TypeData::Enum(_def_id, member_type) => {
// Recurse into the structural member type
self.collect_type_params(member_type, params, visited);
}
TypeData::Intrinsic(_)
| TypeData::Literal(_)
| TypeData::Lazy(_)
| TypeData::Recursive(_)
| TypeData::BoundParameter(_)
| TypeData::TypeQuery(_)
| TypeData::UniqueSymbol(_)
| TypeData::ThisType
| TypeData::ModuleNamespace(_)
| TypeData::Error => {}
TypeData::NoInfer(inner) => {
self.collect_type_params(inner, params, visited);
}
}
}
fn param_depends_on_targets(
&mut self,
name: Atom,
targets: &[Atom],
visited: &mut FxHashSet<Atom>,
) -> bool {
if targets.contains(&name) {
return true;
}
if !visited.insert(name) {
return false;
}
let Some(var) = self.find_type_param(name) else {
return false;
};
let root = self.table.find(var);
let upper_bounds = self.table.probe_value(root).upper_bounds;
for bound in upper_bounds {
for target in targets {
let mut seen = FxHashSet::default();
if self.type_contains_param(bound, *target, &mut seen) {
return true;
}
}
if let Some(TypeData::TypeParameter(info)) = self.interner.lookup(bound)
&& self.param_depends_on_targets(info.name, targets, visited)
{
return true;
}
}
false
}
fn type_contains_param(
&self,
ty: TypeId,
target: Atom,
visited: &mut FxHashSet<TypeId>,
) -> bool {
if !visited.insert(ty) {
return false;
}
let key = match self.interner.lookup(ty) {
Some(key) => key,
None => return false,
};
match key {
TypeData::TypeParameter(info) | TypeData::Infer(info) => info.name == target,
TypeData::Array(elem) => self.type_contains_param(elem, target, visited),
TypeData::Tuple(elements) => {
let elements = self.interner.tuple_list(elements);
elements
.iter()
.any(|e| self.type_contains_param(e.type_id, target, visited))
}
TypeData::Union(members) | TypeData::Intersection(members) => {
let members = self.interner.type_list(members);
members
.iter()
.any(|&member| self.type_contains_param(member, target, visited))
}
TypeData::Object(shape_id) => {
let shape = self.interner.object_shape(shape_id);
shape
.properties
.iter()
.any(|p| self.type_contains_param(p.type_id, target, visited))
}
TypeData::ObjectWithIndex(shape_id) => {
let shape = self.interner.object_shape(shape_id);
shape
.properties
.iter()
.any(|p| self.type_contains_param(p.type_id, target, visited))
|| shape.string_index.as_ref().is_some_and(|idx| {
self.type_contains_param(idx.key_type, target, visited)
|| self.type_contains_param(idx.value_type, target, visited)
})
|| shape.number_index.as_ref().is_some_and(|idx| {
self.type_contains_param(idx.key_type, target, visited)
|| self.type_contains_param(idx.value_type, target, visited)
})
}
TypeData::Application(app_id) => {
let app = self.interner.type_application(app_id);
self.type_contains_param(app.base, target, visited)
|| app
.args
.iter()
.any(|&arg| self.type_contains_param(arg, target, visited))
}
TypeData::Function(shape_id) => {
let shape = self.interner.function_shape(shape_id);
if shape.type_params.iter().any(|tp| tp.name == target) {
return false;
}
shape
.this_type
.is_some_and(|this_type| self.type_contains_param(this_type, target, visited))
|| shape
.params
.iter()
.any(|p| self.type_contains_param(p.type_id, target, visited))
|| self.type_contains_param(shape.return_type, target, visited)
}
TypeData::Callable(shape_id) => {
let shape = self.interner.callable_shape(shape_id);
let in_call = shape.call_signatures.iter().any(|sig| {
if sig.type_params.iter().any(|tp| tp.name == target) {
false
} else {
sig.this_type.is_some_and(|this_type| {
self.type_contains_param(this_type, target, visited)
}) || sig
.params
.iter()
.any(|p| self.type_contains_param(p.type_id, target, visited))
|| self.type_contains_param(sig.return_type, target, visited)
}
});
if in_call {
return true;
}
let in_construct = shape.construct_signatures.iter().any(|sig| {
if sig.type_params.iter().any(|tp| tp.name == target) {
false
} else {
sig.this_type.is_some_and(|this_type| {
self.type_contains_param(this_type, target, visited)
}) || sig
.params
.iter()
.any(|p| self.type_contains_param(p.type_id, target, visited))
|| self.type_contains_param(sig.return_type, target, visited)
}
});
if in_construct {
return true;
}
shape
.properties
.iter()
.any(|p| self.type_contains_param(p.type_id, target, visited))
}
TypeData::Conditional(cond_id) => {
let cond = self.interner.conditional_type(cond_id);
self.type_contains_param(cond.check_type, target, visited)
|| self.type_contains_param(cond.extends_type, target, visited)
|| self.type_contains_param(cond.true_type, target, visited)
|| self.type_contains_param(cond.false_type, target, visited)
}
TypeData::Mapped(mapped_id) => {
let mapped = self.interner.mapped_type(mapped_id);
if mapped.type_param.name == target {
return false;
}
self.type_contains_param(mapped.constraint, target, visited)
|| self.type_contains_param(mapped.template, target, visited)
}
TypeData::IndexAccess(obj, idx) => {
self.type_contains_param(obj, target, visited)
|| self.type_contains_param(idx, target, visited)
}
TypeData::KeyOf(operand) | TypeData::ReadonlyType(operand) => {
self.type_contains_param(operand, target, visited)
}
TypeData::TemplateLiteral(spans) => {
let spans = self.interner.template_list(spans);
spans.iter().any(|span| match span {
TemplateSpan::Text(_) => false,
TemplateSpan::Type(inner) => self.type_contains_param(*inner, target, visited),
})
}
TypeData::StringIntrinsic { type_arg, .. } => {
self.type_contains_param(type_arg, target, visited)
}
TypeData::Enum(_def_id, member_type) => {
// Recurse into the structural member type
self.type_contains_param(member_type, target, visited)
}
TypeData::Intrinsic(_)
| TypeData::Literal(_)
| TypeData::Lazy(_)
| TypeData::Recursive(_)
| TypeData::BoundParameter(_)
| TypeData::TypeQuery(_)
| TypeData::UniqueSymbol(_)
| TypeData::ThisType
| TypeData::ModuleNamespace(_)
| TypeData::Error => false,
TypeData::NoInfer(inner) => self.type_contains_param(inner, target, visited),
}
}
/// Resolve all type parameters to concrete types
pub fn resolve_all(&mut self) -> Result<Vec<(Atom, TypeId)>, InferenceError> {
// Clone type_params to avoid borrow conflict
let type_params: Vec<_> = self.type_params.clone();
let mut results = Vec::new();
for (name, var, _) in type_params {
match self.probe(var) {
Some(ty) => results.push((name, ty)),
None => return Err(InferenceError::Unresolved(var)),
}
}
Ok(results)
}
/// Get the interner reference
pub fn interner(&self) -> &dyn TypeDatabase {
self.interner
}
// =========================================================================
// Constraint Collection
// =========================================================================
/// Add a lower bound constraint: ty <: var
/// This is used when an argument type flows into a type parameter.
/// Updated to use `NakedTypeVariable` (highest priority) for direct argument inference.
pub fn add_lower_bound(&mut self, var: InferenceVar, ty: TypeId) {
self.add_candidate(var, ty, InferencePriority::NakedTypeVariable);
}
/// Add an inference candidate for a variable.
pub fn add_candidate(&mut self, var: InferenceVar, ty: TypeId, priority: InferencePriority) {
let root = self.table.find(var);
let candidate = InferenceCandidate {
type_id: ty,
priority,
is_fresh_literal: is_literal_type(self.interner, ty),
};
self.table.union_value(
root,
InferenceInfo {
candidates: vec![candidate],
..InferenceInfo::default()
},
);
}
/// Add an upper bound constraint: var <: ty
/// This is used for `extends` constraints on type parameters.
pub fn add_upper_bound(&mut self, var: InferenceVar, ty: TypeId) {
let root = self.table.find(var);
self.table.union_value(
root,
InferenceInfo {
upper_bounds: vec![ty],
..InferenceInfo::default()
},
);
}
/// Get the constraints for a variable
pub fn get_constraints(&mut self, var: InferenceVar) -> Option<ConstraintSet> {
let root = self.table.find(var);
let info = self.table.probe_value(root);
if info.is_empty() {
None
} else {
Some(ConstraintSet::from_info(&info))
}
}
/// Check if all inference candidates for a variable have `ReturnType` priority.
/// This indicates the type was inferred from callback return types (Round 2),
/// not from direct arguments (Round 1).
pub fn all_candidates_are_return_type(&mut self, var: InferenceVar) -> bool {
let root = self.table.find(var);
let info = self.table.probe_value(root);
!info.candidates.is_empty()
&& info
.candidates
.iter()
.all(|c| c.priority == InferencePriority::ReturnType)
}
/// Get the original un-widened literal candidate types for an inference variable.
pub fn get_literal_candidates(&mut self, var: InferenceVar) -> Vec<TypeId> {
let root = self.table.find(var);
let info = self.table.probe_value(root);
info.candidates
.iter()
.filter(|c| c.is_fresh_literal)
.map(|c| c.type_id)
.collect()
}
/// Collect a constraint from an assignment: source flows into target
/// If target is an inference variable, source becomes a lower bound.
/// If source is an inference variable, target becomes an upper bound.
pub const fn collect_constraint(&mut self, _source: TypeId, _target: TypeId) {
// Check if target is an inference variable (via TypeData lookup)
// For now, we rely on the caller to call add_lower_bound/add_upper_bound directly
// This is a placeholder for more sophisticated constraint collection
}
/// Perform structural type inference from a source type to a target type.
///
/// This is the core algorithm for inferring type parameters from function arguments.
/// It walks the structure of both types, collecting constraints for type parameters.
///
/// # Arguments
/// * `source` - The type from the value argument (e.g., `string` from `identity("hello")`)
/// * `target` - The type from the parameter (e.g., `T` from `function identity<T>(x: T)`)
/// * `priority` - The inference priority (e.g., `NakedTypeVariable` for direct arguments)
///
/// # Type Inference Algorithm
///
/// TypeScript uses structural type inference with the following rules:
///
/// 1. **Direct Parameter Match**: If target is a type parameter `T` we're inferring,
/// add source as a lower bound candidate for `T`.
///
/// 2. **Structural Recursion**: For complex types, recurse into the structure:
/// - Objects: Match properties recursively
/// - Arrays: Match element types
/// - Functions: Match parameters (contravariant) and return types (covariant)
///
/// 3. **Variance Handling**:
/// - Covariant positions (properties, arrays, return types): `infer(source, target)`
/// - Contravariant positions (function parameters): `infer(target, source)` (swapped!)
///
/// # Example
/// ```ignore
/// let mut ctx = InferenceContext::new(&interner);
/// let t_var = ctx.fresh_type_param(interner.intern_string("T"), false);
///
/// // Inference: identity("hello") should infer T = string
/// ctx.infer_from_types(string_type, t_type, InferencePriority::NakedTypeVariable)?;
/// ```
pub fn infer_from_types(
&mut self,
source: TypeId,
target: TypeId,
priority: InferencePriority,
) -> Result<(), InferenceError> {
// Resolve the types to their actual TypeDatas
let source_key = self.interner.lookup(source);
let target_key = self.interner.lookup(target);
// Block inference if target is NoInfer<T> (TypeScript 5.4+)
// NoInfer prevents inference from flowing through this type position
if let Some(TypeData::NoInfer(_)) = target_key {
return Ok(()); // Stop inference - don't descend into NoInfer
}
// Unwrap NoInfer from source if present (rare but possible)
let source_key = if let Some(TypeData::NoInfer(inner)) = source_key {
self.interner.lookup(inner)
} else {
source_key
};
// Case 1: Target is a TypeParameter we're inferring (Lower Bound: source <: T)
if let Some(TypeData::TypeParameter(ref param_info)) = target_key
&& let Some(var) = self.find_type_param(param_info.name)
{
// Add source as a lower bound candidate for this type parameter
self.add_candidate(var, source, priority);
return Ok(());
}
// Case 2: Source is a TypeParameter we're inferring (Upper Bound: T <: target)
// CRITICAL: This handles contravariance! When function parameters are swapped,
// the TypeParameter moves to source position and becomes an upper bound.
if let Some(TypeData::TypeParameter(ref param_info)) = source_key
&& let Some(var) = self.find_type_param(param_info.name)
{
// T <: target, so target is an UPPER bound
self.add_upper_bound(var, target);
return Ok(());
}
// Case 3: Structural recursion - match based on type structure
match (source_key, target_key) {
// Object types: recurse into properties
(Some(TypeData::Object(source_shape)), Some(TypeData::Object(target_shape))) => {
self.infer_objects(source_shape, target_shape, priority)?;
}
// Function types: handle variance (parameters are contravariant, return is covariant)
(Some(TypeData::Function(source_func)), Some(TypeData::Function(target_func))) => {
self.infer_functions(source_func, target_func, priority)?;
}
// Callable types: infer across signatures and properties
(Some(TypeData::Callable(source_call)), Some(TypeData::Callable(target_call))) => {
self.infer_callables(source_call, target_call, priority)?;
}
// Array types: recurse into element types
(Some(TypeData::Array(source_elem)), Some(TypeData::Array(target_elem))) => {
self.infer_from_types(source_elem, target_elem, priority)?;
}
// Tuple types: recurse into elements
(Some(TypeData::Tuple(source_elems)), Some(TypeData::Tuple(target_elems))) => {
self.infer_tuples(source_elems, target_elems, priority)?;
}
// Union types: try to infer against each member
(Some(TypeData::Union(source_members)), Some(TypeData::Union(target_members))) => {
self.infer_unions(source_members, target_members, priority)?;
}
// Intersection types
(
Some(TypeData::Intersection(source_members)),
Some(TypeData::Intersection(target_members)),
) => {
self.infer_intersections(source_members, target_members, priority)?;
}
// TypeApplication: recurse into instantiated type
(Some(TypeData::Application(source_app)), Some(TypeData::Application(target_app))) => {
self.infer_applications(source_app, target_app, priority)?;
}
// Index access types: infer both object and index types
(
Some(TypeData::IndexAccess(source_obj, source_idx)),
Some(TypeData::IndexAccess(target_obj, target_idx)),
) => {
self.infer_from_types(source_obj, target_obj, priority)?;
self.infer_from_types(source_idx, target_idx, priority)?;
}
// ReadonlyType: unwrap if both are readonly (e.g. readonly [T] vs readonly [number])
(
Some(TypeData::ReadonlyType(source_inner)),
Some(TypeData::ReadonlyType(target_inner)),
) => {
self.infer_from_types(source_inner, target_inner, priority)?;
}
// Unwrap ReadonlyType when only target is readonly (mutable source is compatible)
(_, Some(TypeData::ReadonlyType(target_inner))) => {
self.infer_from_types(source, target_inner, priority)?;
}
// Task #40: Template literal deconstruction for infer patterns
// Handles: source extends `prefix${infer T}suffix` ? true : false
(Some(source_key), Some(TypeData::TemplateLiteral(target_id))) => {
self.infer_from_template_literal(source, Some(&source_key), target_id, priority)?;
}
// Mapped type inference: infer from object properties against mapped type
// Handles: source { a: string, b: number } against target { [P in K]: T }
// Infers K from property names and T from property value types
(Some(TypeData::Object(source_shape)), Some(TypeData::Mapped(mapped_id))) => {
self.infer_from_mapped_type(source_shape, mapped_id, priority)?;
}
// If we can't match structurally, that's okay - it might mean the types are incompatible
// The Checker will handle this with proper error reporting
_ => {
// No structural match possible
// This is not an error - the Checker will verify assignability separately
}
}
Ok(())
}
/// Infer from object types by matching properties
fn infer_objects(
&mut self,
source_shape: ObjectShapeId,
target_shape: ObjectShapeId,
priority: InferencePriority,
) -> Result<(), InferenceError> {
let source_shape = self.interner.object_shape(source_shape);
let target_shape = self.interner.object_shape(target_shape);
// For each property in the target, try to find a matching property in the source
for target_prop in &target_shape.properties {
if let Some(source_prop) = source_shape
.properties
.iter()
.find(|p| p.name == target_prop.name)
{
self.infer_from_types(source_prop.type_id, target_prop.type_id, priority)?;
}
}
// Also check index signatures for inference
// If target has a string index signature, infer from source's string index
if let (Some(target_string_idx), Some(source_string_idx)) =
(&target_shape.string_index, &source_shape.string_index)
{
self.infer_from_types(
source_string_idx.value_type,
target_string_idx.value_type,
priority,
)?;
}
// If target has a number index signature, infer from source's number index
if let (Some(target_number_idx), Some(source_number_idx)) =
(&target_shape.number_index, &source_shape.number_index)
{
self.infer_from_types(
source_number_idx.value_type,
target_number_idx.value_type,
priority,
)?;
}
Ok(())
}
/// Infer type arguments from an object type matched against a mapped type.
///
/// When source is `{ a: string, b: number }` and target is `{ [P in K]: T }`:
/// - Infer K from the union of source property name literals ("a" | "b")
/// - Infer T from each source property value type against the mapped template
fn infer_from_mapped_type(
&mut self,
source_shape: ObjectShapeId,
mapped_id: MappedTypeId,
priority: InferencePriority,
) -> Result<(), InferenceError> {
let mapped = self.interner.mapped_type(mapped_id);
let source = self.interner.object_shape(source_shape);
if source.properties.is_empty() {
return Ok(());
}
// Infer the constraint type (K) from the union of source property names
// e.g., for { foo: string, bar: number }, K = "foo" | "bar"
let name_literals: Vec<TypeId> = source
.properties
.iter()
.map(|p| self.interner.literal_string_atom(p.name))
.collect();
let names_union = if name_literals.len() == 1 {
name_literals[0]
} else {
self.interner.union(name_literals)
};
self.infer_from_types(names_union, mapped.constraint, priority)?;
// Infer the template type (T) from each source property value type
for prop in &source.properties {
self.infer_from_types(prop.type_id, mapped.template, priority)?;
}
Ok(())
}
/// Infer from function types, handling variance correctly
fn infer_functions(
&mut self,
source_func: FunctionShapeId,
target_func: FunctionShapeId,
priority: InferencePriority,
) -> Result<(), InferenceError> {
let source_sig = self.interner.function_shape(source_func);
let target_sig = self.interner.function_shape(target_func);
tracing::trace!(
source_params = source_sig.params.len(),
target_params = target_sig.params.len(),
"infer_functions called"
);
// Parameters are contravariant: swap source and target
let mut source_params = source_sig.params.iter().peekable();
let mut target_params = target_sig.params.iter().peekable();
loop {
let source_rest = source_params.peek().is_some_and(|p| p.rest);
let target_rest = target_params.peek().is_some_and(|p| p.rest);
tracing::trace!(
source_rest,
target_rest,
"Checking rest params in loop iteration"
);
// If both have rest params, infer the rest element types
if source_rest && target_rest {
let source_param = source_params.next().unwrap();
let target_param = target_params.next().unwrap();
self.infer_from_types(target_param.type_id, source_param.type_id, priority)?;
break;
}
// If source has rest param, infer all remaining target params into it
if source_rest {
let source_param = source_params.next().unwrap();
for target_param in target_params.by_ref() {
self.infer_from_types(target_param.type_id, source_param.type_id, priority)?;
}
break;
}
// If target has rest param, infer all remaining source params into it
if target_rest {
let target_param = target_params.next().unwrap();
// CRITICAL: Check if target rest param is a type parameter (like A extends any[])
// If so, we need to infer it as a TUPLE of all remaining source params,
// not as individual param types.
//
// Example: wrap<A extends any[], R>(fn: (...args: A) => R)
// with add(a: number, b: number): number
// should infer A = [number, number], not A = number
let target_is_type_param = matches!(
self.interner.lookup(target_param.type_id),
Some(TypeData::TypeParameter(_) | TypeData::Infer(_))
);
tracing::trace!(
target_is_type_param,
target_param_type = ?target_param.type_id,
"Rest parameter inference - target is type param check"
);
if target_is_type_param {
// Collect all remaining source params into a tuple
let mut tuple_elements = Vec::new();
for source_param in source_params.by_ref() {
tuple_elements.push(TupleElement {
type_id: source_param.type_id,
name: source_param.name,
optional: source_param.optional,
rest: source_param.rest,
});
}
tracing::trace!(
num_elements = tuple_elements.len(),
"Collected source params into tuple"
);
// Infer the tuple type against the type parameter
// Note: Parameters are contravariant, so target comes first
if !tuple_elements.is_empty() {
let tuple_type = self.interner.tuple(tuple_elements);
tracing::trace!(
tuple_type = ?tuple_type,
target_param = ?target_param.type_id,
"Inferring tuple against type parameter"
);
self.infer_from_types(target_param.type_id, tuple_type, priority)?;
}
} else {
// Target rest param is not a type parameter (e.g., number[] or Array<string>)
// Infer each source param individually against the rest element type
for source_param in source_params.by_ref() {
self.infer_from_types(
target_param.type_id,
source_param.type_id,
priority,
)?;
}
}
break;
}
// Neither has rest param, do normal pairwise comparison
match (source_params.next(), target_params.next()) {
(Some(source_param), Some(target_param)) => {
// Note the swapped arguments! This is the key to handling contravariance.
self.infer_from_types(target_param.type_id, source_param.type_id, priority)?;
}
_ => break, // Mismatch in arity - stop here
}
}
// Return type is covariant: normal order
self.infer_from_types(source_sig.return_type, target_sig.return_type, priority)?;
// This type is contravariant
if let (Some(source_this), Some(target_this)) = (source_sig.this_type, target_sig.this_type)
{
self.infer_from_types(target_this, source_this, priority)?;
}
// Type predicates are covariant
if let (Some(source_pred), Some(target_pred)) =
(&source_sig.type_predicate, &target_sig.type_predicate)
{
// Compare targets by index if possible
let targets_match = match (source_pred.parameter_index, target_pred.parameter_index) {
(Some(s_idx), Some(t_idx)) => s_idx == t_idx,
_ => source_pred.target == target_pred.target,
};
tracing::trace!(
targets_match,
?source_pred.parameter_index,
?target_pred.parameter_index,
"Inferring from type predicates"
);
if targets_match
&& source_pred.asserts == target_pred.asserts
&& let (Some(source_ty), Some(target_ty)) =
(source_pred.type_id, target_pred.type_id)
{
self.infer_from_types(source_ty, target_ty, priority)?;
}
}
Ok(())
}
/// Infer from tuple types
fn infer_tuples(
&mut self,
source_elems: TupleListId,
target_elems: TupleListId,
priority: InferencePriority,
) -> Result<(), InferenceError> {
let source_list = self.interner.tuple_list(source_elems);
let target_list = self.interner.tuple_list(target_elems);
for (source_elem, target_elem) in source_list.iter().zip(target_list.iter()) {
self.infer_from_types(source_elem.type_id, target_elem.type_id, priority)?;
}
Ok(())
}
/// Infer from callable types, handling signatures and properties
fn infer_callables(
&mut self,
source_id: CallableShapeId,
target_id: CallableShapeId,
priority: InferencePriority,
) -> Result<(), InferenceError> {
let source = self.interner.callable_shape(source_id);
let target = self.interner.callable_shape(target_id);
// For each call signature in the target, try to find a compatible one in the source
for target_sig in &target.call_signatures {
for source_sig in &source.call_signatures {
if source_sig.params.len() == target_sig.params.len() {
for (s_param, t_param) in source_sig.params.iter().zip(target_sig.params.iter())
{
self.infer_from_types(t_param.type_id, s_param.type_id, priority)?;
}
self.infer_from_types(
source_sig.return_type,
target_sig.return_type,
priority,
)?;
break;
}
}
}
// For each construct signature
for target_sig in &target.construct_signatures {
for source_sig in &source.construct_signatures {
if source_sig.params.len() == target_sig.params.len() {
for (s_param, t_param) in source_sig.params.iter().zip(target_sig.params.iter())
{
self.infer_from_types(t_param.type_id, s_param.type_id, priority)?;
}
self.infer_from_types(
source_sig.return_type,
target_sig.return_type,
priority,
)?;
break;
}
}
}
// Properties
for target_prop in &target.properties {
if let Some(source_prop) = source
.properties
.iter()
.find(|p| p.name == target_prop.name)
{
self.infer_from_types(source_prop.type_id, target_prop.type_id, priority)?;
}
}
// String index
if let (Some(target_idx), Some(source_idx)) = (&target.string_index, &source.string_index) {
self.infer_from_types(source_idx.value_type, target_idx.value_type, priority)?;
}
// Number index
if let (Some(target_idx), Some(source_idx)) = (&target.number_index, &source.number_index) {
self.infer_from_types(source_idx.value_type, target_idx.value_type, priority)?;
}
Ok(())
}
/// Infer from union types
fn infer_unions(
&mut self,
source_members: TypeListId,
target_members: TypeListId,
priority: InferencePriority,
) -> Result<(), InferenceError> {
let source_list = self.interner.type_list(source_members);
let target_list = self.interner.type_list(target_members);
// TypeScript inference filtering: when the target union contains both
// type parameters and fixed types (e.g., `T | undefined`), strip source
// members that match fixed target members before inferring against the
// parameterized members. This prevents `undefined` in `number | undefined`
// from being inferred as a candidate for `T` in `T | undefined`.
let (parameterized, fixed): (Vec<TypeId>, Vec<TypeId>) = target_list
.iter()
.partition(|&&t| self.target_contains_inference_param(t));
if !parameterized.is_empty() && !fixed.is_empty() {
// Filter source: only infer members not already covered by fixed targets
for &source_ty in source_list.iter() {
let matches_fixed = fixed.contains(&source_ty);
if !matches_fixed {
for &target_ty in ¶meterized {
self.infer_from_types(source_ty, target_ty, priority)?;
}
}
}
} else {
// No filtering needed — fall back to exhaustive inference
for source_ty in source_list.iter() {
for target_ty in target_list.iter() {
self.infer_from_types(*source_ty, *target_ty, priority)?;
}
}
}
Ok(())
}
/// Check if a target type directly is or contains an inference type parameter.
fn target_contains_inference_param(&self, target: TypeId) -> bool {
let Some(key) = self.interner.lookup(target) else {
return false;
};
match key {
TypeData::TypeParameter(ref info) => self.find_type_param(info.name).is_some(),
_ => false,
}
}
/// Infer from intersection types
fn infer_intersections(
&mut self,
source_members: TypeListId,
target_members: TypeListId,
priority: InferencePriority,
) -> Result<(), InferenceError> {
let source_list = self.interner.type_list(source_members);
let target_list = self.interner.type_list(target_members);
// For intersections, we can pick any member that matches
for source_ty in source_list.iter() {
for target_ty in target_list.iter() {
// Don't fail if one member doesn't match
let _ = self.infer_from_types(*source_ty, *target_ty, priority);
}
}
Ok(())
}
/// Infer from `TypeApplication` (generic type instantiations)
fn infer_applications(
&mut self,
source_app: TypeApplicationId,
target_app: TypeApplicationId,
priority: InferencePriority,
) -> Result<(), InferenceError> {
let source_info = self.interner.type_application(source_app);
let target_info = self.interner.type_application(target_app);
// The base types must match for inference to work
if source_info.base != target_info.base {
return Ok(());
}
// Recurse into the type arguments
for (source_arg, target_arg) in source_info.args.iter().zip(target_info.args.iter()) {
self.infer_from_types(*source_arg, *target_arg, priority)?;
}
Ok(())
}
// =========================================================================
// Task #40: Template Literal Deconstruction
// =========================================================================
/// Infer from template literal patterns with `infer` placeholders.
///
/// This implements the "Reverse String Matcher" for extracting type information
/// from string literals that match template patterns like `user_${infer ID}`.
///
/// # Example
///
/// ```typescript
/// type GetID<T> = T extends `user_${infer ID}` ? ID : never;
/// // GetID<"user_123"> should infer ID = "123"
/// ```
///
/// # Algorithm
///
/// The matching is **non-greedy** for all segments except the last:
/// 1. Scan through template spans sequentially
/// 2. For text spans: match literal text at current position
/// 3. For infer type spans: capture text until next literal anchor (non-greedy)
/// 4. For the last span: capture all remaining text (greedy)
///
/// # Arguments
///
/// * `source` - The source type being checked (e.g., `"user_123"`)
/// * `source_key` - The `TypeData` of the source (cached for efficiency)
/// * `target_template` - The template literal pattern to match against
/// * `priority` - Inference priority for the extracted candidates
fn infer_from_template_literal(
&mut self,
source: TypeId,
source_key: Option<&TypeData>,
target_template: TemplateLiteralId,
priority: InferencePriority,
) -> Result<(), InferenceError> {
let spans = self.interner.template_list(target_template);
// Special case: if source is `any` or the intrinsic `string` type, all infer vars get that type
if source == TypeId::ANY
|| matches!(source_key, Some(TypeData::Intrinsic(IntrinsicKind::String)))
{
for span in spans.iter() {
if let TemplateSpan::Type(type_id) = span
&& let Some(TypeData::Infer(param_info)) = self.interner.lookup(*type_id)
&& let Some(var) = self.find_type_param(param_info.name)
{
// Source is `any` or `string`, so infer that for all variables
self.add_candidate(var, source, priority);
}
}
return Ok(());
}
// If source is a union, try to match each member against the template
if let Some(TypeData::Union(source_members)) = source_key {
let members = self.interner.type_list(*source_members);
for &member in members.iter() {
let member_key = self.interner.lookup(member);
self.infer_from_template_literal(
member,
member_key.as_ref(),
target_template,
priority,
)?;
}
return Ok(());
}
// For literal string types, perform the actual pattern matching
if let Some(source_str) = self.extract_string_literal(source)
&& let Some(captures) = self.match_template_pattern(&source_str, &spans)
{
// Convert captured strings to literal types and add as candidates
for (infer_var, captured_string) in captures {
let literal_type = self.interner.literal_string(&captured_string);
self.add_candidate(infer_var, literal_type, priority);
}
}
Ok(())
}
/// Extract a string literal value from a `TypeId`.
///
/// Returns None if the type is not a literal string.
fn extract_string_literal(&self, type_id: TypeId) -> Option<String> {
match self.interner.lookup(type_id) {
Some(TypeData::Literal(LiteralValue::String(s))) => Some(self.interner.resolve_atom(s)),
_ => None,
}
}
/// Match a source string against a template pattern, extracting infer variable bindings.
///
/// # Arguments
///
/// * `source` - The source string to match (e.g., `"user_123"`)
/// * `spans` - The template spans (e.g., `[Text("user_"), Type(ID), Text("_")]`)
///
/// # Returns
///
/// * `Some(bindings)` - Mapping from inference variables to captured strings
/// * `None` - The source doesn't match the pattern
fn match_template_pattern(
&self,
source: &str,
spans: &[TemplateSpan],
) -> Option<Vec<(InferenceVar, String)>> {
let mut bindings = Vec::new();
let mut pos = 0;
for (i, span) in spans.iter().enumerate() {
let is_last = i == spans.len() - 1;
match span {
TemplateSpan::Text(text_atom) => {
// Match literal text at current position
let text = self.interner.resolve_atom(*text_atom).to_string();
if !source.get(pos..)?.starts_with(&text) {
return None; // Text doesn't match
}
pos += text.len();
}
TemplateSpan::Type(type_id) => {
// Check if this is an infer variable
if let Some(TypeData::Infer(param_info)) = self.interner.lookup(*type_id)
&& let Some(var) = self.find_type_param(param_info.name)
{
if is_last {
// Last span: capture all remaining text (greedy)
let captured = source[pos..].to_string();
bindings.push((var, captured));
pos = source.len();
} else {
// Non-last span: capture until next literal anchor (non-greedy)
// Find the next text span to use as an anchor
if let Some(anchor_text) = self.find_next_text_anchor(spans, i) {
let anchor = self.interner.resolve_atom(anchor_text).to_string();
// Find the first occurrence of the anchor (non-greedy)
let capture_end = source[pos..].find(&anchor)? + pos;
let captured = source[pos..capture_end].to_string();
bindings.push((var, captured));
pos = capture_end;
} else {
// No text anchor found (e.g., `${infer A}${infer B}`)
// Capture empty string for non-greedy match and continue
bindings.push((var, String::new()));
// pos remains unchanged - next infer var starts here
}
}
}
}
}
}
// Must have consumed the entire source string
(pos == source.len()).then_some(bindings)
}
/// Find the next text span after a given index to use as a matching anchor.
fn find_next_text_anchor(&self, spans: &[TemplateSpan], start_idx: usize) -> Option<Atom> {
spans.iter().skip(start_idx + 1).find_map(|span| {
if let TemplateSpan::Text(text) = span {
Some(*text)
} else {
None
}
})
}
}
// DISABLED: Tests use deprecated add_candidate / resolve_with_constraints API
// The inference system has been refactored to use unification-based inference.
#[cfg(test)]
#[path = "../tests/infer_tests.rs"]
mod tests;