[ty] Remove unnecessary FileScopeId to ScopeId conversion (#20481)

This commit is contained in:
Micha Reiser 2025-09-20 13:20:10 +02:00 committed by GitHub
parent eb354608d2
commit 3ffe56d19d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 21 deletions

View file

@ -26,10 +26,6 @@ pub struct ScopeId<'db> {
impl get_size2::GetSize for ScopeId<'_> {} impl get_size2::GetSize for ScopeId<'_> {}
impl<'db> ScopeId<'db> { impl<'db> ScopeId<'db> {
pub(crate) fn is_function_like(self, db: &'db dyn Db) -> bool {
self.node(db).scope_kind().is_function_like()
}
pub(crate) fn is_annotation(self, db: &'db dyn Db) -> bool { pub(crate) fn is_annotation(self, db: &'db dyn Db) -> bool {
self.node(db).scope_kind().is_annotation() self.node(db).scope_kind().is_annotation()
} }

View file

@ -9,7 +9,7 @@ use super::{
use crate::FxOrderMap; use crate::FxOrderMap;
use crate::module_resolver::KnownModule; use crate::module_resolver::KnownModule;
use crate::semantic_index::definition::{Definition, DefinitionState}; use crate::semantic_index::definition::{Definition, DefinitionState};
use crate::semantic_index::scope::NodeWithScopeKind; use crate::semantic_index::scope::{NodeWithScopeKind, Scope};
use crate::semantic_index::symbol::Symbol; use crate::semantic_index::symbol::Symbol;
use crate::semantic_index::{ use crate::semantic_index::{
DeclarationWithConstraint, SemanticIndex, attribute_declarations, attribute_scopes, DeclarationWithConstraint, SemanticIndex, attribute_declarations, attribute_scopes,
@ -2935,8 +2935,8 @@ impl<'db> ClassLiteral<'db> {
let class_map = use_def_map(db, class_body_scope); let class_map = use_def_map(db, class_body_scope);
let class_table = place_table(db, class_body_scope); let class_table = place_table(db, class_body_scope);
let is_valid_scope = |method_scope: ScopeId<'db>| { let is_valid_scope = |method_scope: &Scope| {
if let Some(method_def) = method_scope.node(db).as_function() { if let Some(method_def) = method_scope.node().as_function() {
let method_name = method_def.node(&module).name.as_str(); let method_name = method_def.node(&module).name.as_str();
if let Place::Type(Type::FunctionLiteral(method_type), _) = if let Place::Type(Type::FunctionLiteral(method_type), _) =
class_symbol(db, class_body_scope, method_name).place class_symbol(db, class_body_scope, method_name).place
@ -2954,7 +2954,7 @@ impl<'db> ClassLiteral<'db> {
for (attribute_declarations, method_scope_id) in for (attribute_declarations, method_scope_id) in
attribute_declarations(db, class_body_scope, &name) attribute_declarations(db, class_body_scope, &name)
{ {
let method_scope = method_scope_id.to_scope_id(db, file); let method_scope = index.scope(method_scope_id);
if !is_valid_scope(method_scope) { if !is_valid_scope(method_scope) {
continue; continue;
} }
@ -3010,14 +3010,13 @@ impl<'db> ClassLiteral<'db> {
for (attribute_assignments, method_scope_id) in for (attribute_assignments, method_scope_id) in
attribute_assignments(db, class_body_scope, &name) attribute_assignments(db, class_body_scope, &name)
{ {
let method_scope = method_scope_id.to_scope_id(db, file); let method_scope = index.scope(method_scope_id);
if !is_valid_scope(method_scope) { if !is_valid_scope(method_scope) {
continue; continue;
} }
// The attribute assignment inherits the reachability of the method which contains it // The attribute assignment inherits the reachability of the method which contains it
let is_method_reachable = if let Some(method_def) = method_scope.node(db).as_function() let is_method_reachable = if let Some(method_def) = method_scope.node().as_function() {
{
let method = index.expect_single_definition(method_def); let method = index.expect_single_definition(method_def);
let method_place = class_table let method_place = class_table
.symbol_id(&method_def.node(&module).name) .symbol_id(&method_def.node(&module).name)

View file

@ -191,10 +191,8 @@ impl ClassDisplay<'_> {
let mut name_parts = vec![]; let mut name_parts = vec![];
// Skips itself // Skips itself
for (ancestor_file_scope_id, ancestor_scope) in index.ancestor_scopes(file_scope_id).skip(1) for (_, ancestor_scope) in index.ancestor_scopes(file_scope_id).skip(1) {
{ let node = ancestor_scope.node();
let ancestor_scope_id = ancestor_file_scope_id.to_scope_id(self.db, file);
let node = ancestor_scope_id.node(self.db);
match ancestor_scope.kind() { match ancestor_scope.kind() {
ScopeKind::Class => { ScopeKind::Class => {

View file

@ -4858,7 +4858,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
let db = self.db(); let db = self.db();
let scope = self.scope(); let scope = self.scope();
let file_scope_id = scope.file_scope_id(db); let file_scope_id = scope.file_scope_id(db);
let current_file = self.file();
'names: for name in names { 'names: for name in names {
// Walk up parent scopes looking for a possible enclosing scope that may have a // Walk up parent scopes looking for a possible enclosing scope that may have a
// definition of this name visible to us. Note that we skip the scope containing the // definition of this name visible to us. Note that we skip the scope containing the
@ -4866,8 +4866,8 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
for (enclosing_scope_file_id, _) in self.index.ancestor_scopes(file_scope_id).skip(1) { for (enclosing_scope_file_id, _) in self.index.ancestor_scopes(file_scope_id).skip(1) {
// Class scopes are not visible to nested scopes, and `nonlocal` cannot refer to // Class scopes are not visible to nested scopes, and `nonlocal` cannot refer to
// globals, so check only function-like scopes. // globals, so check only function-like scopes.
let enclosing_scope_id = enclosing_scope_file_id.to_scope_id(db, current_file); let enclosing_scope = self.index.scope(enclosing_scope_file_id);
if !enclosing_scope_id.is_function_like(db) { if !enclosing_scope.kind().is_function_like() {
continue; continue;
} }
let enclosing_place_table = self.index.place_table(enclosing_scope_file_id); let enclosing_place_table = self.index.place_table(enclosing_scope_file_id);
@ -6348,7 +6348,6 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
} }
let place = PlaceAndQualifiers::from(local_scope_place).or_fall_back_to(db, || { let place = PlaceAndQualifiers::from(local_scope_place).or_fall_back_to(db, || {
let current_file = self.file();
let mut symbol_resolves_locally = false; let mut symbol_resolves_locally = false;
if let Some(symbol) = place_expr.as_symbol() { if let Some(symbol) = place_expr.as_symbol() {
if let Some(symbol_id) = place_table.symbol_id(symbol.name()) { if let Some(symbol_id) = place_table.symbol_id(symbol.name()) {
@ -6414,7 +6413,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
// check only function-like scopes. // check only function-like scopes.
// There is one exception to this rule: annotation scopes can see // There is one exception to this rule: annotation scopes can see
// names defined in an immediately-enclosing class scope. // names defined in an immediately-enclosing class scope.
let enclosing_scope_id = enclosing_scope_file_id.to_scope_id(db, current_file); let enclosing_scope = self.index.scope(enclosing_scope_file_id);
let is_immediately_enclosing_scope = scope.is_annotation(db) let is_immediately_enclosing_scope = scope.is_annotation(db)
&& scope && scope
@ -6481,7 +6480,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
} }
} }
if !enclosing_scope_id.is_function_like(db) && !is_immediately_enclosing_scope { if !enclosing_scope.kind().is_function_like() && !is_immediately_enclosing_scope {
continue; continue;
} }
@ -6502,6 +6501,8 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
break; break;
} }
let enclosing_scope_id = enclosing_scope_file_id.to_scope_id(db, self.file());
// If the name is declared or bound in this scope, figure out its type. This might // If the name is declared or bound in this scope, figure out its type. This might
// resolve the name and end the walk. But if the name is declared `nonlocal` in // resolve the name and end the walk. But if the name is declared `nonlocal` in
// this scope, we'll keep walking enclosing scopes and union this type with the // this scope, we'll keep walking enclosing scopes and union this type with the