Skip to content

fix: support type aliases in binding files (#172) - #174

Merged
lirenjie95 merged 1 commit into
masterfrom
fix/172-type-alias
Aug 24, 2026
Merged

fix: support type aliases in binding files (#172)#174
lirenjie95 merged 1 commit into
masterfrom
fix/172-type-alias

Conversation

@lirenjie95

Copy link
Copy Markdown
Collaborator

Summary

Using a type alias (e.g. pub type Amount = i64;) as a parameter or struct field type in an r2g binding file used to crash codegen (Option::unwrap() on a None in rust2go-common) or emit references to a nonexistent AmountRef. This PR makes type aliases work in binding files.

Fixes #172

Changes

  • rust2go-common/src/common.rs:
    • RawRsFile::new now expands non-generic type aliases at parse time: alias chains are resolved (type A = B; type B = i64;), cycles are reported with a clear panic message, and usages are expanded recursively inside generic arguments (e.g. Vec<Alias>). Expansion applies to struct field types and trait method params/return types.
    • Added ParamType::to_rust_ref_assoc, rendering the ref type as <T as ::rust2go::ToRef>::Ref — this form is correct for both concrete types and aliases (the alias shares the underlying type's ToRef impl).
    • Unit tests for the expansion (primitives, chains, Vec<Alias>, struct fields, trait params/returns).
  • rust2go-common/src/r2g.rs: use to_rust_ref_assoc for response ref types in generated callbacks (3 sites) — the proc-macro side cannot resolve aliases itself.
  • rust2go-macro/src/lib.rs: r2g_derive emits <#ty as ::rust2go::ToRef>::Ref for non-primitive ref fields instead of assuming a {ty}Ref type name.
  • test/: end-to-end coverage — pub type UserId = u64; used in a struct field (BalanceRequest), a sync trait method, and an async #[drop_safe] method with bare-alias params/return; Go impl + regenerated bindings + Rust tests.

Testing

  • Reproduced the panic with a minimal case before the fix; after the fix the generated gen.go expands alias fields to uint64_t and Go signatures use *uint64.
  • cargo fmt --check, cargo clippy --workspace --all-features --all-targets -- --deny warnings, and the full test suite (workspace, with mem-ring tested separately due to the known unrelated workspace feature-unification issue) all pass locally.

Using a type alias (e.g. `pub type Amount = i64;`) as a parameter or
struct field type in an r2g binding file used to panic codegen
(`Option::unwrap()` on a missing struct entry) or emit references to a
nonexistent `AmountRef`.

- rust2go-common: expand non-generic type aliases (with chain resolution
  and cycle detection) in struct fields and trait signatures at parse
  time; generic arguments are expanded recursively.
- rust2go-common: add ParamType::to_rust_ref_assoc rendering
  `<T as ToRef>::Ref` and use it for response ref types on the
  proc-macro side, which cannot resolve aliases itself.
- rust2go-macro: r2g_derive emits `<#ty as ::rust2go::ToRef>::Ref` for
  non-primitive ref fields instead of assuming `{ty}Ref`.
- test: cover aliases in struct fields, sync/async trait params and
  return types, plus unit tests for the alias expansion.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.00000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.52%. Comparing base (1779a40) to head (d2a9b99).

Files with missing lines Patch % Lines
rust2go-common/src/common.rs 95.48% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #174      +/-   ##
==========================================
+ Coverage   82.84%   83.52%   +0.68%     
==========================================
  Files          18       18              
  Lines        3503     3672     +169     
==========================================
+ Hits         2902     3067     +165     
- Misses        601      605       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lirenjie95
lirenjie95 merged commit a49bc9f into master Aug 24, 2026
3 checks passed
@lirenjie95
lirenjie95 deleted the fix/172-type-alias branch August 24, 2026 08:32
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.

Using a type alias in a binding file crashes codegen

2 participants