mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-09 21:28:21 +00:00
Remove RefEquality
(#6393)
## Summary See discussion in https://github.com/astral-sh/ruff/pull/6351#discussion_r1284996979. We can remove `RefEquality` entirely and instead use a text offset for statement keys, since no two statements can start at the same text offset. ## Test Plan `cargo test`
This commit is contained in:
parent
9328606843
commit
c895252aae
3 changed files with 28 additions and 88 deletions
|
@ -3,8 +3,8 @@ use std::ops::Index;
|
|||
use rustc_hash::FxHashMap;
|
||||
|
||||
use ruff_index::{newtype_index, IndexVec};
|
||||
use ruff_python_ast::types::RefEquality;
|
||||
use ruff_python_ast::Stmt;
|
||||
use ruff_python_ast::{Ranged, Stmt};
|
||||
use ruff_text_size::TextSize;
|
||||
|
||||
/// Id uniquely identifying a statement AST node.
|
||||
///
|
||||
|
@ -30,7 +30,19 @@ struct StatementWithParent<'a> {
|
|||
#[derive(Debug, Default)]
|
||||
pub struct Statements<'a> {
|
||||
statements: IndexVec<StatementId, StatementWithParent<'a>>,
|
||||
statement_to_id: FxHashMap<RefEquality<'a, Stmt>, StatementId>,
|
||||
statement_to_id: FxHashMap<StatementKey, StatementId>,
|
||||
}
|
||||
|
||||
/// A unique key for a statement AST node. No two statements can appear at the same location
|
||||
/// in the source code, since compound statements must be delimited by _at least_ one character
|
||||
/// (a colon), so the starting offset is a cheap and sufficient unique identifier.
|
||||
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
|
||||
pub struct StatementKey(TextSize);
|
||||
|
||||
impl From<&Stmt> for StatementKey {
|
||||
fn from(statement: &Stmt) -> Self {
|
||||
Self(statement.start())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Statements<'a> {
|
||||
|
@ -43,7 +55,10 @@ impl<'a> Statements<'a> {
|
|||
parent: Option<StatementId>,
|
||||
) -> StatementId {
|
||||
let next_id = self.statements.next_index();
|
||||
if let Some(existing_id) = self.statement_to_id.insert(RefEquality(statement), next_id) {
|
||||
if let Some(existing_id) = self
|
||||
.statement_to_id
|
||||
.insert(StatementKey::from(statement), next_id)
|
||||
{
|
||||
panic!("Statements already exists with ID: {existing_id:?}");
|
||||
}
|
||||
self.statements.push(StatementWithParent {
|
||||
|
@ -56,7 +71,9 @@ impl<'a> Statements<'a> {
|
|||
/// Returns the [`StatementId`] of the given statement.
|
||||
#[inline]
|
||||
pub fn statement_id(&self, statement: &'a Stmt) -> Option<StatementId> {
|
||||
self.statement_to_id.get(&RefEquality(statement)).copied()
|
||||
self.statement_to_id
|
||||
.get(&StatementKey::from(statement))
|
||||
.copied()
|
||||
}
|
||||
|
||||
/// Return the [`StatementId`] of the parent statement.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue