mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-10 05:39:12 +00:00
Invert parent-shadowed bindings map (#4847)
This commit is contained in:
parent
3fa4440d87
commit
466719247b
2 changed files with 57 additions and 61 deletions
|
@ -47,7 +47,7 @@ pub struct SemanticModel<'a> {
|
|||
// Arena of global bindings.
|
||||
globals: GlobalsArena<'a>,
|
||||
// Map from binding index to indexes of bindings that shadow it in other scopes.
|
||||
pub shadowed_bindings: HashMap<BindingId, Vec<BindingId>, BuildNoHashHasher<BindingId>>,
|
||||
pub shadowed_bindings: HashMap<BindingId, BindingId, BuildNoHashHasher<BindingId>>,
|
||||
// Body iteration; used to peek at siblings.
|
||||
pub body: &'a [Stmt],
|
||||
pub body_index: usize,
|
||||
|
@ -145,13 +145,23 @@ impl<'a> SemanticModel<'a> {
|
|||
})
|
||||
}
|
||||
|
||||
/// Return the current `Binding` for a given `name`.
|
||||
/// Return the current [`Binding`] for a given `name`.
|
||||
pub fn find_binding(&self, member: &str) -> Option<&Binding> {
|
||||
self.scopes()
|
||||
.find_map(|scope| scope.get(member))
|
||||
.map(|binding_id| &self.bindings[binding_id])
|
||||
}
|
||||
|
||||
/// Return the [`Binding`] that the given [`BindingId`] shadows, if any.
|
||||
///
|
||||
/// Note that this will only return bindings that are shadowed by a binding in a parent scope.
|
||||
pub fn shadowed_binding(&self, binding_id: BindingId) -> Option<&Binding> {
|
||||
self.shadowed_bindings
|
||||
.get(&binding_id)
|
||||
.copied()
|
||||
.map(|id| &self.bindings[id])
|
||||
}
|
||||
|
||||
/// Return `true` if `member` is bound as a builtin.
|
||||
pub fn is_builtin(&self, member: &str) -> bool {
|
||||
self.find_binding(member)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue