Skip to content

feat(compiler): handle Rust identifier escaping and name collisions#3744

Open
BaldDemian wants to merge 1 commit into
apache:mainfrom
BaldDemian:rust-keywords
Open

feat(compiler): handle Rust identifier escaping and name collisions#3744
BaldDemian wants to merge 1 commit into
apache:mainfrom
BaldDemian:rust-keywords

Conversation

@BaldDemian

@BaldDemian BaldDemian commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Why?

  • Identifiers colliding with Rust keywords should be escaped.
  • After naming normalization, some identifiers may collide with each other.

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 message definition 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 into compiler/fory_compiler/generators/services/rust.py in 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

  • Substantial AI assistance was used in this PR: yes / no
  • If yes, I included a completed AI Contribution Checklist in this PR description and the required AI Usage Disclosure.
  • If yes, my PR description includes the required ai_review summary 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keywords are not escaped in compiler

1 participant