mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00
Use CompactString
for Identifier
(#12101)
This commit is contained in:
parent
db6ee74cbe
commit
5109b50bb3
474 changed files with 4953 additions and 4776 deletions
|
@ -1,6 +1,5 @@
|
|||
pub mod ast_node_ref;
|
||||
mod db;
|
||||
pub mod name;
|
||||
mod node_key;
|
||||
pub mod semantic_index;
|
||||
pub mod types;
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
||||
pub struct Name(smol_str::SmolStr);
|
||||
|
||||
impl Name {
|
||||
#[inline]
|
||||
pub fn new(name: &str) -> Self {
|
||||
Self(smol_str::SmolStr::new(name))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_static(name: &'static str) -> Self {
|
||||
Self(smol_str::SmolStr::new_static(name))
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
self.0.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Name {
|
||||
type Target = str;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for Name
|
||||
where
|
||||
T: Into<smol_str::SmolStr>,
|
||||
{
|
||||
fn from(value: T) -> Self {
|
||||
Self(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Name {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<str> for Name {
|
||||
fn eq(&self, other: &str) -> bool {
|
||||
self.as_str() == other
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<Name> for str {
|
||||
fn eq(&self, other: &Name) -> bool {
|
||||
other == self
|
||||
}
|
||||
}
|
|
@ -5,9 +5,9 @@ use rustc_hash::FxHashMap;
|
|||
use ruff_db::parsed::ParsedModule;
|
||||
use ruff_index::IndexVec;
|
||||
use ruff_python_ast as ast;
|
||||
use ruff_python_ast::name::Name;
|
||||
use ruff_python_ast::visitor::{walk_expr, walk_stmt, Visitor};
|
||||
|
||||
use crate::name::Name;
|
||||
use crate::node_key::NodeKey;
|
||||
use crate::semantic_index::ast_ids::{
|
||||
AstId, AstIdsBuilder, ScopeAssignmentId, ScopeClassId, ScopeFunctionId, ScopeImportFromId,
|
||||
|
@ -133,7 +133,6 @@ impl<'a> SemanticIndexBuilder<'a> {
|
|||
fn add_or_update_symbol_with_definition(
|
||||
&mut self,
|
||||
name: Name,
|
||||
|
||||
definition: Definition,
|
||||
) -> ScopedSymbolId {
|
||||
let symbol_table = self.current_symbol_table();
|
||||
|
@ -168,7 +167,7 @@ impl<'a> SemanticIndexBuilder<'a> {
|
|||
ast::TypeParam::ParamSpec(ast::TypeParamParamSpec { name, .. }) => name,
|
||||
ast::TypeParam::TypeVarTuple(ast::TypeParamTypeVarTuple { name, .. }) => name,
|
||||
};
|
||||
self.add_or_update_symbol(Name::new(name), SymbolFlags::IS_DEFINED);
|
||||
self.add_or_update_symbol(name.id.clone(), SymbolFlags::IS_DEFINED);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -233,7 +232,7 @@ impl Visitor<'_> for SemanticIndexBuilder<'_> {
|
|||
for decorator in &function_def.decorator_list {
|
||||
self.visit_decorator(decorator);
|
||||
}
|
||||
let name = Name::new(&function_def.name.id);
|
||||
let name = &function_def.name.id;
|
||||
let function_id = ScopeFunctionId(statement_id);
|
||||
let definition = Definition::FunctionDef(function_id);
|
||||
let scope = self.current_scope();
|
||||
|
@ -243,7 +242,7 @@ impl Visitor<'_> for SemanticIndexBuilder<'_> {
|
|||
);
|
||||
|
||||
self.with_type_params(
|
||||
&name,
|
||||
name,
|
||||
&WithTypeParams::FunctionDef {
|
||||
node: function_def,
|
||||
id: AstId::new(scope, function_id),
|
||||
|
@ -257,7 +256,7 @@ impl Visitor<'_> for SemanticIndexBuilder<'_> {
|
|||
|
||||
builder.push_scope(
|
||||
NodeWithScopeId::Function(AstId::new(scope, function_id)),
|
||||
&name,
|
||||
name,
|
||||
Some(symbol),
|
||||
Some(definition),
|
||||
);
|
||||
|
@ -271,7 +270,7 @@ impl Visitor<'_> for SemanticIndexBuilder<'_> {
|
|||
self.visit_decorator(decorator);
|
||||
}
|
||||
|
||||
let name = Name::new(&class.name.id);
|
||||
let name = &class.name.id;
|
||||
let class_id = ScopeClassId(statement_id);
|
||||
let definition = Definition::from(class_id);
|
||||
let scope = self.current_scope();
|
||||
|
@ -280,7 +279,7 @@ impl Visitor<'_> for SemanticIndexBuilder<'_> {
|
|||
self.add_or_update_symbol_with_definition(name.clone(), definition),
|
||||
);
|
||||
self.with_type_params(
|
||||
&name,
|
||||
name,
|
||||
&WithTypeParams::ClassDef {
|
||||
node: class,
|
||||
id: AstId::new(scope, class_id),
|
||||
|
@ -293,7 +292,7 @@ impl Visitor<'_> for SemanticIndexBuilder<'_> {
|
|||
|
||||
builder.push_scope(
|
||||
NodeWithScopeId::Class(AstId::new(scope, class_id)),
|
||||
&name,
|
||||
name,
|
||||
Some(id),
|
||||
Some(definition),
|
||||
);
|
||||
|
@ -306,16 +305,16 @@ impl Visitor<'_> for SemanticIndexBuilder<'_> {
|
|||
ast::Stmt::Import(ast::StmtImport { names, .. }) => {
|
||||
for (i, alias) in names.iter().enumerate() {
|
||||
let symbol_name = if let Some(asname) = &alias.asname {
|
||||
asname.id.as_str()
|
||||
asname.id.clone()
|
||||
} else {
|
||||
alias.name.id.split('.').next().unwrap()
|
||||
Name::new(alias.name.id.split('.').next().unwrap())
|
||||
};
|
||||
|
||||
let def = Definition::Import(ImportDefinition {
|
||||
import_id: ScopeImportId(statement_id),
|
||||
alias: u32::try_from(i).unwrap(),
|
||||
});
|
||||
self.add_or_update_symbol_with_definition(Name::new(symbol_name), def);
|
||||
self.add_or_update_symbol_with_definition(symbol_name, def);
|
||||
}
|
||||
}
|
||||
ast::Stmt::ImportFrom(ast::StmtImportFrom {
|
||||
|
@ -326,15 +325,15 @@ impl Visitor<'_> for SemanticIndexBuilder<'_> {
|
|||
}) => {
|
||||
for (i, alias) in names.iter().enumerate() {
|
||||
let symbol_name = if let Some(asname) = &alias.asname {
|
||||
asname.id.as_str()
|
||||
&asname.id
|
||||
} else {
|
||||
alias.name.id.as_str()
|
||||
&alias.name.id
|
||||
};
|
||||
let def = Definition::ImportFrom(ImportFromDefinition {
|
||||
import_id: ScopeImportFromId(statement_id),
|
||||
name: u32::try_from(i).unwrap(),
|
||||
});
|
||||
self.add_or_update_symbol_with_definition(Name::new(symbol_name), def);
|
||||
self.add_or_update_symbol_with_definition(symbol_name.clone(), def);
|
||||
}
|
||||
}
|
||||
ast::Stmt::Assign(node) => {
|
||||
|
@ -375,10 +374,10 @@ impl Visitor<'_> for SemanticIndexBuilder<'_> {
|
|||
};
|
||||
match self.current_definition {
|
||||
Some(definition) if flags.contains(SymbolFlags::IS_DEFINED) => {
|
||||
self.add_or_update_symbol_with_definition(Name::new(id), definition);
|
||||
self.add_or_update_symbol_with_definition(id.clone(), definition);
|
||||
}
|
||||
_ => {
|
||||
self.add_or_update_symbol(Name::new(id), flags);
|
||||
self.add_or_update_symbol(id.clone(), flags);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,13 +7,12 @@ use rustc_hash::FxHasher;
|
|||
use salsa::DebugWithDb;
|
||||
use smallvec::SmallVec;
|
||||
|
||||
use ruff_db::vfs::VfsFile;
|
||||
use ruff_index::{newtype_index, IndexVec};
|
||||
|
||||
use crate::name::Name;
|
||||
use crate::semantic_index::definition::Definition;
|
||||
use crate::semantic_index::{root_scope, semantic_index, symbol_table, SymbolMap};
|
||||
use crate::Db;
|
||||
use ruff_db::vfs::VfsFile;
|
||||
use ruff_index::{newtype_index, IndexVec};
|
||||
use ruff_python_ast::name::Name;
|
||||
|
||||
#[derive(Eq, PartialEq, Debug)]
|
||||
pub struct Symbol {
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
use salsa::DebugWithDb;
|
||||
|
||||
use ruff_db::parsed::parsed_module;
|
||||
use ruff_db::vfs::VfsFile;
|
||||
use ruff_index::newtype_index;
|
||||
use ruff_python_ast as ast;
|
||||
|
||||
use crate::name::Name;
|
||||
use crate::semantic_index::ast_ids::{AstIdNode, ScopeAstIdNode};
|
||||
use crate::semantic_index::symbol::{FileScopeId, PublicSymbolId, ScopeId};
|
||||
use crate::semantic_index::{
|
||||
|
@ -14,6 +8,11 @@ use crate::semantic_index::{
|
|||
use crate::types::infer::{TypeInference, TypeInferenceBuilder};
|
||||
use crate::Db;
|
||||
use crate::FxIndexSet;
|
||||
use ruff_db::parsed::parsed_module;
|
||||
use ruff_db::vfs::VfsFile;
|
||||
use ruff_index::newtype_index;
|
||||
use ruff_python_ast as ast;
|
||||
use ruff_python_ast::name::Name;
|
||||
|
||||
mod display;
|
||||
mod infer;
|
||||
|
|
|
@ -9,7 +9,6 @@ use ruff_index::IndexVec;
|
|||
use ruff_python_ast as ast;
|
||||
use ruff_python_ast::{ExprContext, TypeParams};
|
||||
|
||||
use crate::name::Name;
|
||||
use crate::semantic_index::ast_ids::{ScopeAstIdNode, ScopeExpressionId};
|
||||
use crate::semantic_index::definition::{Definition, ImportDefinition, ImportFromDefinition};
|
||||
use crate::semantic_index::symbol::{FileScopeId, ScopeId, ScopeKind, ScopedSymbolId, SymbolTable};
|
||||
|
@ -199,7 +198,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
}
|
||||
|
||||
let function_ty = self.function_ty(FunctionType {
|
||||
name: Name::new(&name.id),
|
||||
name: name.id.clone(),
|
||||
decorators: decorator_tys,
|
||||
});
|
||||
|
||||
|
@ -248,7 +247,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
assert_eq!(class_body_scope.kind(), ScopeKind::Class);
|
||||
|
||||
let class_ty = self.class_ty(ClassType {
|
||||
name: Name::new(name),
|
||||
name: name.id.clone(),
|
||||
bases,
|
||||
body_scope: class_body_scope_id.to_scope_id(self.db, self.file_id),
|
||||
});
|
||||
|
@ -398,7 +397,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
} = alias;
|
||||
|
||||
let ty = module_ty
|
||||
.member(&self.typing_context(), &Name::new(&name.id))
|
||||
.member(&self.typing_context(), &name.id)
|
||||
.unwrap_or(Type::Unknown);
|
||||
|
||||
self.definition_tys.insert(
|
||||
|
@ -557,7 +556,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
|
||||
let value_ty = self.infer_expression(value);
|
||||
let member_ty = value_ty
|
||||
.member(&self.typing_context(), &Name::new(&attr.id))
|
||||
.member(&self.typing_context(), &attr.id)
|
||||
.unwrap_or(Type::Unknown);
|
||||
|
||||
match ctx {
|
||||
|
@ -695,9 +694,9 @@ mod tests {
|
|||
use ruff_db::vfs::system_path_to_file;
|
||||
|
||||
use crate::db::tests::TestDb;
|
||||
use crate::name::Name;
|
||||
use crate::types::{public_symbol_ty_by_name, Type, TypingContext};
|
||||
use red_knot_module_resolver::{set_module_resolution_settings, ModuleResolutionSettings};
|
||||
use ruff_python_ast::name::Name;
|
||||
|
||||
fn setup_db() -> TestDb {
|
||||
let mut db = TestDb::new();
|
||||
|
@ -791,7 +790,7 @@ class C:
|
|||
};
|
||||
|
||||
let context = TypingContext::global(&db);
|
||||
let member_ty = class_id.class_member(&context, &Name::new("f"));
|
||||
let member_ty = class_id.class_member(&context, &Name::new_static("f"));
|
||||
|
||||
let Some(Type::Function(func_id)) = member_ty else {
|
||||
panic!("C.f is not a Function");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue