Skip to content

Commit 3ffe56d

Browse files
authored
[ty] Remove unnecessary FileScopeId to ScopeId conversion (#20481)
1 parent eb35460 commit 3ffe56d

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

crates/ty_python_semantic/src/semantic_index/scope.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ pub struct ScopeId<'db> {
2626
impl get_size2::GetSize for ScopeId<'_> {}
2727

2828
impl<'db> ScopeId<'db> {
29-
pub(crate) fn is_function_like(self, db: &'db dyn Db) -> bool {
30-
self.node(db).scope_kind().is_function_like()
31-
}
32-
3329
pub(crate) fn is_annotation(self, db: &'db dyn Db) -> bool {
3430
self.node(db).scope_kind().is_annotation()
3531
}

crates/ty_python_semantic/src/types/class.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{
99
use crate::FxOrderMap;
1010
use crate::module_resolver::KnownModule;
1111
use crate::semantic_index::definition::{Definition, DefinitionState};
12-
use crate::semantic_index::scope::NodeWithScopeKind;
12+
use crate::semantic_index::scope::{NodeWithScopeKind, Scope};
1313
use crate::semantic_index::symbol::Symbol;
1414
use crate::semantic_index::{
1515
DeclarationWithConstraint, SemanticIndex, attribute_declarations, attribute_scopes,
@@ -2935,8 +2935,8 @@ impl<'db> ClassLiteral<'db> {
29352935
let class_map = use_def_map(db, class_body_scope);
29362936
let class_table = place_table(db, class_body_scope);
29372937

2938-
let is_valid_scope = |method_scope: ScopeId<'db>| {
2939-
if let Some(method_def) = method_scope.node(db).as_function() {
2938+
let is_valid_scope = |method_scope: &Scope| {
2939+
if let Some(method_def) = method_scope.node().as_function() {
29402940
let method_name = method_def.node(&module).name.as_str();
29412941
if let Place::Type(Type::FunctionLiteral(method_type), _) =
29422942
class_symbol(db, class_body_scope, method_name).place
@@ -2954,7 +2954,7 @@ impl<'db> ClassLiteral<'db> {
29542954
for (attribute_declarations, method_scope_id) in
29552955
attribute_declarations(db, class_body_scope, &name)
29562956
{
2957-
let method_scope = method_scope_id.to_scope_id(db, file);
2957+
let method_scope = index.scope(method_scope_id);
29582958
if !is_valid_scope(method_scope) {
29592959
continue;
29602960
}
@@ -3010,14 +3010,13 @@ impl<'db> ClassLiteral<'db> {
30103010
for (attribute_assignments, method_scope_id) in
30113011
attribute_assignments(db, class_body_scope, &name)
30123012
{
3013-
let method_scope = method_scope_id.to_scope_id(db, file);
3013+
let method_scope = index.scope(method_scope_id);
30143014
if !is_valid_scope(method_scope) {
30153015
continue;
30163016
}
30173017

30183018
// The attribute assignment inherits the reachability of the method which contains it
3019-
let is_method_reachable = if let Some(method_def) = method_scope.node(db).as_function()
3020-
{
3019+
let is_method_reachable = if let Some(method_def) = method_scope.node().as_function() {
30213020
let method = index.expect_single_definition(method_def);
30223021
let method_place = class_table
30233022
.symbol_id(&method_def.node(&module).name)

crates/ty_python_semantic/src/types/display.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,8 @@ impl ClassDisplay<'_> {
191191
let mut name_parts = vec![];
192192

193193
// Skips itself
194-
for (ancestor_file_scope_id, ancestor_scope) in index.ancestor_scopes(file_scope_id).skip(1)
195-
{
196-
let ancestor_scope_id = ancestor_file_scope_id.to_scope_id(self.db, file);
197-
let node = ancestor_scope_id.node(self.db);
194+
for (_, ancestor_scope) in index.ancestor_scopes(file_scope_id).skip(1) {
195+
let node = ancestor_scope.node();
198196

199197
match ancestor_scope.kind() {
200198
ScopeKind::Class => {

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4858,16 +4858,16 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
48584858
let db = self.db();
48594859
let scope = self.scope();
48604860
let file_scope_id = scope.file_scope_id(db);
4861-
let current_file = self.file();
4861+
48624862
'names: for name in names {
48634863
// Walk up parent scopes looking for a possible enclosing scope that may have a
48644864
// definition of this name visible to us. Note that we skip the scope containing the
48654865
// use that we are resolving, since we already looked for the place there up above.
48664866
for (enclosing_scope_file_id, _) in self.index.ancestor_scopes(file_scope_id).skip(1) {
48674867
// Class scopes are not visible to nested scopes, and `nonlocal` cannot refer to
48684868
// globals, so check only function-like scopes.
4869-
let enclosing_scope_id = enclosing_scope_file_id.to_scope_id(db, current_file);
4870-
if !enclosing_scope_id.is_function_like(db) {
4869+
let enclosing_scope = self.index.scope(enclosing_scope_file_id);
4870+
if !enclosing_scope.kind().is_function_like() {
48714871
continue;
48724872
}
48734873
let enclosing_place_table = self.index.place_table(enclosing_scope_file_id);
@@ -6348,7 +6348,6 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
63486348
}
63496349

63506350
let place = PlaceAndQualifiers::from(local_scope_place).or_fall_back_to(db, || {
6351-
let current_file = self.file();
63526351
let mut symbol_resolves_locally = false;
63536352
if let Some(symbol) = place_expr.as_symbol() {
63546353
if let Some(symbol_id) = place_table.symbol_id(symbol.name()) {
@@ -6414,7 +6413,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
64146413
// check only function-like scopes.
64156414
// There is one exception to this rule: annotation scopes can see
64166415
// names defined in an immediately-enclosing class scope.
6417-
let enclosing_scope_id = enclosing_scope_file_id.to_scope_id(db, current_file);
6416+
let enclosing_scope = self.index.scope(enclosing_scope_file_id);
64186417

64196418
let is_immediately_enclosing_scope = scope.is_annotation(db)
64206419
&& scope
@@ -6481,7 +6480,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
64816480
}
64826481
}
64836482

6484-
if !enclosing_scope_id.is_function_like(db) && !is_immediately_enclosing_scope {
6483+
if !enclosing_scope.kind().is_function_like() && !is_immediately_enclosing_scope {
64856484
continue;
64866485
}
64876486

@@ -6502,6 +6501,8 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
65026501
break;
65036502
}
65046503

6504+
let enclosing_scope_id = enclosing_scope_file_id.to_scope_id(db, self.file());
6505+
65056506
// If the name is declared or bound in this scope, figure out its type. This might
65066507
// resolve the name and end the walk. But if the name is declared `nonlocal` in
65076508
// this scope, we'll keep walking enclosing scopes and union this type with the

0 commit comments

Comments
 (0)