Skip to content

Add support for chained associated types and returned associated types#1773

Closed
mootz12 wants to merge 2 commits intomainfrom
chained-associated-types
Closed

Add support for chained associated types and returned associated types#1773
mootz12 wants to merge 2 commits intomainfrom
chained-associated-types

Conversation

@mootz12
Copy link
Copy Markdown
Contributor

@mootz12 mootz12 commented Mar 17, 2026

What

Resolve chained associated types and return-type associated types in #[contractimpl] macro expansion. The flatten_associated_items_in_impl_fns function now iteratively resolves associated type definitions that reference other associated types (e.g., type End = Self::Foo where type Foo = Self::Base) and substitutes Self::* 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

Copilot AI review requested due to automatic review settings March 17, 2026 18:57
Comment on lines +350 to +354
if let ReturnType::Type(_, ty) = &mut f.sig.output {
if let Some(resolved) = resolve_self_type(ty, &associated_types) {
**ty = resolved;
}
}
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.

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_fns to iteratively resolve chained Self::AssociatedType aliases 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.lock to 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.

Copy link
Copy Markdown
Member

@leighmcculloch leighmcculloch left a comment

Choose a reason for hiding this comment

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

Will this change also make it work where you reference an associated type through an associated type trait?

@mootz12
Copy link
Copy Markdown
Contributor Author

mootz12 commented Mar 19, 2026

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.

@mootz12 mootz12 closed this Mar 19, 2026
@mootz12 mootz12 deleted the chained-associated-types branch March 19, 2026 15:41
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.

#[contractimpl] macro: associated type flattening is one-pass and non-recursive

3 participants