Skip to content
Merged
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
5 changes: 5 additions & 0 deletions rust/rubydex/src/model/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ impl Namespace {
pub fn set_singleton_class_id(&mut self, declaration_id: DeclarationId) {
all_namespaces!(self, it => it.set_singleton_class_id(declaration_id));
}

#[must_use]
pub fn owner_id(&self) -> &DeclarationId {
all_namespaces!(self, it => &it.owner_id)
}
}

namespace_declaration!(Class, ClassDeclaration);
Expand Down
25 changes: 25 additions & 0 deletions rust/rubydex/src/model/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,31 @@ impl Graph {
name_id
}

/// Searches for the initial attached object for an arbitrarily nested singleton class.
/// Walks up the owner chain until finding a non-singleton namespace.
///
/// # Example
/// For `Foo::<Foo>::<<Foo>>`, returns `Foo`
///
/// # Panics
///
/// Panics if we attached a singleton class to something that isn't a namespace
#[must_use]
pub fn attached_object<'a>(&'a self, maybe_singleton: &'a Namespace) -> &'a Namespace {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a doc comment here to clarify the purpose. Also, should we validate that we eventually find a non-singleton class?

Suggested change
pub fn attached_object<'a>(&'a self, maybe_singleton: &'a Namespace) -> &'a Namespace {
/// Searches for the initial attached object for an arbitrarily nested singleton class.
/// Walks up the owner chain until finding a non-singleton namespace.
///
/// # Example
/// For `Foo::<Foo>::<<Foo>>`, returns `Foo`
///
/// # Panics
///
/// Panics if we attached a singleton class to something that isn't a namespace
/// or if the owner chain is broken.
#[must_use]
pub fn attached_object<'a>(&'a self, maybe_singleton: &'a Namespace) -> &'a Namespace {

My only concern is - what happens if we have a broken chain where the loop never exits? Should we add a safety check or assert that attached_object is actually different from the input?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the docs you suggested. Regarding the broken chain, I'm trying to think if we can add some debug asserts to verify that owners are always namespaces and ultimately connected to Object.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can have a sanity check about the resolution data, so that we can avoid paying the price of checking in the completion loop. I'll prototype this in a different PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let mut attached_object = maybe_singleton;

while matches!(attached_object, Namespace::SingletonClass(_)) {
attached_object = self
.declarations
.get(attached_object.owner_id())
.unwrap()
.as_namespace()
.unwrap();
}

attached_object
}

#[must_use]
pub fn get(&self, name: &str) -> Option<Vec<&Definition>> {
let declaration_id = DeclarationId::from(name);
Expand Down
5 changes: 5 additions & 0 deletions rust/rubydex/src/model/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ impl ResolvedName {
pub fn declaration_id(&self) -> &DeclarationId {
&self.declaration_id
}

#[must_use]
pub fn nesting(&self) -> &Option<NameId> {
self.name.nesting()
}
}

/// A usage of a constant name. This could be a constant reference or a definition like a class or module
Expand Down
Loading
Loading