forked from M-Labs/nac3
1
0
Fork 0

[core] typecheck: Disallow redeclaration of var shadowing global

This commit is contained in:
David Mak 2024-10-05 17:08:46 +08:00
parent 3ce2eddcdc
commit 42a2f243b5
1 changed files with 14 additions and 0 deletions

View File

@ -34,6 +34,20 @@ impl<'a> Inferencer<'a> {
Err(HashSet::from([format!("cannot assign to a `none` (at {})", pattern.location)]))
}
ExprKind::Name { id, .. } => {
// If `id` refers to a declared symbol, reject this assignment if it is used in the
// context of an (implicit) global variable
if let Some(id_info) = self.defined_identifiers.get(id) {
if matches!(
id_info.source,
DeclarationSource::Global { is_explicit: Some(false) }
) {
return Err(HashSet::from([format!(
"cannot access local variable '{id}' before it is declared (at {})",
pattern.location
)]));
}
}
if !defined_identifiers.contains_key(id) {
defined_identifiers.insert(*id, IdentifierInfo::default());
}