Improve parsing for associated type usage and add better errors for unsupported patterns#1780
Improve parsing for associated type usage and add better errors for unsupported patterns#1780
Conversation
There was a problem hiding this comment.
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 flattenSelf::*in both function inputs and return types, and emit asyn::Errorwhen 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.
There was a problem hiding this comment.
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::Assoctypes 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.lockfor 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. |
leighmcculloch
left a comment
There was a problem hiding this comment.
nice error handling using the spans 👍🏻
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