Skip to content

Commit

Permalink
Document swift compiler bug in @DependencyClient. (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrandonw committed Mar 4, 2024
1 parent 09e49dd commit f5f64da
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,25 @@ dependency from the implementation (see
<doc:LivePreviewTest#Separating-interface-and-implementation> for more information). But now there
is no need to maintain that code as it is automatically provided for you by the macro.

> Warning: Due to a [bug in the Swift compiler](https://github.com/apple/swift/issues/71070),
> endpoints that are not throwing will not emit a test failure when invoked. This applies to
> dependencies with endpoints like this:
>
> ```swift
> @DependencyClient
> struct NumberFetcher {
> var get: () async -> Int = { 42 }
> }
> ```
>
> The workaround is to invoke `XCTFail` directly in the closure:
>
> ```swift
> @DependencyClient
> struct NumberFetcher {
> var get: () async -> Int = { XCTFail("\(Self.self).get"); return 42 }
> }
> ```
[designing-deps]: https://www.pointfree.co/collections/dependencies
[xctest-dynamic-overlay-gh]: http://github.com/pointfreeco/xctest-dynamic-overlay
20 changes: 20 additions & 0 deletions Sources/DependenciesMacros/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@
/// and does not need to signify a real value. For example, if the endpoint returns a boolean, you
/// can return `false`, or if it returns an array, you can return `[]`.`
///
/// > Warning: Due to a [bug in the Swift compiler](https://github.com/apple/swift/issues/71070),
/// > endpoints that are not throwing will not emit a test failure when invoked. This applies to
/// > dependencies with endpoints like this:
/// >
/// > ```swift
/// > @DependencyClient
/// > struct NumberFetcher {
/// > var get: () async -> Int = { 42 }
/// > }
/// > ```
/// >
/// > The workaround is to invoke `XCTFail` directly in the closure:
/// >
/// > ```swift
/// > @DependencyClient
/// > struct NumberFetcher {
/// > var get: () async -> Int = { XCTFail("\(Self.self).get"); return 42 }
/// > }
/// > ```
///
/// [designing-dependencies]: https://swiftpackageindex.com/pointfreeco/swift-dependencies/main/documentation/dependencies/designingdependencies
/// [separating-interface-implementation]: https://swiftpackageindex.com/pointfreeco/swift-dependencies/main/documentation/dependencies/livepreviewtest#Separating-interface-and-implementation
@attached(member, names: named(init))
Expand Down

0 comments on commit f5f64da

Please sign in to comment.