[3/x] Improve compile-time checks for Swift mocks #273
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.
Stack
๐ #275 [5/x] Clean up Synchronized wrapper
๐ #274 [4/x] Fix nested optional property and Obj-C types
๐ #273 โ [3/x] Improve compile-time checks for Swift mocks
๐ #272 [2/x] Remove Obj-C based nil check
๐ #271 [1/x] Add Algolia DocSearch to the documentation
Overview
The Objective-C overloads introduced in 0.18 makes it difficult for the Swift compiler to choose the correct branch when type checking
given
expressions. This is exacerbated with the stubbing operator which can provide incorrect constraints on the return type, preventing implicit bridging of optionals and Foundation types.Additionally, closure-based stubs on Swift methods arenโt protected against changes to the function signature and silently fall back to use Objective-C wildcard stubbing.
Although the issues likely wonโt surface frequently, they are very big foot guns. A few non-mutually exclusive options considered:
given
andgivenDynamic
. This is a source-incompatible change and makes the API less ergonomic. In addition, the dynamic stubbing path is not just used for Objective-C mocks, but also for the Swift property syntaxgiven(myMock.property)
.given
. Due to how the compiler picks overloads, the new function can only be annotated with adeprecated
orobsoleted
warning, but notunavailable
error.givenSwift
to surface compiler errors or disambiguate the type checking when using the stubbing operator.The best options are to pair (2) and (3), with the intention that
givenSwift
is only used for debugging warnings emitted by (2) or in cases where itโs more ergonomic to not provide explicit type annotations (e.g. for complex closure stubs).Test Plan
MockingbirdTests