Skip to content

Improve parsing for associated type usage and add better errors for unsupported patterns#1780

Merged
mootz12 merged 8 commits intomainfrom
associated-type-errors
Mar 23, 2026
Merged

Improve parsing for associated type usage and add better errors for unsupported patterns#1780
mootz12 merged 8 commits intomainfrom
associated-type-errors

Conversation

@mootz12
Copy link
Copy Markdown
Contributor

@mootz12 mootz12 commented Mar 19, 2026

What

Flatten associated types for function return values as well as function inputs. Issue errors so the compiler can clearly warn users when the macro logic cannot resolve an associated type. Currently, associated types can only reference concrete types that are within the macros scope. Anything out of scope (like supertrait types not defined in the same impl block), will throw a compiler error.

Why

Returning associated types, chaining associated types, or using supertrait associated types resulted in no compiler errors but had unclear build errors.

For chained associated types and returning associated types, the macro has enough information to flatten these.

Closes #1779

Known limitations

None

Copilot AI review requested due to automatic review settings March 19, 2026 15:48
@mootz12 mootz12 changed the title Associated type errors Add errors for unsupported associated type usage Mar 19, 2026
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 improves #[contractimpl] macro handling of associated types by flattening Self::Assoc in function signatures (including return types) and producing clearer macro-time errors when an associated type cannot be resolved. It also adds a new regression test contract to validate associated-type return values.

Changes:

  • Update macro parsing (syn_ext.rs) to flatten Self::* in both function inputs and return types, and emit a syn::Error when unresolved associated types remain.
  • Add a new test contract (tests/associated_type_retval) plus snapshot output to cover associated-type return values.
  • Update generated expansion artifacts and workspace lockfile to include the new test crate.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
soroban-sdk-macros/src/syn_ext.rs Adds return-type flattening and explicit errors for unresolved Self::Assoc in impl fn signatures.
tests/associated_type_retval/src/lib.rs New regression test contract exercising associated types in inputs and return values.
tests/associated_type_retval/Cargo.toml Declares the new test crate.
tests/associated_type_retval/test_snapshots/test/test_associated_type_retval.1.json Snapshot for the new test’s observable ledger/storage behavior.
tests-expanded/test_associated_type_retval_wasm32v1-none.rs New expanded macro output for the wasm build of the test crate.
tests-expanded/test_associated_type_retval_tests.rs New expanded macro output for the native test build.
Cargo.lock Adds the new test crate to the lockfile.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread soroban-sdk-macros/src/syn_ext.rs Outdated
Comment thread soroban-sdk-macros/src/syn_ext.rs Outdated
@mootz12 mootz12 changed the title Add errors for unsupported associated type usage Improve parsing for associated type usage and add better errors for unsupported patterns Mar 19, 2026
@mootz12 mootz12 requested a review from Copilot March 19, 2026 20:19
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 enhances #[contractimpl] macro parsing so associated types (Self::Type) in function signatures are flattened more reliably for both parameters and return types, and emits clearer syn::Errors for unsupported associated-type patterns (e.g., qualified paths).

Changes:

  • Update macro parsing to recursively resolve Self::Assoc types within function input and return types, with explicit errors for unsupported patterns.
  • Add a new integration test contract covering chained associated type resolution in inputs and return values (plus snapshot output).
  • Add macro-level unit tests validating successful resolution and improved error reporting; update Cargo.lock for the new test crate.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
soroban-sdk-macros/src/syn_ext.rs Adds recursive associated-type resolution and introduces targeted parse-time errors + unit tests.
tests/associated_type_chained/src/lib.rs New test contract exercising associated types in inputs/returns (including nested generic wrappers).
tests/associated_type_chained/Cargo.toml Adds the new test crate to the workspace test suite.
tests/associated_type_chained/test_snapshots/test/test_associated_type_retval.1.json Snapshot for the new integration test.
tests-expanded/test_associated_type_chained_wasm32v1-none.rs Expanded-code artifact for the new test.
tests-expanded/test_associated_type_chained_tests.rs Expanded-tests artifact for the new test.
Cargo.lock Records the new test crate package entry.

Comment thread soroban-sdk-macros/src/syn_ext.rs Outdated
Comment thread soroban-sdk-macros/src/syn_ext.rs Outdated
Comment thread tests/associated_type_chained/src/lib.rs Outdated
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

Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.

Comment thread soroban-sdk-macros/src/syn_ext.rs
Comment thread tests/associated_type_chained/src/lib.rs
@mootz12 mootz12 requested review from a team and leighmcculloch March 20, 2026 00:08
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.

nice error handling using the spans 👍🏻

@mootz12 mootz12 merged commit e691ca4 into main Mar 23, 2026
192 of 194 checks passed
@mootz12 mootz12 deleted the associated-type-errors branch March 23, 2026 13:35
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