Skip to main content

tsz_solver/
lib.rs

1//! Query-Based Structural Solver
2//!
3//! This module implements a declarative, query-based type solver architecture.
4//! It uses:
5//!
6//! - **Ena**: For unification (Union-Find) in generic type inference
7//! - **Custom `TypeData`**: Structural type representation with interning
8//! - **Cycle Detection**: Coinductive semantics for recursive types
9//!
10//! Key benefits:
11//! - O(1) type equality via interning (`TypeId` comparison)
12//! - Automatic cycle handling via coinductive semantics
13//! - Lazy evaluation - only compute types that are queried
14mod application;
15mod caches;
16pub mod canonicalize;
17pub mod classes;
18mod contextual;
19pub mod def;
20mod diagnostics;
21pub mod evaluation;
22#[cfg(test)]
23mod flow_analysis;
24mod format;
25mod inference;
26mod instantiation;
27mod intern;
28pub mod judge;
29mod narrowing;
30pub mod objects;
31pub mod operations;
32pub mod recursion;
33pub mod relations;
34#[cfg(test)]
35mod sound;
36mod type_factory;
37pub mod type_queries;
38pub mod type_resolver;
39pub mod types;
40pub mod unsoundness_audit;
41pub mod utils;
42pub mod visitor;
43mod visitors;
44pub use intern::TypeInterner;
45pub use operations::infer_generic_function;
46pub use operations::widening;
47pub use visitors::visitor::{
48    ConstAssertionVisitor, ObjectTypeKind, RecursiveTypeCollector, TypeCollectorVisitor, TypeKind,
49    TypeKindVisitor, TypePredicateVisitor, TypeVisitor, application_id, array_element_type,
50    bound_parameter_index, callable_shape_id, classify_object_type, collect_all_types,
51    collect_enum_def_ids, collect_infer_bindings, collect_lazy_def_ids, collect_referenced_types,
52    collect_type_queries, conditional_type_id, contains_error_type, contains_infer_types,
53    contains_this_type, contains_type_matching, contains_type_parameters, enum_components,
54    for_each_child, for_each_child_by_id, function_shape_id, index_access_parts,
55    intersection_list_id, intrinsic_kind, is_array_type, is_conditional_type, is_empty_object_type,
56    is_empty_object_type_db, is_enum_type, is_error_type, is_function_type, is_function_type_db,
57    is_generic_application, is_identity_comparable_type, is_index_access_type,
58    is_intersection_type, is_literal_type, is_literal_type_db, is_mapped_type,
59    is_module_namespace_type, is_object_like_type, is_object_like_type_db, is_primitive_type,
60    is_template_literal_type, is_this_type, is_tuple_type, is_type_kind, is_type_parameter,
61    is_type_reference, is_union_type, keyof_inner_type, lazy_def_id, literal_number,
62    literal_string, literal_value, mapped_type_id, module_namespace_symbol_ref,
63    no_infer_inner_type, object_shape_id, object_with_index_shape_id, readonly_inner_type,
64    recursive_index, ref_symbol, string_intrinsic_components, template_literal_id, test_type,
65    tuple_list_id, type_param_info, type_query_symbol, union_list_id, unique_symbol_ref,
66    walk_referenced_types,
67};
68
69pub use application::*;
70pub use caches::db::{QueryDatabase, TypeDatabase};
71pub use caches::query_cache::{QueryCache, RelationCacheProbe, RelationCacheStats};
72pub use canonicalize::*;
73pub use classes::class_hierarchy::*;
74pub use classes::inheritance::*;
75pub use contextual::{ContextualTypeContext, apply_contextual_type};
76pub use def::*;
77pub use diagnostics::SubtypeFailureReason;
78pub use diagnostics::builders::{
79    DiagnosticBuilder, DiagnosticCollector, SourceLocation, SpannedDiagnosticBuilder,
80};
81pub use diagnostics::{
82    DiagnosticArg, DiagnosticSeverity, PendingDiagnostic, PendingDiagnosticBuilder, SourceSpan,
83};
84pub use evaluation::evaluate::*;
85pub use format::TypeFormatter;
86pub use inference::infer::*;
87pub use instantiation::instantiate::{
88    MAX_INSTANTIATION_DEPTH, TypeInstantiator, TypeSubstitution, instantiate_type,
89    substitute_this_type,
90};
91pub use narrowing::*;
92pub use objects::*;
93pub use operations::compound_assignment;
94pub use operations::compound_assignment::*;
95pub use operations::expression_ops;
96pub use operations::expression_ops::*;
97pub use operations::{
98    AssignabilityChecker, BinaryOpEvaluator, BinaryOpResult, CallEvaluator, CallResult,
99    MAX_CONSTRAINT_RECURSION_DEPTH, PrimitiveClass, get_contextual_signature_with_compat_checker,
100};
101pub use relations::compat::*;
102pub use relations::judge::*;
103pub use relations::lawyer::AnyPropagationRules;
104pub use relations::relation_queries::*;
105pub use relations::subtype::{
106    AnyPropagationMode, SubtypeChecker, SubtypeResult, TypeEnvironment, TypeResolver,
107    are_types_structurally_identical, is_subtype_of,
108};
109pub use type_factory::*;
110pub use types::{
111    CallSignature, CallableShapeId, IntrinsicKind, LiteralValue, MappedModifier, ObjectShapeId,
112    PropertyInfo, PropertyLookup, SymbolRef, TypeApplication, TypeApplicationId, TypeData, TypeId,
113    TypeListId, Visibility, is_compiler_managed_type,
114};
115pub use types::{
116    CallableShape, ConditionalType, FunctionShape, FunctionShapeId, IndexSignature, MappedType,
117    MappedTypeId, ObjectFlags, ObjectShape, OrderedFloat, ParamInfo, RelationCacheKey,
118    TemplateSpan, TupleElement, TupleListId, TypeParamInfo, TypePredicate, TypePredicateTarget,
119};
120pub use unsoundness_audit::*;
121pub use widening::*;
122
123// Test modules: Most are loaded by their source files via #[path = "tests/..."] declarations.
124// Only include modules here that aren't loaded elsewhere to avoid duplicate_mod warnings.
125#[cfg(test)]
126#[path = "../tests/bidirectional_tests.rs"]
127mod bidirectional_tests;
128// callable_tests: loaded from relations/subtype.rs
129// compat_tests: loaded from relations/compat.rs
130// contextual_tests: loaded from contextual/mod.rs
131// db_tests: loaded from caches/db.rs
132// diagnostics_tests: loaded from diagnostics.rs
133// evaluate_tests: loaded from evaluation/evaluate.rs
134// index_signature_tests: loaded from relations/subtype.rs
135// infer_tests: loaded from inference/infer.rs
136// instantiate_tests: loaded from instantiation/instantiate.rs
137#[cfg(test)]
138#[path = "../tests/integration_tests.rs"]
139mod integration_tests;
140// intern_tests: loaded from intern/mod.rs
141#[cfg(test)]
142#[path = "../tests/enum_nominality.rs"]
143mod enum_nominality;
144#[cfg(test)]
145#[path = "../tests/intersection_union_tests.rs"]
146mod intersection_union_tests;
147// lawyer_tests: loaded from lawyer.rs
148#[cfg(test)]
149#[path = "../tests/mapped_key_remap_tests.rs"]
150mod mapped_key_remap_tests;
151// narrowing_tests: loaded from narrowing/mod.rs
152// operations_tests: loaded from operations/mod.rs
153// subtype_tests: loaded from relations/subtype.rs
154#[cfg(test)]
155#[path = "../tests/intersection_distributivity_tests.rs"]
156mod intersection_distributivity_tests;
157#[cfg(test)]
158#[path = "../tests/intersection_type_param_tests.rs"]
159mod intersection_type_param_tests;
160#[cfg(test)]
161#[path = "../tests/template_expansion_tests.rs"]
162mod template_expansion_tests;
163#[cfg(test)]
164#[path = "../tests/template_literal_comprehensive_test.rs"]
165mod template_literal_comprehensive_test;
166#[cfg(test)]
167#[path = "../tests/template_literal_subtype_tests.rs"]
168mod template_literal_subtype_tests;
169#[cfg(test)]
170#[path = "../tests/type_law_tests.rs"]
171mod type_law_tests;
172// types_tests: loaded from types.rs
173// union_tests: loaded from relations/subtype.rs
174#[cfg(test)]
175#[path = "../tests/isomorphism_tests.rs"]
176mod isomorphism_tests;
177#[cfg(test)]
178#[path = "../tests/isomorphism_validation.rs"]
179mod isomorphism_validation;
180// solver_refactoring_tests: kept in root crate (depends on checker types)
181#[cfg(test)]
182#[path = "../tests/array_comprehensive_tests.rs"]
183mod array_comprehensive_tests;
184#[cfg(test)]
185#[path = "../tests/async_promise_comprehensive_tests.rs"]
186mod async_promise_comprehensive_tests;
187#[cfg(test)]
188#[path = "../tests/class_comprehensive_tests.rs"]
189mod class_comprehensive_tests;
190// compound_assignment_tests: loaded from operations/compound_assignment.rs
191#[cfg(test)]
192#[path = "../tests/conditional_comprehensive_tests.rs"]
193mod conditional_comprehensive_tests;
194#[cfg(test)]
195#[path = "../tests/function_comprehensive_tests.rs"]
196mod function_comprehensive_tests;
197#[cfg(test)]
198#[path = "../tests/index_access_comprehensive_tests.rs"]
199mod index_access_comprehensive_tests;
200#[cfg(test)]
201#[path = "../tests/interface_comprehensive_tests.rs"]
202mod interface_comprehensive_tests;
203#[cfg(test)]
204#[path = "../tests/keyof_comprehensive_tests.rs"]
205mod keyof_comprehensive_tests;
206#[cfg(test)]
207#[path = "../tests/mapped_comprehensive_tests.rs"]
208mod mapped_comprehensive_tests;
209#[cfg(test)]
210#[path = "../tests/narrowing_comprehensive_tests.rs"]
211mod narrowing_comprehensive_tests;
212#[cfg(test)]
213#[path = "../tests/template_literal_comprehensive_tests.rs"]
214mod template_literal_comprehensive_tests;
215#[cfg(test)]
216#[path = "../tests/tuple_comprehensive_tests.rs"]
217mod tuple_comprehensive_tests;
218#[cfg(test)]
219#[path = "../tests/type_parameter_comprehensive_tests.rs"]
220mod type_parameter_comprehensive_tests;
221#[cfg(test)]
222#[path = "tests/type_queries_contextual_structure_tests.rs"]
223mod type_queries_contextual_structure_tests;
224#[cfg(test)]
225#[path = "tests/type_queries_function_rewrite_tests.rs"]
226mod type_queries_function_rewrite_tests;
227#[cfg(test)]
228#[path = "tests/type_queries_property_names_tests.rs"]
229mod type_queries_property_names_tests;
230#[cfg(test)]
231#[path = "tests/typedata_contract_tests.rs"]
232mod typedata_contract_tests;
233#[cfg(test)]
234#[path = "../tests/union_intersection_comprehensive_tests.rs"]
235mod union_intersection_comprehensive_tests;
236#[cfg(test)]
237#[path = "tests/visitor_tests.rs"]
238mod visitor_tests;