Skip to main content

harn_modules/
typecheck.rs

1//! Canonical type-checker projection of a resolved module graph.
2
3use std::path::Path;
4
5use harn_parser::analysis::TypeCheckConfig;
6use harn_parser::NamespaceImportBinding;
7
8use crate::ModuleGraph;
9
10impl ModuleGraph {
11    /// Project every import visible to `file` into one type-checker config.
12    ///
13    /// Callers layer execution-specific strictness and authority on top. The
14    /// graph remains the sole owner of named, typed, callable, and namespace
15    /// import resolution.
16    pub fn typecheck_import_config_for_file(&self, file: &Path) -> TypeCheckConfig {
17        let namespace_imports = self
18            .namespace_imports_for_file(file)
19            .unwrap_or_default()
20            .into_iter()
21            .map(|info| {
22                (
23                    info.alias,
24                    NamespaceImportBinding {
25                        module_path: info.raw_path,
26                        members: info.member_names.into_iter().collect(),
27                        member_types: info
28                            .member_signatures
29                            .iter()
30                            .map(|(name, signature)| (name.clone(), signature.fn_type.clone()))
31                            .collect(),
32                        member_param_names: info
33                            .member_signatures
34                            .iter()
35                            .map(|(name, signature)| (name.clone(), signature.param_names.clone()))
36                            .collect(),
37                        member_required_params: info
38                            .member_signatures
39                            .iter()
40                            .map(|(name, signature)| (name.clone(), signature.required_params))
41                            .collect(),
42                        member_type_predicates: info
43                            .member_signatures
44                            .into_iter()
45                            .filter_map(|(name, signature)| {
46                                signature.type_predicate.map(|predicate| (name, predicate))
47                            })
48                            .collect(),
49                    },
50                )
51            })
52            .collect();
53
54        TypeCheckConfig::new()
55            .with_imported_names(self.imported_names_for_file(file))
56            .with_imported_type_decls(
57                self.imported_type_declarations_for_file(file)
58                    .unwrap_or_default(),
59            )
60            .with_imported_callable_decls(
61                self.imported_callable_declarations_for_file(file)
62                    .unwrap_or_default(),
63            )
64            .with_namespace_imports(namespace_imports)
65    }
66}