Skip to content

Handle composed and mixed associated type constraints #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

nolinmcfarland
Copy link
Contributor

📝 Summary

This pull request fixes a bug in MockedMacro.mockGenericParameterClause where associated types using composition (&) in their constraints weren’t handled correctly.

Previously, the @Mocked assumed all inherited types were IdentifierTypeSyntax nodes, which holds for comma-separated constraints (e.g. Hashable, Sendable). However, composed constraints (e.g. Hashable & Sendable) are represented in the syntax tree as CompositionTypeSyntax, which nests multiple IdentifierTypeSyntax elements. As a result, these weren’t being parsed, and the generated generic parameter clause was incomplete or incorrect.

To fix this, I've:

  • Updated mockGenericParameterClause to correctly extract constraints from both IdentifierTypeSyntax and nested CompositionTypeSyntax.
  • Added unit test coverage to catch composed constraints and mixed cases going forward.

🛠️ Type of Change

  • Bug fix (change that fixes an issue)
  • New feature (change that adds functionality)
  • Breaking change (bug fix or feature that is not backwards compatible)
  • Documentation (DocC, API docs, markdown files, templates, etc.)
  • Testing (new tests, updated tests, etc.)
  • Refactoring or code formatting (no logic changes)
  • Updating dependencies (Swift packages, Homebrew, etc.)
  • CI/CD (change to automated workflows)

🧪 How Has This Been Tested?

I've tested the @Mocked macro expansion when an associatedtype has constraints that are comma-separated, composed, or a mix of both. Please see screenshots of the expansion below.
I've also added unit tests to validate these scenarios going forward.

Constraint scenario @Mocked expansion
Comma-separated CommaSeparated
Composed Composed
Mixed Mixed

🔗 Related PRs or Issues

✅ Checklist

  • I have added relevant tests
  • I have verified all tests pass
  • I have formatted my code using SwiftFormat
  • I have updated documentation (if needed)
  • I have added the appropriate label to my PR
  • I have read the contributing guidelines
  • I agree to follow this project's Code of Conduct

@nolinmcfarland nolinmcfarland requested a review from ngimelliUW May 16, 2025 14:28
@nolinmcfarland nolinmcfarland self-assigned this May 16, 2025
@nolinmcfarland nolinmcfarland added the bug Something isn't working label May 16, 2025
@nolinmcfarland nolinmcfarland changed the title Bugfix/associated types with composed constraints Handle composed and mixed associated type constraints May 16, 2025
(commaSeparatedInheritedTypes + composedInheritedTypes)
.map(\.name.text)
.joined(separator: " & ")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Merging both comma-separated and composed inherited types allows us to handle mixed constraint cases correctly.

Copy link

codecov bot commented May 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.84%. Comparing base (b0db14b) to head (72192a6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #108      +/-   ##
==========================================
+ Coverage   68.78%   68.84%   +0.06%     
==========================================
  Files          71       71              
  Lines        4558     4567       +9     
==========================================
+ Hits         3135     3144       +9     
  Misses       1423     1423              

☔ View full report in Codecov by Sentry.
📢 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.

Copy link

@Yurssoft Yurssoft left a comment

Choose a reason for hiding this comment

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

Looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Generics syntax is incorrectly generated from protocols that have associated types with multiple conformances
2 participants