Skip to content

Commit 0e92465

Browse files
committed
feat: prevented global scope -> local scope duplicate entries by checking for global scope entries for variable names and arguments to prevent some naming issues
1 parent 0cb18fb commit 0e92465

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

compiler/astoir_hir/src/scope.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
33
use compiler_global_scope::key::EntryKey;
44
use compiler_typing::{TypedGlobalScope, TypedGlobalScopeEntry, raw::RawType, tree::Type};
5-
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin, builders::make_cannot_find};
5+
use diagnostics::{
6+
DiagnosticResult, DiagnosticSpanOrigin, MaybeDiagnostic,
7+
builders::{make_already_in_scope, make_cannot_find},
8+
};
69

710
use crate::{
811
ctx::{HIRBranchedContext, HIRFunction, HIRFunctionImpl},
@@ -29,6 +32,18 @@ impl HIRGlobalScopeStorage {
2932
}
3033
}
3134

35+
pub fn enforce_not_here<K: DiagnosticSpanOrigin>(
36+
&mut self,
37+
name: EntryKey,
38+
origin: &K,
39+
) -> MaybeDiagnostic {
40+
if self.scope.entry_to_ind.contains_key(&name) {
41+
return Err(make_already_in_scope(origin, &name.name_hash).into());
42+
}
43+
44+
Ok(())
45+
}
46+
3247
/// This doesn't automatically handle descriptors and implementations
3348
pub fn append<K: DiagnosticSpanOrigin>(
3449
&mut self,

compiler/astoir_hir_lowering/src/func.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ pub fn lower_ast_function_declaration(
8888
let branch = curr_ctx.start_branch();
8989

9090
for arg in &arguments {
91+
context
92+
.global_scope
93+
.enforce_not_here(EntryKey { name_hash: arg.0 }, &*node)?;
94+
9195
match curr_ctx.introduce_variable(arg.0, arg.1.clone(), true) {
9296
Ok(_) => {}
9397
Err(_) => return Err(make_already_in_scope(&*node, &arg.0).into()),

compiler/astoir_hir_lowering/src/var.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use astoir_hir::{
33
ctx::{HIRBranchedContext, HIRContext, VariableKind, get_variable},
44
nodes::{HIRNode, HIRNodeKind},
55
};
6+
use compiler_global_scope::key::EntryKey;
67
use diagnostics::{DiagnosticResult, builders::make_variable_uninit};
78

89
use crate::{arrays::lower_ast_array_index_access, types::lower_ast_type, values::lower_ast_value};
@@ -18,6 +19,13 @@ pub fn lower_ast_variable_declaration(
1819
value,
1920
} = node.kind.clone()
2021
{
22+
context.global_scope.enforce_not_here(
23+
EntryKey {
24+
name_hash: var_name.hash,
25+
},
26+
&*node,
27+
)?;
28+
2129
let lowered = lower_ast_type(context, var_type, &*node)?;
2230

2331
let name_ind =

compiler/astoir_mir_lowering/src/vars.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ pub fn lower_hir_variable_declaration(
2828

2929
let fns_ind = match &ctx.hir_ctx.global_scope.scope.entries[func].entry_type {
3030
TypedGlobalScopeEntry::Function {
31-
descriptor_ind,
31+
descriptor_ind: _,
3232
impl_ind,
3333
} => impl_ind,
3434
TypedGlobalScopeEntry::StructFunction {
35-
descriptor_ind,
35+
descriptor_ind: _,
3636
impl_ind,
37-
struct_type,
37+
struct_type: _,
3838
} => impl_ind,
3939

4040
_ => {

0 commit comments

Comments
 (0)