Skip to main content

shape_runtime/type_system/
mod.rs

1//! Shape Type System
2//!
3//! This module implements type inference, type checking, and type unification
4//! for the Shape language, providing static type safety while maintaining
5//! ergonomic syntax through type inference.
6//!
7//! ## Architecture: Storage vs Semantic Types
8//!
9//! - **SemanticType**: What users see in code (`Option<f64>`, `Result<Table<Row>>`)
10//! - **StorageType**: How data is physically stored (NaN sentinels, bitmaps)
11//!
12//! This separation allows the type checker to be strict while storage can be
13//! optimized for JIT/SIMD performance.
14//!
15//! ## Module Structure
16//!
17//! - `types/`: Core type definitions (Type, TypeVar, TypeScheme, TypeConstraint)
18//! - `unification/`: Type unification algorithm (Robinson's algorithm)
19//! - `solver/`: Constraint solving
20//! - `checking/`: Pattern checking and type checking
21//! - `environment/`: Type environments and scopes
22//! - `inference/`: Type inference engine
23
24pub mod checker;
25pub mod checking;
26pub mod constraints;
27pub mod environment;
28pub mod error_bridge;
29pub mod errors;
30pub mod exhaustiveness;
31pub mod inference;
32pub mod semantic;
33pub mod storage;
34pub mod suggestions;
35pub mod types;
36pub mod unification;
37pub mod universal_error;
38
39// Re-export from types module
40pub use types::{
41    BuiltinTypes, TYVAR_ANNOTATION_PREFIX, Type, TypeConstraint, TypeScheme, TypeVar, TypeVarGen,
42    annotation_as_tyvar, annotation_to_semantic, annotation_to_string, semantic_to_annotation,
43    substitute, tyvar_to_annotation,
44};
45
46// Re-export other public types
47pub use checker::{
48    TypeAnalysisMode, TypeCheckResult, TypeChecker, TypeWarning, analyze_program,
49    analyze_program_with_mode,
50};
51pub use environment::TypeEnvironment;
52pub use errors::{TypeError, TypeErrorWithLocation, TypeResult};
53pub use inference::{PropertyAssignment, PropertyAssignmentCollector, TypeInferenceEngine};
54pub use semantic::{EnumVariant, FunctionParam, FunctionSignature, SemanticType, TypeVarId};
55pub use storage::StorageType;
56pub use universal_error::{ErrorDetails, ErrorLocation, UniversalError};
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use shape_ast::ast::TypeAnnotation;
62
63    #[test]
64    fn test_type_to_semantic_primitives() {
65        let num = BuiltinTypes::number();
66        let semantic = num.to_semantic().unwrap();
67        assert_eq!(semantic, SemanticType::Number);
68
69        let string = BuiltinTypes::string();
70        let semantic = string.to_semantic().unwrap();
71        assert_eq!(semantic, SemanticType::String);
72
73        let boolean = BuiltinTypes::boolean();
74        let semantic = boolean.to_semantic().unwrap();
75        assert_eq!(semantic, SemanticType::Bool);
76    }
77
78    #[test]
79    fn test_type_to_semantic_option() {
80        let option_num = Type::Generic {
81            base: Box::new(Type::Concrete(TypeAnnotation::Reference(
82                "Option".into(),
83            ))),
84            args: vec![BuiltinTypes::number()],
85        };
86        let semantic = option_num.to_semantic().unwrap();
87        assert_eq!(
88            semantic,
89            SemanticType::Option(Box::new(SemanticType::Number))
90        );
91    }
92
93    #[test]
94    fn test_type_to_semantic_result() {
95        let result_num = Type::Generic {
96            base: Box::new(Type::Concrete(TypeAnnotation::Reference(
97                "Result".into(),
98            ))),
99            args: vec![BuiltinTypes::number()],
100        };
101        let semantic = result_num.to_semantic().unwrap();
102        assert_eq!(
103            semantic,
104            SemanticType::Result {
105                ok_type: Box::new(SemanticType::Number),
106                err_type: None
107            }
108        );
109    }
110
111    #[test]
112    fn test_type_to_semantic_generic_table() {
113        let table_num = Type::Generic {
114            base: Box::new(Type::Concrete(TypeAnnotation::Reference(
115                "Table".into(),
116            ))),
117            args: vec![BuiltinTypes::number()],
118        };
119        let semantic = table_num.to_semantic().unwrap();
120        assert_eq!(
121            semantic,
122            SemanticType::Generic {
123                name: "Table".to_string(),
124                args: vec![SemanticType::Number],
125            }
126        );
127    }
128
129    #[test]
130    fn test_semantic_to_inference_roundtrip() {
131        let original = SemanticType::Option(Box::new(SemanticType::Number));
132        let inference = original.to_inference_type();
133        let roundtrip = inference.to_semantic().unwrap();
134        assert_eq!(original, roundtrip);
135    }
136
137    #[test]
138    fn test_semantic_result_to_inference() {
139        let result_type = SemanticType::Result {
140            ok_type: Box::new(SemanticType::String),
141            err_type: None,
142        };
143        let inference = result_type.to_inference_type();
144        let roundtrip = inference.to_semantic().unwrap();
145        assert_eq!(result_type, roundtrip);
146    }
147}