mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-03 13:23:10 +00:00
[ty] Have SemanticIndex::place_table() and SemanticIndex::use_def_map return references (#19944)
This commit is contained in:
parent
083bb85d9d
commit
4ac2b2c222
3 changed files with 52 additions and 58 deletions
|
|
@ -70,8 +70,7 @@ pub(crate) fn place_table<'db>(db: &'db dyn Db, scope: ScopeId<'db>) -> Arc<Plac
|
|||
let file = scope.file(db);
|
||||
let _span = tracing::trace_span!("place_table", scope=?scope.as_id(), ?file).entered();
|
||||
let index = semantic_index(db, file);
|
||||
|
||||
index.place_table(scope.file_scope_id(db))
|
||||
Arc::clone(&index.place_tables[scope.file_scope_id(db)])
|
||||
}
|
||||
|
||||
/// 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
|
|||
let file = scope.file(db);
|
||||
let _span = tracing::trace_span!("use_def_map", scope=?scope.as_id(), ?file).entered();
|
||||
let index = semantic_index(db, file);
|
||||
|
||||
index.use_def_map(scope.file_scope_id(db))
|
||||
Arc::clone(&index.use_def_maps[scope.file_scope_id(db)])
|
||||
}
|
||||
|
||||
/// Returns all attribute assignments (and their method scope IDs) with a symbol name matching
|
||||
|
|
@ -252,8 +250,8 @@ impl<'db> SemanticIndex<'db> {
|
|||
/// Use the Salsa cached [`place_table()`] query if you only need the
|
||||
/// place table for a single scope.
|
||||
#[track_caller]
|
||||
pub(super) fn place_table(&self, scope_id: FileScopeId) -> Arc<PlaceTable> {
|
||||
self.place_tables[scope_id].clone()
|
||||
pub(super) fn place_table(&self, scope_id: FileScopeId) -> &PlaceTable {
|
||||
&self.place_tables[scope_id]
|
||||
}
|
||||
|
||||
/// Returns the use-def map for a specific scope.
|
||||
|
|
@ -261,8 +259,8 @@ impl<'db> SemanticIndex<'db> {
|
|||
/// Use the Salsa cached [`use_def_map()`] query if you only need the
|
||||
/// use-def map for a single scope.
|
||||
#[track_caller]
|
||||
pub(super) fn use_def_map(&self, scope_id: FileScopeId) -> Arc<UseDefMap<'_>> {
|
||||
self.use_def_maps[scope_id].clone()
|
||||
pub(super) fn use_def_map(&self, scope_id: FileScopeId) -> &UseDefMap<'db> {
|
||||
&self.use_def_maps[scope_id]
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
|
|
@ -907,7 +905,7 @@ y = 2
|
|||
);
|
||||
|
||||
let class_table = index.place_table(class_scope_id);
|
||||
assert_eq!(names(&class_table), vec!["x"]);
|
||||
assert_eq!(names(class_table), vec!["x"]);
|
||||
|
||||
let use_def = index.use_def_map(class_scope_id);
|
||||
let binding = use_def
|
||||
|
|
@ -929,7 +927,7 @@ y = 2
|
|||
let index = semantic_index(&db, file);
|
||||
let global_table = index.place_table(FileScopeId::global());
|
||||
|
||||
assert_eq!(names(&global_table), vec!["func", "y"]);
|
||||
assert_eq!(names(global_table), vec!["func", "y"]);
|
||||
|
||||
let [(function_scope_id, function_scope)] = index
|
||||
.child_scopes(FileScopeId::global())
|
||||
|
|
@ -944,7 +942,7 @@ y = 2
|
|||
);
|
||||
|
||||
let function_table = index.place_table(function_scope_id);
|
||||
assert_eq!(names(&function_table), vec!["x"]);
|
||||
assert_eq!(names(function_table), vec!["x"]);
|
||||
|
||||
let use_def = index.use_def_map(function_scope_id);
|
||||
let binding = use_def
|
||||
|
|
@ -976,7 +974,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
|
|||
|
||||
let function_table = index.place_table(function_scope_id);
|
||||
assert_eq!(
|
||||
names(&function_table),
|
||||
names(function_table),
|
||||
vec!["a", "b", "c", "d", "args", "kwargs"],
|
||||
);
|
||||
|
||||
|
|
@ -1021,7 +1019,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
|
|||
|
||||
let lambda_table = index.place_table(lambda_scope_id);
|
||||
assert_eq!(
|
||||
names(&lambda_table),
|
||||
names(lambda_table),
|
||||
vec!["a", "b", "c", "d", "args", "kwargs"],
|
||||
);
|
||||
|
||||
|
|
@ -1062,7 +1060,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
|
|||
let index = semantic_index(&db, file);
|
||||
let global_table = index.place_table(FileScopeId::global());
|
||||
|
||||
assert_eq!(names(&global_table), vec!["iter1"]);
|
||||
assert_eq!(names(global_table), vec!["iter1"]);
|
||||
|
||||
let [(comprehension_scope_id, comprehension_scope)] = index
|
||||
.child_scopes(FileScopeId::global())
|
||||
|
|
@ -1081,7 +1079,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
|
|||
|
||||
let comprehension_symbol_table = index.place_table(comprehension_scope_id);
|
||||
|
||||
assert_eq!(names(&comprehension_symbol_table), vec!["x", "y"]);
|
||||
assert_eq!(names(comprehension_symbol_table), vec!["x", "y"]);
|
||||
|
||||
let use_def = index.use_def_map(comprehension_scope_id);
|
||||
for name in ["x", "y"] {
|
||||
|
|
@ -1159,7 +1157,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
|
|||
let index = semantic_index(&db, file);
|
||||
let global_table = index.place_table(FileScopeId::global());
|
||||
|
||||
assert_eq!(names(&global_table), vec!["iter1"]);
|
||||
assert_eq!(names(global_table), vec!["iter1"]);
|
||||
|
||||
let [(comprehension_scope_id, comprehension_scope)] = index
|
||||
.child_scopes(FileScopeId::global())
|
||||
|
|
@ -1178,7 +1176,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
|
|||
|
||||
let comprehension_symbol_table = index.place_table(comprehension_scope_id);
|
||||
|
||||
assert_eq!(names(&comprehension_symbol_table), vec!["y", "iter2"]);
|
||||
assert_eq!(names(comprehension_symbol_table), vec!["y", "iter2"]);
|
||||
|
||||
let [(inner_comprehension_scope_id, inner_comprehension_scope)] = index
|
||||
.child_scopes(comprehension_scope_id)
|
||||
|
|
@ -1197,7 +1195,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
|
|||
|
||||
let inner_comprehension_symbol_table = index.place_table(inner_comprehension_scope_id);
|
||||
|
||||
assert_eq!(names(&inner_comprehension_symbol_table), vec!["x"]);
|
||||
assert_eq!(names(inner_comprehension_symbol_table), vec!["x"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -1212,7 +1210,7 @@ with item1 as x, item2 as y:
|
|||
let index = semantic_index(&db, file);
|
||||
let global_table = index.place_table(FileScopeId::global());
|
||||
|
||||
assert_eq!(names(&global_table), vec!["item1", "x", "item2", "y"]);
|
||||
assert_eq!(names(global_table), vec!["item1", "x", "item2", "y"]);
|
||||
|
||||
let use_def = index.use_def_map(FileScopeId::global());
|
||||
for name in ["x", "y"] {
|
||||
|
|
@ -1235,7 +1233,7 @@ with context() as (x, y):
|
|||
let index = semantic_index(&db, file);
|
||||
let global_table = index.place_table(FileScopeId::global());
|
||||
|
||||
assert_eq!(names(&global_table), vec!["context", "x", "y"]);
|
||||
assert_eq!(names(global_table), vec!["context", "x", "y"]);
|
||||
|
||||
let use_def = index.use_def_map(FileScopeId::global());
|
||||
for name in ["x", "y"] {
|
||||
|
|
@ -1260,7 +1258,7 @@ def func():
|
|||
let index = semantic_index(&db, file);
|
||||
let global_table = index.place_table(FileScopeId::global());
|
||||
|
||||
assert_eq!(names(&global_table), vec!["func"]);
|
||||
assert_eq!(names(global_table), vec!["func"]);
|
||||
let [
|
||||
(func_scope1_id, func_scope_1),
|
||||
(func_scope2_id, func_scope_2),
|
||||
|
|
@ -1285,8 +1283,8 @@ def func():
|
|||
|
||||
let func1_table = index.place_table(func_scope1_id);
|
||||
let func2_table = index.place_table(func_scope2_id);
|
||||
assert_eq!(names(&func1_table), vec!["x"]);
|
||||
assert_eq!(names(&func2_table), vec!["y"]);
|
||||
assert_eq!(names(func1_table), vec!["x"]);
|
||||
assert_eq!(names(func2_table), vec!["y"]);
|
||||
|
||||
let use_def = index.use_def_map(FileScopeId::global());
|
||||
let binding = use_def
|
||||
|
|
@ -1308,7 +1306,7 @@ def func[T]():
|
|||
let index = semantic_index(&db, file);
|
||||
let global_table = index.place_table(FileScopeId::global());
|
||||
|
||||
assert_eq!(names(&global_table), vec!["func"]);
|
||||
assert_eq!(names(global_table), vec!["func"]);
|
||||
|
||||
let [(ann_scope_id, ann_scope)] = index
|
||||
.child_scopes(FileScopeId::global())
|
||||
|
|
@ -1323,7 +1321,7 @@ def func[T]():
|
|||
"func"
|
||||
);
|
||||
let ann_table = index.place_table(ann_scope_id);
|
||||
assert_eq!(names(&ann_table), vec!["T"]);
|
||||
assert_eq!(names(ann_table), vec!["T"]);
|
||||
|
||||
let [(func_scope_id, func_scope)] =
|
||||
index.child_scopes(ann_scope_id).collect::<Vec<_>>()[..]
|
||||
|
|
@ -1336,7 +1334,7 @@ def func[T]():
|
|||
"func"
|
||||
);
|
||||
let func_table = index.place_table(func_scope_id);
|
||||
assert_eq!(names(&func_table), vec!["x"]);
|
||||
assert_eq!(names(func_table), vec!["x"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -1352,7 +1350,7 @@ class C[T]:
|
|||
let index = semantic_index(&db, file);
|
||||
let global_table = index.place_table(FileScopeId::global());
|
||||
|
||||
assert_eq!(names(&global_table), vec!["C"]);
|
||||
assert_eq!(names(global_table), vec!["C"]);
|
||||
|
||||
let [(ann_scope_id, ann_scope)] = index
|
||||
.child_scopes(FileScopeId::global())
|
||||
|
|
@ -1364,7 +1362,7 @@ class C[T]:
|
|||
assert_eq!(ann_scope.kind(), ScopeKind::TypeParams);
|
||||
assert_eq!(ann_scope_id.to_scope_id(&db, file).name(&db, &module), "C");
|
||||
let ann_table = index.place_table(ann_scope_id);
|
||||
assert_eq!(names(&ann_table), vec!["T"]);
|
||||
assert_eq!(names(ann_table), vec!["T"]);
|
||||
assert!(
|
||||
ann_table
|
||||
.symbol_by_name("T")
|
||||
|
|
@ -1383,7 +1381,7 @@ class C[T]:
|
|||
class_scope_id.to_scope_id(&db, file).name(&db, &module),
|
||||
"C"
|
||||
);
|
||||
assert_eq!(names(&index.place_table(class_scope_id)), vec!["x"]);
|
||||
assert_eq!(names(index.place_table(class_scope_id)), vec!["x"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue