Use BindingId copies in lieu of &BindingId in semantic model methods (#4633)

This commit is contained in:
Charlie Marsh 2023-05-24 11:55:45 -04:00 committed by GitHub
parent f4f1b1d0ee
commit f0e173d9fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 45 additions and 51 deletions

View file

@ -110,7 +110,7 @@ impl<'a> SemanticModel<'a> {
pub fn find_binding(&self, member: &str) -> Option<&Binding> {
self.scopes()
.find_map(|scope| scope.get(member))
.map(|binding_id| &self.bindings[*binding_id])
.map(|binding_id| &self.bindings[binding_id])
}
/// Return `true` if `member` is bound as a builtin.
@ -124,7 +124,7 @@ impl<'a> SemanticModel<'a> {
// PEP 563 indicates that if a forward reference can be resolved in the module scope, we
// should prefer it over local resolutions.
if self.in_deferred_type_definition() {
if let Some(binding_id) = self.scopes.global().get(symbol).copied() {
if let Some(binding_id) = self.scopes.global().get(symbol) {
// Mark the binding as used.
let context = self.execution_context();
let reference_id = self.references.push(ScopeId::global(), range, context);
@ -160,7 +160,7 @@ impl<'a> SemanticModel<'a> {
}
}
if let Some(binding_id) = scope.get(symbol).copied() {
if let Some(binding_id) = scope.get(symbol) {
// Mark the binding as used.
let context = self.execution_context();
let reference_id = self.references.push(self.scope_id, range, context);
@ -255,7 +255,7 @@ impl<'a> SemanticModel<'a> {
return None;
}
self.scopes[scope_id].get(full_name).copied()
self.scopes[scope_id].get(full_name)
}
/// Resolves the [`Expr`] to a fully-qualified symbol-name, if `value` resolves to an imported
@ -352,7 +352,7 @@ impl<'a> SemanticModel<'a> {
member: &str,
) -> Option<(&Stmt, String)> {
self.scopes().enumerate().find_map(|(scope_index, scope)| {
scope.binding_ids().copied().find_map(|binding_id| {
scope.binding_ids().find_map(|binding_id| {
let binding = &self.bindings[binding_id];
match &binding.kind {
// Ex) Given `module="sys"` and `object="exit"`:

View file

@ -24,8 +24,8 @@ impl Reference {
self.range
}
pub const fn context(&self) -> &ExecutionContext {
&self.context
pub const fn context(&self) -> ExecutionContext {
self.context
}
}

View file

@ -45,8 +45,8 @@ impl<'a> Scope<'a> {
}
/// Returns the [id](BindingId) of the binding bound to the given name.
pub fn get(&self, name: &str) -> Option<&BindingId> {
self.bindings.get(name)
pub fn get(&self, name: &str) -> Option<BindingId> {
self.bindings.get(name).copied()
}
/// Adds a new binding with the given name to this scope.
@ -70,22 +70,23 @@ impl<'a> Scope<'a> {
}
/// Returns the ids of all bindings defined in this scope.
pub fn binding_ids(&self) -> std::collections::hash_map::Values<&str, BindingId> {
self.bindings.values()
pub fn binding_ids(&self) -> impl Iterator<Item = BindingId> + '_ {
self.bindings.values().copied()
}
/// Returns a tuple of the name and id of all bindings defined in this scope.
pub fn bindings(&self) -> std::collections::hash_map::Iter<&'a str, BindingId> {
self.bindings.iter()
pub fn bindings(&self) -> impl Iterator<Item = (&str, BindingId)> + '_ {
self.bindings.iter().map(|(&name, &id)| (name, id))
}
/// Returns an iterator over all [bindings](BindingId) bound to the given name, including
/// those that were shadowed by later bindings.
pub fn bindings_for_name(&self, name: &str) -> impl Iterator<Item = &BindingId> {
pub fn bindings_for_name(&self, name: &str) -> impl Iterator<Item = BindingId> + '_ {
self.bindings
.get(name)
.into_iter()
.chain(self.shadowed_bindings.get(name).into_iter().flatten().rev())
.copied()
}
/// Adds a reference to a star import (e.g., `from sys import *`) to this scope.