Skip to main content

mir_analyzer/
type_env.rs

1// crates/mir-analyzer/src/type_env.rs
2use indexmap::IndexMap;
3use mir_types::Union;
4use std::sync::Arc;
5
6/// Identifies a single analysis scope within a project.
7#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8pub enum ScopeId {
9    Function { file: Arc<str>, name: Arc<str> },
10    Method { class: Arc<str>, method: Arc<str> },
11}
12
13/// Variable type environment for one scope — the stable public view of Context.vars.
14#[derive(Debug)]
15pub struct TypeEnv {
16    vars: IndexMap<String, Union>,
17}
18
19impl TypeEnv {
20    pub(crate) fn new(vars: IndexMap<String, Union>) -> Self {
21        Self { vars }
22    }
23
24    /// Returns the inferred type of `$name`, or `None` if the variable was not tracked.
25    pub fn get_var(&self, name: &str) -> Option<&Union> {
26        self.vars.get(name)
27    }
28
29    /// Iterates over all variable names tracked in this scope.
30    pub fn var_names(&self) -> impl Iterator<Item = &str> {
31        self.vars.keys().map(|s| s.as_str())
32    }
33}