Fix autoclosure forwarding in generated mock bodies#146
Merged
Conversation
Add try/await wrapping when calling autoclosure parameters, rename isInoutParameter/isConsumingParameter to isInout/isConsuming per API Design Guidelines, and fix AutoClosures.swift client verification. Co-Authored-By: Claude Code <noreply@anthropic.com>
9afc588 to
77491dd
Compare
Add async, throwing, and async-throwing variants of the void and non-void autoclosure tests, and update AutoClosures.swift with all eight corresponding methods. Tests and methods are ordered: regular, async, throwing, async-throwing; with void and non-void autoclosure return type variants within each group. Co-Authored-By: Claude Code <noreply@anthropic.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
Use 4-space indentation for enum body members to match BasicFormat output; tabs are only present on explicitly-set generic parameter trivia. Co-Authored-By: Claude Code <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #146 +/- ##
==========================================
+ Coverage 78.42% 80.12% +1.69%
==========================================
Files 73 73
Lines 4055 4120 +65
==========================================
+ Hits 3180 3301 +121
+ Misses 875 819 -56 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Code <noreply@anthropic.com>
pjtnt11
approved these changes
Jun 25, 2026
Without compilationCondition: .none, generated mocks are wrapped in #if SWIFT_MOCKING_ENABLED and not compiled during a regular build, defeating the purpose of the client as a compilation verification target. Co-Authored-By: Claude Code <noreply@anthropic.com>
graycampbell
commented
Jun 25, 2026
| /// Mocked's handling of attributed types. For temporary testing of Mocked's | ||
| /// expansion, use the `Playground` protocol in `main.swift`. | ||
| @Mocked | ||
| @Mocked(compilationCondition: .none) |
Collaborator
Author
There was a problem hiding this comment.
compilationCondition: .none needs to be added to every @Mocked usage in MockingClient. Without it, the default .swiftMockingEnabled condition wraps each generated mock in #if SWIFT_MOCKING_ENABLED, so the mock bodies are never compiled during a regular swift build, defeating the purpose of the client as compilation verification.
mitchtreece
approved these changes
Jun 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Summary
When a mocked protocol method has an
@autoclosureparameter, the generated mock body previously passed the closure directly torecordInputand the invoke closure. This caused a compile error: "Add () to forward@autoclosureparameter". The fix evaluates the autoclosure into a localletbinding before use, wrapping the call intryand/orawaitas required by the closure's effect specifiers.Also renames
isInoutParameter→isInoutandisConsumingParameter→isConsuminginMockedMethodMacro+BodyMacro.swiftto better follow Swift API Design Guidelines (omit needless words — theParametersuffix was redundant given the argument label).compilationCondition: .noneis added to every@Mockedusage inMockingClient. Without it, the default.swiftMockingEnabledcondition wraps each generated mock in#if SWIFT_MOCKING_ENABLED, so the mock bodies are never compiled during a regularswift build— defeating the purpose of the client as a compilation verification target.🛠️ Type of Change
🧪 How Has This Been Tested?
Eight macro expansion tests in
MockedMethod_AutoClosureTests, covering all four effect-specifier variants × two autoclosure return type variants (void and non-void):autoClosureWithVoidReturnType() -> VoidautoClosureWithNonVoidReturnType() -> IntasyncAutoClosureWithVoidReturnType() async -> VoidasyncasyncAutoClosureWithNonVoidReturnType() async -> IntasyncthrowingAutoClosureWithVoidReturnType() throws -> VoidthrowsthrowingAutoClosureWithNonVoidReturnType() throws -> IntthrowsasyncThrowingAutoClosureWithVoidReturnType() async throws -> Voidasync throwsasyncThrowingAutoClosureWithNonVoidReturnType() async throws -> Intasync throwsEach test verifies the correct
let autoClosure = [try] [await] autoClosure()binding is generated, the appropriate effect specifiers appear on the invoke call, and the peer type (MockVoidParameterized[Async][Throwing]Method) and closure type are correct.AutoClosures.swiftinMockingClientis updated with all eight corresponding methods, serving as an end-to-end compilation check.🔗 Related PRs or Issues
Fixes #134
✅ Checklist
SwiftFormat