Skip to content

Commit

Permalink
refactor: remove find_with_depth method from Scope (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
junlarsen authored Feb 15, 2025
1 parent 6e6519a commit 982c991
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl HirModuleTypeCheckerPass {
ice!("visit_nominal_ty called with non-nominal type");
};
// Check if the name refers to a generic type parameter, like `T`.
if let Some(sub) = cx.find_type_binding(n.name) {
if let Some((sub, _)) = cx.find_type_binding(n.name) {
return Ok(sub);
}
// Check if the name refers to a struct or a type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct TypingContext<'hir> {
///
/// We currently don't support nested functions or lambdas, so this does not necessarily have to
/// be a VecDeque, but it's here for future use.
type_binding_context: Scope<&'hir str, &'hir HirTy<'hir>>,
type_binding_context: Scope<&'hir str, (&'hir HirTy<'hir>, Span)>,
let_binding_context: Scope<&'hir str, (&'hir HirTy<'hir>, Span)>,
// TODO: make private
pub type_parameter_instantiations: Scope<(u32, u32), &'hir HirTy<'hir>>,
Expand Down Expand Up @@ -120,17 +120,19 @@ impl<'hir> TypingContext<'hir> {
ty: &'hir HirTy<'hir>,
) -> HirResult<()> {
let current_depth = self.type_binding_context.depth();
if let Some((depth, _)) = self.type_binding_context.find_with_depth(&name) {
if depth >= current_depth {
return Err(HirError::TypeParameterShadowsExisting(
TypeParameterShadowsExisting {
name: name.to_owned(),
span,
},
));
}
if self
.type_binding_context
.find_within_depth(&name, current_depth)
.is_some()
{
return Err(HirError::TypeParameterShadowsExisting(
TypeParameterShadowsExisting {
name: name.to_owned(),
span,
},
));
}
self.type_binding_context.add(name, ty);
self.type_binding_context.add(name, (ty, span));
Ok(())
}

Expand Down Expand Up @@ -167,7 +169,7 @@ impl<'hir> TypingContext<'hir> {
}

/// Find the type of a type binding.
pub fn find_type_binding(&self, name: &'hir str) -> Option<&'hir HirTy<'hir>> {
pub fn find_type_binding(&self, name: &'hir str) -> Option<(&'hir HirTy<'hir>, Span)> {
self.type_binding_context.find(&name).copied()
}

Expand Down
18 changes: 0 additions & 18 deletions compiler/eight-middle/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ impl<K: Ord, V> Scope<K, V> {
None
}

/// Find an item in the context, and return the distance from the root scope.
pub fn find_with_depth(&self, name: &K) -> Option<(usize, &V)> {
for (depth, scope) in self.scopes.iter().enumerate() {
if let Some(item) = scope.get(name) {
return Some((depth, item));
}
}
None
}

pub fn add(&mut self, name: K, id: V) {
let scope = self
.scopes
Expand All @@ -79,14 +69,6 @@ impl<K: Ord, V> Scope<K, V> {
.unwrap_or_else(|| ice!("local context has no scope"));
scope.remove(name);
}

pub fn local_size(&self) -> usize {
self.scopes.front().map(|s| s.len()).unwrap_or(0)
}

pub fn find_local(&self, name: &K) -> Option<&V> {
self.scopes.front().and_then(|s| s.get(name))
}
}

#[cfg(test)]
Expand Down

0 comments on commit 982c991

Please sign in to comment.