transpile: Handle module name collisions#1858
Draft
randomPoison wants to merge 3 commits into
Draft
Conversation
ahomescu
requested changes
Jun 16, 2026
| } | ||
|
|
||
| for i in 1.. { | ||
| let candidate = format!("{module_name}{i}"); |
Contributor
There was a problem hiding this comment.
The transpiler renamer uses the scheme {name}_{i}, it would make sense to keep that for consistency.
| .iter() | ||
| .filter_map(|(module, path)| { | ||
| internal_modules | ||
| .iter() |
Contributor
There was a problem hiding this comment.
This is quadratic in the number of modules, it might be worth optimizing this a little. Maybe sort them first, or use a trie?
| } | ||
| for relpath in collision_relpaths { | ||
| let mut path = module_path(&relpath); | ||
| let module_name = format!("{}_", path.pop().unwrap()); |
Contributor
There was a problem hiding this comment.
"{}_0" for consistency with the transpiler? Or anything other than a trailing _?
| let mut path = module_path(&relpath); | ||
| let module_name = format!("{}_", path.pop().unwrap()); | ||
| let file_name = relpath.file_name().unwrap().to_str().unwrap().to_string(); | ||
| module_tree.insert_path_module(&path, file_name, module_name); |
Contributor
There was a problem hiding this comment.
Doesn't insert_path_module already checks for uniqueness? Why do you need the extra trailing underscore?
| } | ||
| for relpath in collision_relpaths { | ||
| let mut path = module_path(&relpath); | ||
| let module_name = format!("{}_", path.pop().unwrap()); |
Contributor
There was a problem hiding this comment.
Don't we also need to check the new module for collisions?
thedataking
reviewed
Jun 17, 2026
| } | ||
| } | ||
|
|
||
| unreachable!("We tried all the numbers and couln't find one that didn't collide") |
thedataking
reviewed
Jun 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In testing against libgit2, we ran into a case where module names could collide, leading to a generated Rust file not being included in the build. The situation is that we have both a
hash.cfile and some files in ahashdirectory, e.g.hash/sha1.c. C2rust then wants to generate bothand
In this case, the latter is what gets generated, effectively dropping
src/hash.rsfrom the Rust build.This PR has c2rust detect these collisions and rename the colliding module, so we'd generate something like