swift-dependencies icon indicating copy to clipboard operation
swift-dependencies copied to clipboard

When using `@DependencyClient` macro, closures with mandatory return don't use the unimplemented version on tests

Open gpambrozio opened this issue 1 year ago • 1 comments

Description

When using @DependencyClient macro in a client that has closures with mandatory returns, when using these closures in a test without overriding them we don't get errors about the method not being implemented.

According to this message from Brandon this is related to a swift bug. Given that I'd suggest to at least add a note to the documentation mentioning that, instead of having a closure such as { _ in true }, we unfortunately still have to use { _ in unimplemented(placeholder: true) }

See also this Slack thread

Checklist

  • [X] I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
  • [X] If possible, I've reproduced the issue using the main branch of this package.
  • [X] This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

The test shown should have 2 errors.

Actual behavior

The test only produces one error.

Steps to reproduce

Simple Client:

import Dependencies
import DependenciesMacros

@DependencyClient
struct SimpleClient {
    var noReturn: () -> Void
    var withBoolReturn: () -> Bool = { true }
}

extension SimpleClient: TestDependencyKey {
    public static let testValue = SimpleClient()
}

extension DependencyValues {
    var simpleClient: SimpleClient {
        get { self[SimpleClient.self] }
        set { self[SimpleClient.self] = newValue }
    }
}

This test should have 2 failures but only shows one:

import Dependencies
import XCTest
@testable import TCATest

final class TCATestTests: XCTestCase {
    func testExample() throws {
        @Dependency(\.simpleClient) var simpleClient
        _ = simpleClient.withBoolReturn()
        simpleClient.noReturn()
    }
}

Dependencies version information

1.2.1

Destination operating system

iOS

Xcode version information

15.2

Swift Compiler version information

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0

gpambrozio avatar Feb 09 '24 20:02 gpambrozio

Apparently the swift team is saying that this is not a swift bug and have a suggestion for a change in the macro. I'm taking a look in the code now and will submit a pr if I can figure out the fix. Not very experienced with macros so might be easier for you to tackle but I'll try.

gpambrozio avatar Apr 05 '25 19:04 gpambrozio

@mbrandonw @stephencelis This can be closed now, right?

Muhammed9991 avatar Jun 13 '25 13:06 Muhammed9991

@Muhammed9991 Yup, lost track of it, thanks 😄

stephencelis avatar Jul 16 '25 18:07 stephencelis