Add support for chained associated types and returned associated types#1773
Add support for chained associated types and returned associated types#1773
Conversation
| if let ReturnType::Type(_, ty) = &mut f.sig.output { | ||
| if let Some(resolved) = resolve_self_type(ty, &associated_types) { | ||
| **ty = resolved; | ||
| } | ||
| } |
There was a problem hiding this comment.
This is a (sorta unrelated) change Claude recommended. Calling it out explicitly to see if we intentionally didn't flatten function return values before for a reason I am missing.
There was a problem hiding this comment.
Pull request overview
This PR extends the Soroban SDK procedural macros to correctly flatten/resolve chained associated type aliases (e.g., type End = Self::Foo; type Foo = Self::Base;) when generating contract function signatures, and adds a regression test contract + snapshot to validate the behavior.
Changes:
- Enhance
flatten_associated_items_in_impl_fnsto iteratively resolve chainedSelf::AssociatedTypealiases and apply resolution to both function inputs and return types. - Add a new test contract (
associated_type_chained) covering chained associated type resolution, along with its snapshot and committed macro-expansion outputs. - Update
Cargo.lockto include the newly added test crate.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/associated_type_chained/src/lib.rs | New regression test contract exercising chained associated types in a #[contractimpl] trait impl. |
| tests/associated_type_chained/Cargo.toml | Adds the new test crate to the workspace (via tests/*) with appropriate SDK deps. |
| tests/associated_type_chained/test_snapshots/test/test_chained.1.json | Snapshot validating resulting ledger state after invoking the contract via generated client. |
| tests-expanded/test_associated_types_chained_wasm32v1-none.rs | Committed expanded output for the wasm build of the new test contract. |
| tests-expanded/test_associated_types_chained_tests.rs | Committed expanded output for the test build, exercising the generated client calls. |
| soroban-sdk-macros/src/syn_ext.rs | Implements iterative resolution for chained associated types and applies it to fn inputs + outputs. |
| Cargo.lock | Registers the new test_associated_types_chained package in the lockfile. |
You can also share your feedback on Copilot code review. Take the survey.
leighmcculloch
left a comment
There was a problem hiding this comment.
Will this change also make it work where you reference an associated type through an associated type trait?
|
Closing this PR in favor of just issuing compiler errors. There are a lot of edge cases with resolving supertrait associated types, and given this is not a requested feature, we should just make the constraint clear for now. |
What
Resolve chained associated types and return-type associated types in
#[contractimpl]macro expansion. Theflatten_associated_items_in_impl_fnsfunction now iteratively resolves associated type definitions that reference other associated types (e.g.,type End = Self::Foowheretype Foo = Self::Base) and substitutesSelf::*in function return types, not just input parameters.Why
Using chained associated types and returning associated types from interfaces is valid Rust syntax but resulted in unexpected compiler errors.
Known limitations
None