Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type name collision fix #601

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
SRetip committed Jun 27, 2024
commit c95f15e5cdb2d755295a940e0d99a207bdacb66c
9 changes: 2 additions & 7 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,7 @@ impl TypeSpace {
let type_id = TypeId(index);
let mut type_entry = self.id_to_entry.get(&type_id).unwrap().clone();
type_entry.finalize(self)?;
if let Some(mut name) = type_entry.name().cloned() {
while self.names.contains(&name) {
name = format!("{name}Alias");
}
self.names.insert(name.clone());
type_entry.rename(name);
}
type_entry.ensure_unique_name(self);
self.id_to_entry.insert(type_id, type_entry);
}

Expand Down Expand Up @@ -689,6 +683,7 @@ impl TypeSpace {
let type_id = TypeId(index);
let mut type_entry = self.id_to_entry.get(&type_id).unwrap().clone();
type_entry.finalize(self)?;
type_entry.ensure_unique_name(self);
self.id_to_entry.insert(type_id, type_entry);
}

Expand Down
15 changes: 15 additions & 0 deletions typify-impl/src/type_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,21 @@ impl TypeEntry {
self.check_defaults(type_space)
}

// Ensures that the name is unique within the provided `TypeSpace`.
//
// This function checks if the current object's name already exists in the `TypeSpace`. If it does,
// the function modifies the name by appending "Alias" until a unique name is found. The unique name
// is then inserted into the `TypeSpace` and the object's name is updated accordingly.
pub(crate) fn ensure_unique_name(&mut self, type_space: &mut TypeSpace) {
if let Some(mut name) = self.name().cloned() {
while type_space.names.contains(&name) {
name = format!("{name}Alias");
}
type_space.names.insert(name.clone());
self.rename(name);
}
}

pub(crate) fn name(&self) -> Option<&String> {
match &self.details {
TypeEntryDetails::Enum(TypeEntryEnum { name, .. })
Expand Down
Loading