Skip to content

Commit

Permalink
[red-knot] Avoid Ranged for definition target range (#15118)
Browse files Browse the repository at this point in the history
## Summary

Ref:
3533d7f#r150651102

This PR removes the `Ranged` implementation on `DefinitionKind` and
instead uses a method called `target_range` to avoid any confusion about
what range this is for i.e., it's not the range of the node that
represents the definition.
  • Loading branch information
dhruvmanila authored Dec 23, 2024
1 parent 113c804 commit 03bb942
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions crates/red_knot_python_semantic/src/semantic_index/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,14 @@ pub enum DefinitionKind<'db> {
TypeVarTuple(AstNodeRef<ast::TypeParamTypeVarTuple>),
}

impl Ranged for DefinitionKind<'_> {
fn range(&self) -> TextRange {
impl DefinitionKind<'_> {
/// Returns the [`TextRange`] of the definition target.
///
/// A definition target would mainly be the node representing the symbol being defined i.e.,
/// [`ast::ExprName`] or [`ast::Identifier`] but could also be other nodes.
///
/// This is mainly used for logging and debugging purposes.
pub(crate) fn target_range(&self) -> TextRange {
match self {
DefinitionKind::Import(alias) => alias.range(),
DefinitionKind::ImportFrom(import) => import.alias().range(),
Expand All @@ -498,9 +504,7 @@ impl Ranged for DefinitionKind<'_> {
DefinitionKind::TypeVarTuple(type_var_tuple) => type_var_tuple.name.range(),
}
}
}

impl DefinitionKind<'_> {
pub(crate) fn category(&self) -> DefinitionCategory {
match self {
// functions, classes, and imports always bind, and we consider them declarations
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub(crate) fn infer_definition_types<'db>(
let file = definition.file(db);
let _span = tracing::trace_span!(
"infer_definition_types",
range = ?definition.kind(db).range(),
range = ?definition.kind(db).target_range(),
file = %file.path(db)
)
.entered();
Expand All @@ -154,7 +154,7 @@ pub(crate) fn infer_deferred_types<'db>(
let _span = tracing::trace_span!(
"infer_deferred_types",
definition = ?definition.as_id(),
range = ?definition.kind(db).range(),
range = ?definition.kind(db).target_range(),
file = %file.path(db)
)
.entered();
Expand Down

0 comments on commit 03bb942

Please sign in to comment.