use std::collections::{HashMap, HashSet};
use super::{Type, parse_type_str_strict};
use crate::ast::{
BinOp, Expr, FnDef, Literal, Module, Pattern, Spanned, Stmt, TailCallData, TopLevel, TypeDef,
};
mod builtins;
pub mod effect_classification;
pub mod effect_lifting;
mod exhaustiveness;
mod flow;
pub mod hostile_effects;
pub mod hostile_values;
mod infer;
mod memo;
mod modules;
pub mod oracle_subtypes;
pub mod proof_trust_header;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone)]
pub struct TypeError {
pub message: String,
pub line: usize,
pub col: usize,
pub secondary: Option<TypeErrorSpan>,
}
#[derive(Debug, Clone)]
pub struct TypeErrorSpan {
pub line: usize,
pub col: usize,
pub label: String,
}
#[derive(Debug)]
pub struct TypeCheckResult {
pub errors: Vec<TypeError>,
pub fn_sigs: HashMap<String, (Vec<Type>, Type, Vec<String>)>,
pub memo_safe_types: HashSet<String>,
pub unused_bindings: Vec<(String, String, usize)>,
}
pub fn run_type_check(items: &[TopLevel]) -> Vec<TypeError> {
run_type_check_with_base(items, None)
}
pub fn run_type_check_with_base(items: &[TopLevel], base_dir: Option<&str>) -> Vec<TypeError> {
run_type_check_full(items, base_dir).errors
}
pub fn run_type_check_full(items: &[TopLevel], base_dir: Option<&str>) -> TypeCheckResult {
let mut checker = TypeChecker::new();
checker.check(items, base_dir);
finalize_check_result(checker, items)
}
pub fn run_type_check_with_loaded(
items: &[TopLevel],
loaded: &[crate::source::LoadedModule],
) -> TypeCheckResult {
let mut checker = TypeChecker::new();
checker.check_with_loaded(items, loaded);
finalize_check_result(checker, items)
}
fn finalize_check_result(mut checker: TypeChecker, items: &[TopLevel]) -> TypeCheckResult {
let fn_sigs: HashMap<String, (Vec<Type>, Type, Vec<String>)> = checker
.fn_sigs
.iter()
.map(|(k, v)| {
(
k.clone(),
(v.params.clone(), v.ret.clone(), v.effects.clone()),
)
})
.collect();
let memo_safe_types = checker.compute_memo_safe_types(items);
check_module_effect_boundary(items, &mut checker.errors);
TypeCheckResult {
errors: checker.errors,
fn_sigs,
memo_safe_types,
unused_bindings: checker.unused_warnings,
}
}
fn check_module_effect_boundary(items: &[TopLevel], errors: &mut Vec<TypeError>) {
let Some(allowed) = items.iter().find_map(|i| match i {
TopLevel::Module(m) => m.effects.as_ref().map(|e| (e, m)),
_ => None,
}) else {
return;
};
let (allowed_list, module) = allowed;
let allowed_namespaces: HashSet<&str> = allowed_list
.iter()
.filter(|e| !e.contains('.'))
.map(|e| e.as_str())
.collect();
let allowed_methods: HashSet<&str> = allowed_list.iter().map(|e| e.as_str()).collect();
for item in items {
let TopLevel::FnDef(fd) = item else { continue };
for eff in &fd.effects {
let method = eff.node.as_str();
if allowed_methods.contains(method) {
continue;
}
if let Some((ns, _)) = method.split_once('.')
&& allowed_namespaces.contains(ns)
{
continue;
}
errors.push(TypeError {
message: format!(
"module '{}' declared `effects [{}]` but '{}' uses '{}' which is not in the declared boundary",
module.name,
allowed_list.join(", "),
fd.name,
method
),
line: eff.line,
col: 1,
secondary: module.effects_line.map(|l| TypeErrorSpan {
line: l,
col: 1,
label: "module effects declared here".to_string(),
}),
});
}
}
}
#[derive(Debug, Clone)]
struct FnSig {
params: Vec<Type>,
ret: Type,
effects: Vec<String>,
}
struct TypeChecker {
fn_sigs: HashMap<String, FnSig>,
value_members: HashMap<String, Type>,
record_field_types: HashMap<String, Type>,
sig_aliases: HashMap<String, String>,
type_variants: HashMap<String, Vec<String>>,
globals: HashMap<String, Type>,
locals: HashMap<String, Type>,
errors: Vec<TypeError>,
current_fn_ret: Option<Type>,
current_fn_line: Option<usize>,
opaque_types: HashSet<String>,
used_names: HashSet<String>,
fn_bindings: Vec<(String, usize)>,
unused_warnings: Vec<(String, String, usize)>,
in_verify_trace_context: bool,
}
impl TypeChecker {
fn new() -> Self {
let mut type_variants = HashMap::new();
type_variants.insert(
"Result".to_string(),
vec!["Ok".to_string(), "Err".to_string()],
);
type_variants.insert(
"Option".to_string(),
vec!["Some".to_string(), "None".to_string()],
);
let mut tc = TypeChecker {
fn_sigs: HashMap::new(),
value_members: HashMap::new(),
record_field_types: HashMap::new(),
sig_aliases: HashMap::new(),
type_variants,
globals: HashMap::new(),
locals: HashMap::new(),
errors: Vec::new(),
current_fn_ret: None,
current_fn_line: None,
opaque_types: HashSet::new(),
used_names: HashSet::new(),
fn_bindings: Vec::new(),
unused_warnings: Vec::new(),
in_verify_trace_context: false,
};
tc.register_builtins();
tc
}
fn find_fn_sig(&self, key: &str) -> Option<&FnSig> {
self.fn_sigs
.get(key)
.or_else(|| self.sig_aliases.get(key).and_then(|c| self.fn_sigs.get(c)))
}
fn find_value_member(&self, key: &str) -> Option<&Type> {
self.value_members.get(key).or_else(|| {
self.sig_aliases
.get(key)
.and_then(|c| self.value_members.get(c))
})
}
fn find_record_field_type(&self, key: &str) -> Option<&Type> {
self.record_field_types.get(key).or_else(|| {
self.sig_aliases
.get(key)
.and_then(|c| self.record_field_types.get(c))
})
}
fn caller_has_effect(&self, caller_effects: &[String], required_effect: &str) -> bool {
caller_effects
.iter()
.any(|declared| crate::effects::effect_satisfies(declared, required_effect))
}
fn error(&mut self, msg: impl Into<String>) {
let line = self.current_fn_line.unwrap_or(1);
self.errors.push(TypeError {
message: msg.into(),
line,
col: 0,
secondary: None,
});
}
fn error_at_line(&mut self, line: usize, msg: impl Into<String>) {
self.errors.push(TypeError {
message: msg.into(),
line,
col: 0,
secondary: None,
});
}
fn insert_sig(&mut self, name: &str, params: &[Type], ret: Type, effects: &[&str]) {
self.fn_sigs.insert(
name.to_string(),
FnSig {
params: params.to_vec(),
ret,
effects: effects.iter().map(|s| s.to_string()).collect(),
},
);
}
fn fn_type_from_sig(sig: &FnSig) -> Type {
Type::Fn(
sig.params.clone(),
Box::new(sig.ret.clone()),
sig.effects.clone(),
)
}
fn sig_from_callable_type(ty: &Type) -> Option<FnSig> {
match ty {
Type::Fn(params, ret, effects) => Some(FnSig {
params: params.clone(),
ret: *ret.clone(),
effects: effects.clone(),
}),
_ => None,
}
}
fn binding_type(&self, name: &str) -> Option<Type> {
self.locals
.get(name)
.or_else(|| self.globals.get(name))
.cloned()
}
pub(super) fn constraint_compatible(actual: &Type, expected: &Type) -> bool {
if matches!(actual, Type::Unknown) && !matches!(expected, Type::Unknown) {
return false;
}
actual.compatible(expected)
}
}