Skip to content

Commit fee6aa2

Browse files
authored
Resolves issue with endpoints not reporting errors when there's a custom closure (#355)
* Fixing issue with generated code not using the unimplemented default method. * Fixing all tests Not sure if DependencyClientTest is still necessary but keeping it to allow the maintainers to decide.
1 parent 4e6b6a8 commit fee6aa2

File tree

5 files changed

+18
-135
lines changed

5 files changed

+18
-135
lines changed

Sources/DependenciesMacrosPlugin/DependencyEndpointMacro.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ public enum DependencyEndpointMacro: AccessorMacro, PeerMacro {
3131
}
3232

3333
return [
34-
"""
35-
@storageRestrictions(initializes: _\(raw: identifier))
36-
init(initialValue) {
37-
_\(raw: identifier) = initialValue
38-
}
39-
""",
4034
"""
4135
get {
4236
_\(raw: identifier)

Tests/DependenciesMacrosPluginTests/DependencyClientMacroTests.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -816,10 +816,6 @@ final class DependencyClientMacroTests: BaseTestCase {
816816
struct Client {
817817
@available(iOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(macOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(tvOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(watchOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.")
818818
var fetch: (_ id: Int) throws -> String {
819-
@storageRestrictions(initializes: _fetch)
820-
init(initialValue) {
821-
_fetch = initialValue
822-
}
823819
get {
824820
_fetch
825821
}
@@ -864,10 +860,6 @@ final class DependencyClientMacroTests: BaseTestCase {
864860
struct Client {
865861
@available(iOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(macOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(tvOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.") @available(watchOS, deprecated: 9999, message: "This property has a method equivalent that is preferred for autocomplete via this deprecation. It is perfectly fine to use for overriding and accessing via '@Dependency'.")
866862
var fetch: (_ id: Int) throws -> String {
867-
@storageRestrictions(initializes: _fetch)
868-
init(initialValue) {
869-
_fetch = initialValue
870-
}
871863
get {
872864
_fetch
873865
}
@@ -910,10 +902,6 @@ final class DependencyClientMacroTests: BaseTestCase {
910902
#"""
911903
struct Client {
912904
var fetch: (Int) throws -> String {
913-
@storageRestrictions(initializes: _fetch)
914-
init(initialValue) {
915-
_fetch = initialValue
916-
}
917905
get {
918906
_fetch
919907
}

Tests/DependenciesMacrosPluginTests/DependencyClientTests.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@ final class DependencyClientTests: BaseTestCase {
1919
}
2020
}
2121

22-
func testSwiftBug() {
22+
func testUnimplementedWithNonThrowingEndpoint() {
2323
let client = ClientWithNonThrowingEndpoint()
2424

25-
// NB: This should cause a test failure but currently does not due to a Swift compiler bug:
26-
// https://github.com/apple/swift/issues/71070
27-
XCTAssertEqual(client.fetch(), 42)
25+
XCTExpectFailure {
26+
XCTAssertEqual(client.fetch(), 42)
27+
} issueMatcher: {
28+
$0.compactDescription == """
29+
failed - Unimplemented: \'ClientWithNonThrowingEndpoint.fetch\'
30+
"""
31+
}
2832

2933
XCTExpectFailure {
3034
XCTAssertEqual(client.fetchWithUnimplemented(), 42)
3135
} issueMatcher: {
36+
$0.compactDescription == """
37+
failed - Unimplemented: \'ClientWithNonThrowingEndpoint.fetchWithUnimplemented\'
38+
""" ||
3239
$0.compactDescription == """
3340
failed - Unimplemented …
3441

Tests/DependenciesMacrosPluginTests/DependencyEndpointMacroTests.swift

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
2424
#"""
2525
struct Client {
2626
var endpoint: () -> Void {
27-
@storageRestrictions(initializes: _endpoint)
28-
init(initialValue) {
29-
_endpoint = initialValue
30-
}
3127
get {
3228
_endpoint
3329
}
@@ -56,10 +52,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
5652
#"""
5753
struct Client {
5854
var endpoint: () -> Bool {
59-
@storageRestrictions(initializes: _endpoint)
60-
init(initialValue) {
61-
_endpoint = initialValue
62-
}
6355
get {
6456
_endpoint
6557
}
@@ -110,10 +102,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
110102
#"""
111103
struct Client {
112104
var endpoint: () -> Bool {
113-
@storageRestrictions(initializes: _endpoint)
114-
init(initialValue) {
115-
_endpoint = initialValue
116-
}
117105
get {
118106
_endpoint
119107
}
@@ -164,10 +152,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
164152
#"""
165153
struct Client {
166154
var endpoint: (Int, Bool, String) -> Bool {
167-
@storageRestrictions(initializes: _endpoint)
168-
init(initialValue) {
169-
_endpoint = initialValue
170-
}
171155
get {
172156
_endpoint
173157
}
@@ -197,10 +181,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
197181
#"""
198182
struct Client {
199183
var endpoint: () throws -> Bool {
200-
@storageRestrictions(initializes: _endpoint)
201-
init(initialValue) {
202-
_endpoint = initialValue
203-
}
204184
get {
205185
_endpoint
206186
}
@@ -230,10 +210,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
230210
#"""
231211
public struct ApiClient {
232212
public var apiRequest: @Sendable (ServerRoute.Api.Route) async throws -> (Data, URLResponse) {
233-
@storageRestrictions(initializes: _apiRequest)
234-
init(initialValue) {
235-
_apiRequest = initialValue
236-
}
237213
get {
238214
_apiRequest
239215
}
@@ -263,10 +239,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
263239
#"""
264240
struct Client {
265241
var endpoint: () -> () {
266-
@storageRestrictions(initializes: _endpoint)
267-
init(initialValue) {
268-
_endpoint = initialValue
269-
}
270242
get {
271243
_endpoint
272244
}
@@ -295,10 +267,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
295267
#"""
296268
struct Client {
297269
var endpoint: () -> Int? {
298-
@storageRestrictions(initializes: _endpoint)
299-
init(initialValue) {
300-
_endpoint = initialValue
301-
}
302270
get {
303271
_endpoint
304272
}
@@ -328,10 +296,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
328296
#"""
329297
struct Client {
330298
var endpoint: () -> Optional<Int> {
331-
@storageRestrictions(initializes: _endpoint)
332-
init(initialValue) {
333-
_endpoint = initialValue
334-
}
335299
get {
336300
_endpoint
337301
}
@@ -361,10 +325,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
361325
#"""
362326
struct Client {
363327
var endpoint: @Sendable (Int) -> Void {
364-
@storageRestrictions(initializes: _endpoint)
365-
init(initialValue) {
366-
_endpoint = initialValue
367-
}
368328
get {
369329
_endpoint
370330
}
@@ -393,10 +353,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
393353
#"""
394354
public struct Client {
395355
public var endpoint: @Sendable (String, _ id: Int, _ progress: Float) async -> Void {
396-
@storageRestrictions(initializes: _endpoint)
397-
init(initialValue) {
398-
_endpoint = initialValue
399-
}
400356
get {
401357
_endpoint
402358
}
@@ -430,10 +386,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
430386
#"""
431387
public struct Client {
432388
public var endpoint: @MainActor @Sendable (_ id: Int) async -> Void {
433-
@storageRestrictions(initializes: _endpoint)
434-
init(initialValue) {
435-
_endpoint = initialValue
436-
}
437389
get {
438390
_endpoint
439391
}
@@ -467,10 +419,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
467419
#"""
468420
public struct Client {
469421
public var endpoint: @Sendable (_ id: Int) async -> Void {
470-
@storageRestrictions(initializes: _endpoint)
471-
init(initialValue) {
472-
_endpoint = initialValue
473-
}
474422
get {
475423
_endpoint
476424
}
@@ -504,10 +452,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
504452
#"""
505453
struct Client {
506454
var endpoint: (_ id: Int) -> Void {
507-
@storageRestrictions(initializes: _endpoint)
508-
init(initialValue) {
509-
_endpoint = initialValue
510-
}
511455
get {
512456
_endpoint
513457
}
@@ -540,10 +484,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
540484
#"""
541485
struct Client {
542486
var endpoint: () -> Void {
543-
@storageRestrictions(initializes: _endpoint)
544-
init(initialValue) {
545-
_endpoint = initialValue
546-
}
547487
get {
548488
_endpoint
549489
}
@@ -596,10 +536,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
596536
#"""
597537
struct Client {
598538
var endpoint: (_ id: Int) -> Void {
599-
@storageRestrictions(initializes: _endpoint)
600-
init(initialValue) {
601-
_endpoint = initialValue
602-
}
603539
get {
604540
_endpoint
605541
}
@@ -649,10 +585,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
649585
} expansion: {
650586
#"""
651587
var `return`: () throws -> Int {
652-
@storageRestrictions(initializes: _return)
653-
init(initialValue) {
654-
_return = initialValue
655-
}
656588
get {
657589
_return
658590
}
@@ -678,10 +610,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
678610
} expansion: {
679611
#"""
680612
var `return`: (_ id: Int) throws -> Int {
681-
@storageRestrictions(initializes: _return)
682-
init(initialValue) {
683-
_return = initialValue
684-
}
685613
get {
686614
_return
687615
}
@@ -737,10 +665,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
737665
#"""
738666
struct Blah {
739667
public var doAThing: (_ value: Int) -> String {
740-
@storageRestrictions(initializes: _doAThing)
741-
init(initialValue) {
742-
_doAThing = initialValue
743-
}
744668
get {
745669
_doAThing
746670
}
@@ -776,10 +700,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
776700
#"""
777701
struct Blah {
778702
public var doAThing: (_ a: inout Int, _ b: Int, _ c: inout Bool) -> String {
779-
@storageRestrictions(initializes: _doAThing)
780-
init(initialValue) {
781-
_doAThing = initialValue
782-
}
783703
get {
784704
_doAThing
785705
}
@@ -813,10 +733,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
813733
#"""
814734
struct Foo {
815735
var bar: (_ a: @autoclosure () -> Int, _ b: () -> Int, _ c: @autoclosure () -> Int) -> Void {
816-
@storageRestrictions(initializes: _bar)
817-
init(initialValue) {
818-
_bar = initialValue
819-
}
820736
get {
821737
_bar
822738
}
@@ -891,10 +807,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
891807
#"""
892808
struct Blah {
893809
public var foo: () -> Void {
894-
@storageRestrictions(initializes: _foo)
895-
init(initialValue) {
896-
_foo = initialValue
897-
}
898810
get {
899811
_foo
900812
}
@@ -910,10 +822,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
910822
}()
911823
}
912824
public var bar: () -> String {
913-
@storageRestrictions(initializes: _bar)
914-
init(initialValue) {
915-
_bar = initialValue
916-
}
917825
get {
918826
_bar
919827
}
@@ -952,11 +860,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
952860
willSet {
953861
print("!")
954862
}
955-
@storageRestrictions(initializes: _foo)
956-
init(initialValue) {
957-
_foo = initialValue
958-
}
959-
960863
get {
961864
_foo
962865
}
@@ -991,10 +894,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
991894
#"""
992895
package struct Client {
993896
package var endpoint: () -> Void {
994-
@storageRestrictions(initializes: _endpoint)
995-
init(initialValue) {
996-
_endpoint = initialValue
997-
}
998897
get {
999898
_endpoint
1000899
}
@@ -1023,10 +922,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
1023922
#"""
1024923
package struct Client {
1025924
package var endpoint: (_ id: Int) -> Void {
1026-
@storageRestrictions(initializes: _endpoint)
1027-
init(initialValue) {
1028-
_endpoint = initialValue
1029-
}
1030925
get {
1031926
_endpoint
1032927
}
@@ -1059,10 +954,6 @@ final class DependencyEndpointMacroTests: BaseTestCase {
1059954
#"""
1060955
struct Client {
1061956
var endpoint: () -> Void // This is a comment {
1062-
@storageRestrictions(initializes: _endpoint)
1063-
init(initialValue) {
1064-
_endpoint = initialValue
1065-
}
1066957
get {
1067958
_endpoint
1068959
}

Tests/DependenciesMacrosPluginTests/DependencyEndpointTests.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
var endpoint: () -> Int = { 42 }
2727
}
2828
let client = Client()
29-
// NB: This invocation of 'endpoint' *should* fail, but it does not due to a bug in the
30-
// Swift compiler: https://github.com/apple/swift/issues/71070
31-
let output = client.endpoint()
32-
XCTAssert(output == 42)
29+
XCTExpectFailure {
30+
_ = client.endpoint()
31+
} issueMatcher: {
32+
$0.compactDescription == """
33+
failed - Unimplemented: 'Client.endpoint'
34+
"""
35+
}
3336
}
3437
#endif
3538
}

0 commit comments

Comments
 (0)