Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions rust/rubydex/src/model/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ impl Declaration {
}
}

#[must_use]
pub fn as_singleton_class(&self) -> Option<&Namespace> {
match self {
Declaration::Namespace(ns @ Namespace::SingletonClass(_)) => Some(ns),
_ => None,
}
}

#[must_use]
pub fn as_namespace_mut(&mut self) -> Option<&mut Namespace> {
match self {
Expand All @@ -349,6 +357,20 @@ impl Declaration {
all_declarations!(self, it => it.definition_ids.is_empty())
}

/// Returns true if this declaration is no longer anchored to anything
/// and can be removed. For most declarations, that means having no
/// definitions. Singleton classes are an exception: they're also kept
/// alive by their members (e.g. `@x`, `def self.bar`) and by inbound
/// constant references (e.g. `Foo.new`).
#[must_use]
pub fn is_removable(&self) -> bool {
if let Some(ns) = self.as_singleton_class() {
return ns.definitions().is_empty() && ns.members().is_empty() && ns.references().is_empty();
}

self.has_no_definitions()
}

pub fn add_definition(&mut self, definition_id: DefinitionId) {
all_declarations!(self, it => {
debug_assert!(
Expand Down
Loading
Loading