1
0
forked from M-Labs/nac3

[core] typecheck/type_inferencer: Infer whether variables are global

This commit is contained in:
David Mak 2024-10-05 17:07:13 +08:00
parent fa573390b1
commit b538611cb0

View File

@ -586,15 +586,45 @@ impl<'a> Fold<()> for Inferencer<'a> {
unreachable!("must be tobj")
}
} else {
let top_level_defs = &self.top_level.definitions.read();
if !self.defined_identifiers.contains_key(id) {
match self.function_data.resolver.get_symbol_type(
self.unifier,
&self.top_level.definitions.read(),
top_level_defs,
self.primitives,
*id,
) {
Ok(_) => {
self.defined_identifiers.insert(*id, IdentifierInfo::default());
// Determine if the referenced id refers to a global symbol
let is_explicit = top_level_defs
.iter()
.map(|def| match *def.read() {
TopLevelDef::Class { name, .. } => (name, false),
TopLevelDef::Function { simple_name, .. } => {
(simple_name, false)
}
TopLevelDef::Variable { simple_name, .. } => {
(simple_name, true)
}
})
.find(|(global, _)| global == id)
.map(|(_, has_explicit_prop)| has_explicit_prop);
self.defined_identifiers.insert(
*id,
IdentifierInfo {
source: match is_explicit {
Some(true) => DeclarationSource::Global {
is_explicit: Some(false),
},
Some(false) => {
DeclarationSource::Global { is_explicit: None }
}
None => DeclarationSource::Local,
},
},
);
}
Err(e) => {
return report_error(