1use std::collections::{HashMap, HashSet};
12use std::path::Path;
13
14use super::{Type, parse_type_str_strict};
15use crate::ast::{BinOp, Expr, FnDef, Literal, Module, Pattern, Stmt, TopLevel, TypeDef};
16use crate::source::{
17 canonicalize_path, find_module_file, parse_source, require_module_declaration,
18};
19
20mod builtins;
21mod exhaustiveness;
22mod flow;
23mod infer;
24mod memo;
25mod modules;
26
27#[cfg(test)]
28mod tests;
29
30#[derive(Debug, Clone)]
35pub struct TypeError {
36 pub message: String,
37 pub line: usize,
38 pub col: usize,
39}
40
41#[derive(Debug)]
43pub struct TypeCheckResult {
44 pub errors: Vec<TypeError>,
45 pub fn_sigs: HashMap<String, (Vec<Type>, Type, Vec<String>)>,
48 pub memo_safe_types: HashSet<String>,
50}
51
52pub fn run_type_check(items: &[TopLevel]) -> Vec<TypeError> {
53 run_type_check_with_base(items, None)
54}
55
56pub fn run_type_check_with_base(items: &[TopLevel], base_dir: Option<&str>) -> Vec<TypeError> {
57 run_type_check_full(items, base_dir).errors
58}
59
60pub fn run_type_check_full(items: &[TopLevel], base_dir: Option<&str>) -> TypeCheckResult {
61 let mut checker = TypeChecker::new();
62 checker.check(items, base_dir);
63
64 let fn_sigs: HashMap<String, (Vec<Type>, Type, Vec<String>)> = checker
66 .fn_sigs
67 .iter()
68 .map(|(k, v)| {
69 (
70 k.clone(),
71 (v.params.clone(), v.ret.clone(), v.effects.clone()),
72 )
73 })
74 .collect();
75
76 let memo_safe_types = checker.compute_memo_safe_types(items);
78
79 TypeCheckResult {
80 errors: checker.errors,
81 fn_sigs,
82 memo_safe_types,
83 }
84}
85
86#[derive(Debug, Clone)]
91struct FnSig {
92 params: Vec<Type>,
93 ret: Type,
94 effects: Vec<String>,
95}
96
97#[derive(Debug, Clone)]
98struct ModuleSigCache {
99 fn_entries: Vec<(String, FnSig)>,
100 value_entries: Vec<(String, Type)>,
101 record_field_entries: Vec<(String, Type)>,
102 type_variants: Vec<(String, Vec<String>)>,
103}
104
105struct TypeChecker {
106 fn_sigs: HashMap<String, FnSig>,
107 module_sig_cache: HashMap<String, ModuleSigCache>,
108 value_members: HashMap<String, Type>,
109 record_field_types: HashMap<String, Type>,
113 type_variants: HashMap<String, Vec<String>>,
116 globals: HashMap<String, Type>,
118 locals: HashMap<String, Type>,
120 errors: Vec<TypeError>,
121 current_fn_ret: Option<Type>,
123 current_fn_line: Option<usize>,
125}
126
127impl TypeChecker {
128 fn new() -> Self {
129 let mut type_variants = HashMap::new();
130 type_variants.insert(
131 "Result".to_string(),
132 vec!["Ok".to_string(), "Err".to_string()],
133 );
134 type_variants.insert(
135 "Option".to_string(),
136 vec!["Some".to_string(), "None".to_string()],
137 );
138
139 let mut tc = TypeChecker {
140 fn_sigs: HashMap::new(),
141 module_sig_cache: HashMap::new(),
142 value_members: HashMap::new(),
143 record_field_types: HashMap::new(),
144 type_variants,
145 globals: HashMap::new(),
146 locals: HashMap::new(),
147 errors: Vec::new(),
148 current_fn_ret: None,
149 current_fn_line: None,
150 };
151 tc.register_builtins();
152 tc
153 }
154
155 fn caller_has_effect(&self, caller_effects: &[String], required_effect: &str) -> bool {
157 caller_effects
158 .iter()
159 .any(|declared| crate::effects::effect_satisfies(declared, required_effect))
160 }
161
162 fn error(&mut self, msg: impl Into<String>) {
163 let line = self.current_fn_line.unwrap_or(1);
164 self.errors.push(TypeError {
165 message: msg.into(),
166 line,
167 col: 0,
168 });
169 }
170
171 fn error_at_line(&mut self, line: usize, msg: impl Into<String>) {
172 self.errors.push(TypeError {
173 message: msg.into(),
174 line,
175 col: 0,
176 });
177 }
178
179 fn insert_sig(&mut self, name: &str, params: &[Type], ret: Type, effects: &[&str]) {
180 self.fn_sigs.insert(
181 name.to_string(),
182 FnSig {
183 params: params.to_vec(),
184 ret,
185 effects: effects.iter().map(|s| s.to_string()).collect(),
186 },
187 );
188 }
189
190 fn fn_type_from_sig(sig: &FnSig) -> Type {
191 Type::Fn(
192 sig.params.clone(),
193 Box::new(sig.ret.clone()),
194 sig.effects.clone(),
195 )
196 }
197
198 fn sig_from_callable_type(ty: &Type) -> Option<FnSig> {
199 match ty {
200 Type::Fn(params, ret, effects) => Some(FnSig {
201 params: params.clone(),
202 ret: *ret.clone(),
203 effects: effects.clone(),
204 }),
205 _ => None,
206 }
207 }
208
209 fn binding_type(&self, name: &str) -> Option<Type> {
210 self.locals
211 .get(name)
212 .or_else(|| self.globals.get(name))
213 .cloned()
214 }
215
216 pub(super) fn constraint_compatible(actual: &Type, expected: &Type) -> bool {
223 if matches!(actual, Type::Unknown) && !matches!(expected, Type::Unknown) {
224 return false;
225 }
226 actual.compatible(expected)
227 }
228}