Skip to content

Commit

Permalink
Use ruff_python_ast::Name in red_knot crate
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jul 1, 2024
1 parent 4ffd07e commit 43ea86c
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 49 deletions.
40 changes: 0 additions & 40 deletions crates/red_knot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::fmt::Formatter;
use std::hash::BuildHasherDefault;
use std::ops::Deref;
use std::path::{Path, PathBuf};

use rustc_hash::{FxHashSet, FxHasher};
Expand Down Expand Up @@ -68,41 +66,3 @@ impl Workspace {
self.open_files.contains(&file_id)
}
}

#[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))
}

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 Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
3 changes: 1 addition & 2 deletions crates/red_knot/src/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::files::FileId;
use crate::module::Module;
use crate::module::ModuleName;
use crate::parse::parse;
use crate::Name;
pub(crate) use definitions::Definition;
use definitions::{ImportDefinition, ImportFromDefinition};
pub(crate) use flow_graph::ConstrainedDefinition;
Expand Down Expand Up @@ -437,7 +436,7 @@ impl SourceOrderVisitor<'_> for SemanticIndexer {
};
let def = Definition::ImportFrom(ImportFromDefinition {
module: module.clone(),
name: Name::new(&alias.name.id),
name: alias.name.id.clone(),
level: *level,
});
self.add_or_update_symbol_with_def(symbol_name, def);
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot/src/semantic/definitions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ast_ids::TypedNodeKey;
use crate::semantic::ModuleName;
use crate::Name;
use ruff_python_ast as ast;
use ruff_python_ast::name::Name;

// TODO storing TypedNodeKey for definitions means we have to search to find them again in the AST;
// this is at best O(log n). If looking up definitions is a bottleneck we should look for
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot/src/semantic/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use hashbrown::hash_map::{Keys, RawEntryMut};
use rustc_hash::{FxHashMap, FxHasher};

use ruff_index::{newtype_index, IndexVec};
use ruff_python_ast::name::Name;

use crate::ast_ids::NodeKey;
use crate::module::ModuleName;
use crate::semantic::{Definition, ExpressionId};
use crate::Name;

type Map<K, V> = hashbrown::HashMap<K, V, ()>;

Expand Down
3 changes: 2 additions & 1 deletion crates/red_knot/src/semantic/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use crate::module::{Module, ModuleName};
use crate::semantic::{
resolve_global_symbol, semantic_index, GlobalSymbolId, ScopeId, ScopeKind, SymbolId,
};
use crate::{FxDashMap, FxIndexSet, Name};
use crate::{FxDashMap, FxIndexSet};
use ruff_index::{newtype_index, IndexVec};
use ruff_python_ast as ast;
use rustc_hash::FxHashMap;

pub(crate) mod infer;

pub(crate) use infer::{infer_definition_type, infer_symbol_public_type};
use ruff_python_ast::name::Name;

/// unique ID for a type
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
Expand Down
8 changes: 4 additions & 4 deletions crates/red_knot/src/semantic/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::semantic::{
resolve_global_symbol, semantic_index, ConstrainedDefinition, Definition, GlobalSymbolId,
ImportDefinition, ImportFromDefinition,
};
use crate::{FileId, Name};
use crate::FileId;

// FIXME: Figure out proper dead-lock free synchronisation now that this takes `&db` instead of `&mut db`.
/// Resolve the public-facing type for a symbol (the type seen by other scopes: other modules, or
Expand Down Expand Up @@ -315,7 +315,7 @@ fn infer_expr_type(db: &dyn SemanticDb, file_id: FileId, expr: &ast::Expr) -> Qu
}
ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => {
let value_type = infer_expr_type(db, file_id, value)?;
let attr_name = &Name::new(&attr.id);
let attr_name = &attr.id;
value_type
.get_member(db, attr_name)
.map(|ty| ty.unwrap_or(Type::Unknown))
Expand Down Expand Up @@ -343,6 +343,7 @@ fn infer_expr_type(db: &dyn SemanticDb, file_id: FileId, expr: &ast::Expr) -> Qu
#[cfg(test)]
mod tests {

use ruff_python_ast::name::Name;
use std::path::PathBuf;

use crate::db::tests::TestDb;
Expand All @@ -351,7 +352,6 @@ mod tests {
resolve_module, set_module_search_paths, ModuleName, ModuleResolutionInputs,
};
use crate::semantic::{infer_symbol_public_type, resolve_global_symbol, Type};
use crate::Name;

// TODO with virtual filesystem we shouldn't have to write files to disk for these
// tests
Expand Down Expand Up @@ -476,7 +476,7 @@ mod tests {
};

let member_ty = class_id
.get_own_class_member(&case.db, &Name::new("f"))
.get_own_class_member(&case.db, &Name::new_static("f"))
.expect("C.f to resolve");

let Some(Type::Function(func_id)) = member_ty else {
Expand Down

0 comments on commit 43ea86c

Please sign in to comment.