Skip to content

Commit

Permalink
Use smol_str for Identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jun 29, 2024
1 parent da78de0 commit c157032
Show file tree
Hide file tree
Showing 456 changed files with 13,689 additions and 4,704 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/red_knot_python_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ bitflags = { workspace = true }
indexmap = { workspace = true }
salsa = { workspace = true }
smallvec = { workspace = true }
smol_str = { workspace = true }
tracing = { workspace = true }
rustc-hash = { workspace = true }
hashbrown = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion crates/red_knot_python_semantic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod ast_node_ref;
mod db;
pub mod name;
mod node_key;
pub mod semantic_index;
pub mod types;
Expand Down
56 changes: 0 additions & 56 deletions crates/red_knot_python_semantic/src/name.rs

This file was deleted.

25 changes: 12 additions & 13 deletions crates/red_knot_python_semantic/src/semantic_index/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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);
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/red_knot_python_semantic/src/semantic_index/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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;
Expand Down
13 changes: 6 additions & 7 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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),
});
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/fix/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub(crate) fn make_redundant_alias<'a>(
.filter_map(|name| {
aliases
.iter()
.find(|alias| alias.asname.is_none() && name == alias.name.id)
.find(|alias| alias.asname.is_none() && *name == alias.name.id)
.map(|alias| Edit::range_replacement(format!("{name} as {name}"), alias.range))
})
.collect()
Expand Down
10 changes: 5 additions & 5 deletions crates/ruff_linter/src/importer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
use std::error::Error;

use anyhow::Result;
use libcst_native::{ImportAlias, Name, NameOrAttribute};
use ruff_python_ast::{self as ast, ModModule, Stmt};
use ruff_python_parser::{Parsed, Tokens};
use ruff_text_size::{Ranged, TextSize};
use libcst_native::{ImportAlias, Name as cstName, NameOrAttribute};

use ruff_diagnostics::Edit;
use ruff_python_ast::imports::{AnyImport, Import, ImportFrom};
use ruff_python_ast::{self as ast, ModModule, Stmt};
use ruff_python_codegen::Stylist;
use ruff_python_parser::{Parsed, Tokens};
use ruff_python_semantic::{ImportedName, SemanticModel};
use ruff_python_trivia::textwrap::indent;
use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextSize};

use crate::cst::matchers::{match_aliases, match_import_from, match_statement};
use crate::fix;
Expand Down Expand Up @@ -425,7 +425,7 @@ impl<'a> Importer<'a> {
let import_from = match_import_from(&mut statement)?;
let aliases = match_aliases(import_from)?;
aliases.push(ImportAlias {
name: NameOrAttribute::N(Box::new(Name {
name: NameOrAttribute::N(Box::new(cstName {
value: member,
lpar: vec![],
rpar: vec![],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub(crate) fn variable_name_task_id(
let ast::ExprStringLiteral { value: task_id, .. } = keyword.value.as_string_literal_expr()?;

// If the target name is the same as the task_id, no violation.
if task_id == id {
if task_id == id.as_str() {
return None;
}

Expand Down
7 changes: 4 additions & 3 deletions crates/ruff_linter/src/rules/flake8_annotations/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ruff_diagnostics::Edit;
use ruff_python_ast::helpers::{
pep_604_union, typing_optional, typing_union, ReturnStatementVisitor,
};
use ruff_python_ast::name::Name;
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::{self as ast, Expr, ExprContext};
use ruff_python_semantic::analyze::terminal::Terminal;
Expand Down Expand Up @@ -140,7 +141,7 @@ impl AutoPythonType {
)
.ok()?;
let expr = Expr::Name(ast::ExprName {
id: binding,
id: Name::from(binding),
range: TextRange::default(),
ctx: ExprContext::Load,
});
Expand Down Expand Up @@ -181,7 +182,7 @@ impl AutoPythonType {
semantic,
)
.ok()?;
let expr = typing_optional(element, binding);
let expr = typing_optional(element, Name::from(binding));
Some((expr, vec![optional_edit]))
}
_ => {
Expand All @@ -198,7 +199,7 @@ impl AutoPythonType {
semantic,
)
.ok()?;
let expr = typing_union(&elements, binding);
let expr = typing_union(&elements, Name::from(binding));
Some((expr, vec![union_edit]))
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/rules/flake8_gettext/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Rules from [flake8-gettext](https://pypi.org/project/flake8-gettext/).
use ruff_python_ast::name::Name;
use ruff_python_ast::{self as ast, Expr};

pub(crate) mod rules;
pub mod settings;

/// Returns true if the [`Expr`] is an internationalization function call.
pub(crate) fn is_gettext_func_call(func: &Expr, functions_names: &[String]) -> bool {
pub(crate) fn is_gettext_func_call(func: &Expr, functions_names: &[Name]) -> bool {
if let Expr::Name(ast::ExprName { id, .. }) = func {
functions_names.contains(id)
} else {
Expand Down
Loading

0 comments on commit c157032

Please sign in to comment.