Skip to content

Commit adaabb7

Browse files
committed
[ty] Have SemanticIndex::place_table() and SemanticIndex::use_def_map return references
1 parent 083bb85 commit adaabb7

File tree

3 files changed

+52
-58
lines changed

3 files changed

+52
-58
lines changed

crates/ty_python_semantic/src/semantic_index.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ pub(crate) fn place_table<'db>(db: &'db dyn Db, scope: ScopeId<'db>) -> Arc<Plac
7070
let file = scope.file(db);
7171
let _span = tracing::trace_span!("place_table", scope=?scope.as_id(), ?file).entered();
7272
let index = semantic_index(db, file);
73-
74-
index.place_table(scope.file_scope_id(db))
73+
Arc::clone(&index.place_tables[scope.file_scope_id(db)])
7574
}
7675

7776
/// Returns the set of modules that are imported anywhere in `file`.
@@ -100,8 +99,7 @@ pub(crate) fn use_def_map<'db>(db: &'db dyn Db, scope: ScopeId<'db>) -> Arc<UseD
10099
let file = scope.file(db);
101100
let _span = tracing::trace_span!("use_def_map", scope=?scope.as_id(), ?file).entered();
102101
let index = semantic_index(db, file);
103-
104-
index.use_def_map(scope.file_scope_id(db))
102+
Arc::clone(&index.use_def_maps[scope.file_scope_id(db)])
105103
}
106104

107105
/// Returns all attribute assignments (and their method scope IDs) with a symbol name matching
@@ -252,17 +250,17 @@ impl<'db> SemanticIndex<'db> {
252250
/// Use the Salsa cached [`place_table()`] query if you only need the
253251
/// place table for a single scope.
254252
#[track_caller]
255-
pub(super) fn place_table(&self, scope_id: FileScopeId) -> Arc<PlaceTable> {
256-
self.place_tables[scope_id].clone()
253+
pub(super) fn place_table(&self, scope_id: FileScopeId) -> &PlaceTable {
254+
&self.place_tables[scope_id]
257255
}
258256

259257
/// Returns the use-def map for a specific scope.
260258
///
261259
/// Use the Salsa cached [`use_def_map()`] query if you only need the
262260
/// use-def map for a single scope.
263261
#[track_caller]
264-
pub(super) fn use_def_map(&self, scope_id: FileScopeId) -> Arc<UseDefMap<'_>> {
265-
self.use_def_maps[scope_id].clone()
262+
pub(super) fn use_def_map(&self, scope_id: FileScopeId) -> &UseDefMap<'db> {
263+
&self.use_def_maps[scope_id]
266264
}
267265

268266
#[track_caller]
@@ -907,7 +905,7 @@ y = 2
907905
);
908906

909907
let class_table = index.place_table(class_scope_id);
910-
assert_eq!(names(&class_table), vec!["x"]);
908+
assert_eq!(names(class_table), vec!["x"]);
911909

912910
let use_def = index.use_def_map(class_scope_id);
913911
let binding = use_def
@@ -929,7 +927,7 @@ y = 2
929927
let index = semantic_index(&db, file);
930928
let global_table = index.place_table(FileScopeId::global());
931929

932-
assert_eq!(names(&global_table), vec!["func", "y"]);
930+
assert_eq!(names(global_table), vec!["func", "y"]);
933931

934932
let [(function_scope_id, function_scope)] = index
935933
.child_scopes(FileScopeId::global())
@@ -944,7 +942,7 @@ y = 2
944942
);
945943

946944
let function_table = index.place_table(function_scope_id);
947-
assert_eq!(names(&function_table), vec!["x"]);
945+
assert_eq!(names(function_table), vec!["x"]);
948946

949947
let use_def = index.use_def_map(function_scope_id);
950948
let binding = use_def
@@ -976,7 +974,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
976974

977975
let function_table = index.place_table(function_scope_id);
978976
assert_eq!(
979-
names(&function_table),
977+
names(function_table),
980978
vec!["a", "b", "c", "d", "args", "kwargs"],
981979
);
982980

@@ -1021,7 +1019,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
10211019

10221020
let lambda_table = index.place_table(lambda_scope_id);
10231021
assert_eq!(
1024-
names(&lambda_table),
1022+
names(lambda_table),
10251023
vec!["a", "b", "c", "d", "args", "kwargs"],
10261024
);
10271025

@@ -1062,7 +1060,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
10621060
let index = semantic_index(&db, file);
10631061
let global_table = index.place_table(FileScopeId::global());
10641062

1065-
assert_eq!(names(&global_table), vec!["iter1"]);
1063+
assert_eq!(names(global_table), vec!["iter1"]);
10661064

10671065
let [(comprehension_scope_id, comprehension_scope)] = index
10681066
.child_scopes(FileScopeId::global())
@@ -1081,7 +1079,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
10811079

10821080
let comprehension_symbol_table = index.place_table(comprehension_scope_id);
10831081

1084-
assert_eq!(names(&comprehension_symbol_table), vec!["x", "y"]);
1082+
assert_eq!(names(comprehension_symbol_table), vec!["x", "y"]);
10851083

10861084
let use_def = index.use_def_map(comprehension_scope_id);
10871085
for name in ["x", "y"] {
@@ -1159,7 +1157,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
11591157
let index = semantic_index(&db, file);
11601158
let global_table = index.place_table(FileScopeId::global());
11611159

1162-
assert_eq!(names(&global_table), vec!["iter1"]);
1160+
assert_eq!(names(global_table), vec!["iter1"]);
11631161

11641162
let [(comprehension_scope_id, comprehension_scope)] = index
11651163
.child_scopes(FileScopeId::global())
@@ -1178,7 +1176,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
11781176

11791177
let comprehension_symbol_table = index.place_table(comprehension_scope_id);
11801178

1181-
assert_eq!(names(&comprehension_symbol_table), vec!["y", "iter2"]);
1179+
assert_eq!(names(comprehension_symbol_table), vec!["y", "iter2"]);
11821180

11831181
let [(inner_comprehension_scope_id, inner_comprehension_scope)] = index
11841182
.child_scopes(comprehension_scope_id)
@@ -1197,7 +1195,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
11971195

11981196
let inner_comprehension_symbol_table = index.place_table(inner_comprehension_scope_id);
11991197

1200-
assert_eq!(names(&inner_comprehension_symbol_table), vec!["x"]);
1198+
assert_eq!(names(inner_comprehension_symbol_table), vec!["x"]);
12011199
}
12021200

12031201
#[test]
@@ -1212,7 +1210,7 @@ with item1 as x, item2 as y:
12121210
let index = semantic_index(&db, file);
12131211
let global_table = index.place_table(FileScopeId::global());
12141212

1215-
assert_eq!(names(&global_table), vec!["item1", "x", "item2", "y"]);
1213+
assert_eq!(names(global_table), vec!["item1", "x", "item2", "y"]);
12161214

12171215
let use_def = index.use_def_map(FileScopeId::global());
12181216
for name in ["x", "y"] {
@@ -1235,7 +1233,7 @@ with context() as (x, y):
12351233
let index = semantic_index(&db, file);
12361234
let global_table = index.place_table(FileScopeId::global());
12371235

1238-
assert_eq!(names(&global_table), vec!["context", "x", "y"]);
1236+
assert_eq!(names(global_table), vec!["context", "x", "y"]);
12391237

12401238
let use_def = index.use_def_map(FileScopeId::global());
12411239
for name in ["x", "y"] {
@@ -1260,7 +1258,7 @@ def func():
12601258
let index = semantic_index(&db, file);
12611259
let global_table = index.place_table(FileScopeId::global());
12621260

1263-
assert_eq!(names(&global_table), vec!["func"]);
1261+
assert_eq!(names(global_table), vec!["func"]);
12641262
let [
12651263
(func_scope1_id, func_scope_1),
12661264
(func_scope2_id, func_scope_2),
@@ -1285,8 +1283,8 @@ def func():
12851283

12861284
let func1_table = index.place_table(func_scope1_id);
12871285
let func2_table = index.place_table(func_scope2_id);
1288-
assert_eq!(names(&func1_table), vec!["x"]);
1289-
assert_eq!(names(&func2_table), vec!["y"]);
1286+
assert_eq!(names(func1_table), vec!["x"]);
1287+
assert_eq!(names(func2_table), vec!["y"]);
12901288

12911289
let use_def = index.use_def_map(FileScopeId::global());
12921290
let binding = use_def
@@ -1308,7 +1306,7 @@ def func[T]():
13081306
let index = semantic_index(&db, file);
13091307
let global_table = index.place_table(FileScopeId::global());
13101308

1311-
assert_eq!(names(&global_table), vec!["func"]);
1309+
assert_eq!(names(global_table), vec!["func"]);
13121310

13131311
let [(ann_scope_id, ann_scope)] = index
13141312
.child_scopes(FileScopeId::global())
@@ -1323,7 +1321,7 @@ def func[T]():
13231321
"func"
13241322
);
13251323
let ann_table = index.place_table(ann_scope_id);
1326-
assert_eq!(names(&ann_table), vec!["T"]);
1324+
assert_eq!(names(ann_table), vec!["T"]);
13271325

13281326
let [(func_scope_id, func_scope)] =
13291327
index.child_scopes(ann_scope_id).collect::<Vec<_>>()[..]
@@ -1336,7 +1334,7 @@ def func[T]():
13361334
"func"
13371335
);
13381336
let func_table = index.place_table(func_scope_id);
1339-
assert_eq!(names(&func_table), vec!["x"]);
1337+
assert_eq!(names(func_table), vec!["x"]);
13401338
}
13411339

13421340
#[test]
@@ -1352,7 +1350,7 @@ class C[T]:
13521350
let index = semantic_index(&db, file);
13531351
let global_table = index.place_table(FileScopeId::global());
13541352

1355-
assert_eq!(names(&global_table), vec!["C"]);
1353+
assert_eq!(names(global_table), vec!["C"]);
13561354

13571355
let [(ann_scope_id, ann_scope)] = index
13581356
.child_scopes(FileScopeId::global())
@@ -1364,7 +1362,7 @@ class C[T]:
13641362
assert_eq!(ann_scope.kind(), ScopeKind::TypeParams);
13651363
assert_eq!(ann_scope_id.to_scope_id(&db, file).name(&db, &module), "C");
13661364
let ann_table = index.place_table(ann_scope_id);
1367-
assert_eq!(names(&ann_table), vec!["T"]);
1365+
assert_eq!(names(ann_table), vec!["T"]);
13681366
assert!(
13691367
ann_table
13701368
.symbol_by_name("T")
@@ -1383,7 +1381,7 @@ class C[T]:
13831381
class_scope_id.to_scope_id(&db, file).name(&db, &module),
13841382
"C"
13851383
);
1386-
assert_eq!(names(&index.place_table(class_scope_id)), vec!["x"]);
1384+
assert_eq!(names(index.place_table(class_scope_id)), vec!["x"]);
13871385
}
13881386

13891387
#[test]

crates/ty_python_semantic/src/types/ide_support.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ impl<'db> AllMembers<'db> {
307307
let file = class_body_scope.file(db);
308308
let index = semantic_index(db, file);
309309
for function_scope_id in attribute_scopes(db, class_body_scope) {
310-
let place_table = index.place_table(function_scope_id);
311-
for place_expr in place_table.members() {
310+
for place_expr in index.place_table(function_scope_id).members() {
312311
let Some(name) = place_expr.as_instance_attribute() else {
313312
continue;
314313
};
@@ -411,8 +410,9 @@ pub fn definition_kind_for_name<'db>(
411410
let symbol_id = place_table.symbol_id(name_str)?;
412411

413412
// Get the use-def map and look up definitions for this place
414-
let use_def_map = index.use_def_map(file_scope);
415-
let declarations = use_def_map.all_reachable_symbol_declarations(symbol_id);
413+
let declarations = index
414+
.use_def_map(file_scope)
415+
.all_reachable_symbol_declarations(symbol_id);
416416

417417
// Find the first valid definition and return its kind
418418
for declaration in declarations {
@@ -662,9 +662,10 @@ pub fn definitions_for_attribute<'db>(
662662
let index = semantic_index(db, file);
663663

664664
for function_scope_id in attribute_scopes(db, class_scope) {
665-
let place_table = index.place_table(function_scope_id);
666-
667-
if let Some(place_id) = place_table.member_id_by_instance_attribute_name(name_str) {
665+
if let Some(place_id) = index
666+
.place_table(function_scope_id)
667+
.member_id_by_instance_attribute_name(name_str)
668+
{
668669
let use_def = index.use_def_map(function_scope_id);
669670

670671
// Check declarations first

crates/ty_python_semantic/src/types/infer.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,7 +1848,6 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
18481848
let mut bound_ty = ty;
18491849

18501850
let global_use_def_map = self.index.use_def_map(FileScopeId::global());
1851-
let nonlocal_use_def_map;
18521851
let place_id = binding.place(self.db());
18531852
let place = place_table.place(place_id);
18541853

@@ -1908,9 +1907,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
19081907
}
19091908
// We found the closest definition. Note that (as in `infer_place_load`) this does
19101909
// *not* need to be a binding. It could be just a declaration, e.g. `x: int`.
1911-
nonlocal_use_def_map = self.index.use_def_map(enclosing_scope_file_id);
1912-
declarations =
1913-
nonlocal_use_def_map.end_of_scope_symbol_declarations(enclosing_symbol_id);
1910+
declarations = self
1911+
.index
1912+
.use_def_map(enclosing_scope_file_id)
1913+
.end_of_scope_symbol_declarations(enclosing_symbol_id);
19141914
is_local = false;
19151915
break;
19161916
}
@@ -2107,8 +2107,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
21072107
.or_fall_back_to(self.db(), || {
21082108
// Fallback to bindings declared on `types.ModuleType` if it's a global symbol
21092109
let scope = self.scope().file_scope_id(self.db());
2110-
let place_table = self.index.place_table(scope);
2111-
let place = place_table.place(declaration.place(self.db()));
2110+
let place = self
2111+
.index
2112+
.place_table(scope)
2113+
.place(declaration.place(self.db()));
21122114
if let PlaceExprRef::Symbol(symbol) = &place {
21132115
if scope.is_global() {
21142116
module_type_implicit_global_symbol(self.db(), symbol.name())
@@ -2501,8 +2503,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
25012503
invalid.ty,
25022504
);
25032505
}
2504-
let use_def = self.index.use_def_map(scope_id);
2505-
if use_def.can_implicitly_return_none(self.db())
2506+
if self
2507+
.index
2508+
.use_def_map(scope_id)
2509+
.can_implicitly_return_none(self.db())
25062510
&& !Type::none(self.db()).is_assignable_to(self.db(), expected_ty)
25072511
{
25082512
let no_return = self.return_types_and_ranges.is_empty();
@@ -5169,20 +5173,11 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
51695173

51705174
let module_ty = Type::module_literal(self.db(), self.file(), module);
51715175

5172-
// The indirection of having `star_import_info` as a separate variable
5173-
// is required in order to make the borrow checker happy.
5174-
let star_import_info = definition
5175-
.kind(self.db())
5176-
.as_star_import()
5177-
.map(|star_import| {
5178-
let place_table = self
5179-
.index
5180-
.place_table(self.scope().file_scope_id(self.db()));
5181-
(star_import, place_table)
5182-
});
5183-
5184-
let name = if let Some((star_import, symbol_table)) = star_import_info.as_ref() {
5185-
symbol_table.symbol(star_import.symbol_id()).name()
5176+
let name = if let Some(star_import) = definition.kind(self.db()).as_star_import() {
5177+
self.index
5178+
.place_table(self.scope().file_scope_id(self.db()))
5179+
.symbol(star_import.symbol_id())
5180+
.name()
51865181
} else {
51875182
&alias.name.id
51885183
};

0 commit comments

Comments
 (0)