diff --git a/crates/ruff_python_semantic/src/scope.rs b/crates/ruff_python_semantic/src/scope.rs index e1366de65a..28c42b286d 100644 --- a/crates/ruff_python_semantic/src/scope.rs +++ b/crates/ruff_python_semantic/src/scope.rs @@ -104,41 +104,6 @@ impl<'a> Scope<'a> { } } -/// Id uniquely identifying a scope in a program. -/// -/// Using a `u32` is sufficient because Ruff only supports parsing documents with a size of max `u32::max` -/// and it is impossible to have more scopes than characters in the file (because defining a function or class -/// requires more than one character). -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)] -pub struct ScopeId(u32); - -impl ScopeId { - /// Returns the ID for the global scope - #[inline] - pub const fn global() -> Self { - ScopeId(0) - } - - /// Returns `true` if this is the id of the global scope - pub const fn is_global(&self) -> bool { - self.0 == 0 - } -} - -impl TryFrom for ScopeId { - type Error = TryFromIntError; - - fn try_from(value: usize) -> Result { - Ok(Self(u32::try_from(value)?)) - } -} - -impl From for usize { - fn from(value: ScopeId) -> Self { - value.0 as usize - } -} - #[derive(Debug, is_macro::Is)] pub enum ScopeKind<'a> { Class(ClassDef<'a>), @@ -181,6 +146,41 @@ pub struct Lambda<'a> { pub body: &'a Expr, } +/// Id uniquely identifying a scope in a program. +/// +/// Using a `u32` is sufficient because Ruff only supports parsing documents with a size of max `u32::max` +/// and it is impossible to have more scopes than characters in the file (because defining a function or class +/// requires more than one character). +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub struct ScopeId(u32); + +impl ScopeId { + /// Returns the ID for the global scope + #[inline] + pub const fn global() -> Self { + ScopeId(0) + } + + /// Returns `true` if this is the id of the global scope + pub const fn is_global(&self) -> bool { + self.0 == 0 + } +} + +impl TryFrom for ScopeId { + type Error = TryFromIntError; + + fn try_from(value: usize) -> Result { + Ok(Self(u32::try_from(value)?)) + } +} + +impl From for usize { + fn from(value: ScopeId) -> Self { + value.0 as usize + } +} + /// The scopes of a program indexed by [`ScopeId`] #[derive(Debug)] pub struct Scopes<'a>(Vec>);