feat(compiler): handle Rust identifier escaping and name collisions#3744
Open
BaldDemian wants to merge 1 commit into
Open
feat(compiler): handle Rust identifier escaping and name collisions#3744BaldDemian wants to merge 1 commit into
BaldDemian wants to merge 1 commit into
Conversation
3 tasks
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.
Why?
The Fory compiler should handle above edge-cases carefully by sanitizing the identifiers or throwing collision errors to the user, instead of generating Rust code that cannot be compiled by the Rust compiler.
What does this PR do?
Scope
This PR handles Rust identifier escaping and name collisions in both normal
messagedefinition and gRPC service definition.Note: for now the handling logic for gRPC service definition is also defined in
compiler/fory_compiler/generators/rust.py, I plan to move them intocompiler/fory_compiler/generators/services/rust.pyin the Rust gRPC code generation PR.Workflow
The current implementation broadly follows the approach used by prost-build.
It first normalizes identifiers from the IDL according to Rust naming conventions, e.g. converting struct names to UpperCamelCase and field names to snake_case.
It then checks whether the normalized identifiers are Rust keywords and escapes them when necessary.
Finally it checks the sanitized (i.e. normalized + escaped) identifier with already allocated identifiers in the same scope for any collision. The sanitized identifiers are cached to make sure the definition site and all use sites refer to the same name.
Related issues
Close #3544.
AI Contribution Checklist
yes/noyes, I included a completed AI Contribution Checklist in this PR description and the requiredAI Usage Disclosure.yes, my PR description includes the requiredai_reviewsummary and screenshot evidence of the final clean AI review results from both fresh reviewers on the current PR diff or current HEAD after the latest code changes.Does this PR introduce any user-facing change?
N/A.
Benchmark
N/A.