Skip to content

Commit 2b24d92

Browse files
committed
[ty] Have SemanticIndex::place_table() and SemanticIndex::use_def_map return references
1 parent 81bccb3 commit 2b24d92

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_table(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_map(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) -> &Arc<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) -> &Arc<UseDefMap<'_>> {
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
@@ -306,8 +306,7 @@ impl<'db> AllMembers<'db> {
306306
let file = class_body_scope.file(db);
307307
let index = semantic_index(db, file);
308308
for function_scope_id in attribute_scopes(db, class_body_scope) {
309-
let place_table = index.place_table(function_scope_id);
310-
for place_expr in place_table.members() {
309+
for place_expr in index.place_table(function_scope_id).members() {
311310
let Some(name) = place_expr.as_instance_attribute() else {
312311
continue;
313312
};
@@ -410,8 +409,9 @@ pub fn definition_kind_for_name<'db>(
410409
let symbol_id = place_table.symbol_id(name_str)?;
411410

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

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

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

669670
// 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
@@ -1869,7 +1869,6 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
18691869
let mut bound_ty = ty;
18701870

18711871
let global_use_def_map = self.index.use_def_map(FileScopeId::global());
1872-
let nonlocal_use_def_map;
18731872
let place_id = binding.place(self.db());
18741873
let place = place_table.place(place_id);
18751874

@@ -1929,9 +1928,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
19291928
}
19301929
// We found the closest definition. Note that (as in `infer_place_load`) this does
19311930
// *not* need to be a binding. It could be just a declaration, e.g. `x: int`.
1932-
nonlocal_use_def_map = self.index.use_def_map(enclosing_scope_file_id);
1933-
declarations =
1934-
nonlocal_use_def_map.end_of_scope_symbol_declarations(enclosing_symbol_id);
1931+
declarations = self
1932+
.index
1933+
.use_def_map(enclosing_scope_file_id)
1934+
.end_of_scope_symbol_declarations(enclosing_symbol_id);
19351935
is_local = false;
19361936
break;
19371937
}
@@ -2128,8 +2128,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
21282128
.or_fall_back_to(self.db(), || {
21292129
// Fallback to bindings declared on `types.ModuleType` if it's a global symbol
21302130
let scope = self.scope().file_scope_id(self.db());
2131-
let place_table = self.index.place_table(scope);
2132-
let place = place_table.place(declaration.place(self.db()));
2131+
let place = self
2132+
.index
2133+
.place_table(scope)
2134+
.place(declaration.place(self.db()));
21332135
if let PlaceExprRef::Symbol(symbol) = &place {
21342136
if scope.is_global() {
21352137
module_type_implicit_global_symbol(self.db(), symbol.name())
@@ -2522,8 +2524,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
25222524
invalid.ty,
25232525
);
25242526
}
2525-
let use_def = self.index.use_def_map(scope_id);
2526-
if use_def.can_implicitly_return_none(self.db())
2527+
if self
2528+
.index
2529+
.use_def_map(scope_id)
2530+
.can_implicitly_return_none(self.db())
25272531
&& !Type::none(self.db()).is_assignable_to(self.db(), expected_ty)
25282532
{
25292533
let no_return = self.return_types_and_ranges.is_empty();
@@ -5189,20 +5193,11 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
51895193

51905194
let module_ty = Type::module_literal(self.db(), self.file(), module);
51915195

5192-
// The indirection of having `star_import_info` as a separate variable
5193-
// is required in order to make the borrow checker happy.
5194-
let star_import_info = definition
5195-
.kind(self.db())
5196-
.as_star_import()
5197-
.map(|star_import| {
5198-
let place_table = self
5199-
.index
5200-
.place_table(self.scope().file_scope_id(self.db()));
5201-
(star_import, place_table)
5202-
});
5203-
5204-
let name = if let Some((star_import, symbol_table)) = star_import_info.as_ref() {
5205-
symbol_table.symbol(star_import.symbol_id()).name()
5196+
let name = if let Some(star_import) = definition.kind(self.db()).as_star_import() {
5197+
self.index
5198+
.place_table(self.scope().file_scope_id(self.db()))
5199+
.symbol(star_import.symbol_id())
5200+
.name()
52065201
} else {
52075202
&alias.name.id
52085203
};

0 commit comments

Comments
 (0)