Add dedicated method to find typed binding (#8517)

## Summary

We have this pattern in a bunch of places, where we find the _only_
binding to a name (and return `None`) if it's bound multiple times. This
PR DRYs it up into a method on `SemanticModel`.
This commit is contained in:
Charlie Marsh 2023-11-06 08:25:32 -08:00 committed by GitHub
parent 5b3e922050
commit eab8ca4d7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 82 additions and 79 deletions

View file

@ -616,6 +616,16 @@ impl<'a> SemanticModel<'a> {
self.resolved_names.get(&name.into()).copied()
}
/// Resolves the [`ast::ExprName`] to the [`BindingId`] of the symbol it refers to, if it's the
/// only binding to that name in its scope.
pub fn only_binding(&self, name: &ast::ExprName) -> Option<BindingId> {
self.resolve_name(name).filter(|id| {
let binding = self.binding(*id);
let scope = &self.scopes[binding.scope];
scope.shadowed_binding(*id).is_none()
})
}
/// Resolves the [`Expr`] to a fully-qualified symbol-name, if `value` resolves to an imported
/// or builtin symbol.
///