Skip to content

fix(rust): escape Rust keywords in union constructor names and deconflict unknown#16635

Closed
devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/1781914395-fix-rust-union-keyword-escaping
Closed

fix(rust): escape Rust keywords in union constructor names and deconflict unknown#16635
devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/1781914395-fix-rust-union-keyword-escaping

Conversation

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Description

Linear ticket: Closes FER-11400

Fixes two compilation errors in generated Rust code for discriminated unions in UnionGenerator.ts:

  1. Bug A — Constructor names that are Rust keywords (static, override, etc.) now emit r# raw identifier prefix via escapeRustKeyword.
  2. Bug B — The forward-compatible catch-all constructor deconflicts to unknown_value when a real variant already uses the unknown name.

Changes Made

  • Route constructor name through this.context.escapeRustKeyword() in generateVariantConstructor
  • Add getUnknownConstructorName() helper that returns "unknown_value" when a real variant collides
  • Add KeywordUnion regression test (discriminants: static, override, unknown) to the snapshot harness
  • Add changelog entry

Generated output for the regression test:

impl KeywordUnion {
    pub fn r#static(value: String) -> Self { ... }
    pub fn r#override(priority: i64) -> Self { ... }
    pub fn unknown(payload: String) -> Self { ... }      // real variant
    pub fn unknown_value(value: serde_json::Value) -> Self { ... } // catch-all
}
  • Updated README.md generator (if applicable) — N/A

Testing

  • Unit tests added/updated — snapshot test with KeywordUnion type
  • Manual testing completed — pnpm --filter @fern-api/rust-model test passes (16/16)

Link to Devin session: https://app.devin.ai/sessions/797a62f254f3410cbe25eab33737157d
Requested by: @jsklan

…lict unknown

Fixes two bugs in UnionGenerator.ts that cause generated Rust code to
fail compilation:

1. Union variant constructor names that are Rust keywords (static,
   override, etc.) now use r# raw identifier prefix.

2. The forward-compatible catch-all constructor deconflicts to
   'unknown_value' when a real variant already produces an 'unknown'
   constructor name.

Adds regression test with KeywordUnion type exercising both fixes.

Resolves FER-11400

Co-Authored-By: judah <jsklan.development@gmail.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@github-actions

github-actions Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

SDK Generation Benchmark Results

Comparing PR branch against median of 5 nightly run(s) on main (latest: 2026-06-19T05:42:07Z).

Full benchmark table (click to expand)
Generator Spec main (generator) main (E2E) PR (generator) Delta
rust-sdk square 166s (n=5) 177s (n=5) 170s +4s (+2.4%)

main (generator): generator-only time via --skip-scripts (includes Docker image build, container startup, IR parsing, and code generation — this is the same Docker-based flow customers use via fern generate). main (E2E): full customer-observable time including build/test scripts (nightly baseline, informational). Delta is computed against generator-only baseline.
⚠️ = generation exited with a non-zero exit code (timing may not reflect a successful run).
Baseline from nightly runs on main (latest: 2026-06-19T05:42:07Z). Trigger benchmark-baseline to refresh.
Last updated: 2026-06-20 00:32 UTC

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Devin Review found 2 potential issues.

Open in Devin Review

@devin-ai-integration devin-ai-integration Bot Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🚩 Missing model generator changelog entry

The code change is in generators/rust/model/src/union/UnionGenerator.ts, which belongs to the model generator (independently versioned at generators/rust/model/versions.yml). The PR only adds a changelog entry for the SDK generator (generators/rust/sdk/changes/unreleased/fix-union-keyword-escaping.yml). The model generator doesn't use a changes/unreleased/ workflow — its versions.yml is edited directly in PRs. Since the model generator is released independently (as shown by its own version history), this fix would not be reflected in model generator release notes. The SDK changelog covers the SDK release, but users of the standalone model generator would miss this fix in their changelogs.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Intentional — rust-model doesn't have its own changelog/release entry. The SDK depends on the model package ("@fern-api/rust-model": "workspace:*") and publishes them together, so the SDK changelog is the correct place.

Comment thread generators/rust/model/src/union/UnionGenerator.ts
Handles edge case where both 'unknown' and 'unknown_value' variants
exist by iterating until a collision-free name is found.

Co-Authored-By: judah <jsklan.development@gmail.com>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Devin Review found 0 new potential issues.

Open in Devin Review

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.

1 participant