square / Cleanse

Lightweight Swift Dependency Injection Framework
Other
1.78k stars 90 forks source link

Can't build Cleanse tests in Xcode 12 #171

Closed harleyjcooper closed 3 years ago

harleyjcooper commented 3 years ago

When building the Cleanse project in Xcode 12, it fails with a number of Ambiguous use of 'to' failures in MemoryManagementTests.swift and CleanseTests.swift.

Compilation and unit tests succeed by making the return type explicit:

diff --git a/CleanseTests/CleanseTests.swift b/CleanseTests/CleanseTests.swift
index c4e1649..7a0b757 100644
--- a/CleanseTests/CleanseTests.swift
+++ b/CleanseTests/CleanseTests.swift
@@ -90,8 +90,8 @@ struct APIComponent : RootComponent {
 struct BurgerModule : Module {
     static func configure(binder: Binder<Singleton>) {
         binder.bind().to(factory: Burger.init)
-        binder.bind().to { return Cheese.cheddar }
-        binder.bind().to { Roll.ciabatta }
+        binder.bind().to { return Cheese.cheddar } as BindingReceipt<Cheese>
+        binder.bind().to { Roll.ciabatta } as BindingReceipt<Roll>

         var singletonCountTest = 1
         binder
diff --git a/CleanseTests/MemoryManagementTests.swift b/CleanseTests/MemoryManagementTests.swift
index a2cf8b3..ea10e2e 100644
--- a/CleanseTests/MemoryManagementTests.swift
+++ b/CleanseTests/MemoryManagementTests.swift
@@ -82,9 +82,9 @@ class MemoryManagementTests: XCTestCase {
             binder.bind().sharedInScope().to(factory: Single2.init)
             binder.bind().sharedInScope().to(factory: SingleStruct1.init)

-            binder.bind().intoCollection().sharedInScope().to { SingleCollectionElement(value: 3) }
-            binder.bind().intoCollection().sharedInScope().to { SingleCollectionElement(value: 4) }
-            binder.bind().intoCollection().sharedInScope().to { SingleCollectionElement(value: 5) }
+            binder.bind().intoCollection().sharedInScope().to { SingleCollectionElement(value: 3) } as BindingReceipt<SingleCollectionElement>
+            binder.bind().intoCollection().sharedInScope().to { SingleCollectionElement(value: 4) } as BindingReceipt<SingleCollectionElement>
+            binder.bind().intoCollection().sharedInScope().to { SingleCollectionElement(value: 5) } as BindingReceipt<SingleCollectionElement>
         }
     }

but it's more likely indicative of some underlying issue.

s-hocking commented 3 years ago

+1

Another workaround is to use the parameter labels, e.g.:

binder.bind().to(value: Cheese.cheddar)
binder.bind().to(value: Roll.ciabatta)
sebastianv1 commented 3 years ago

@harleyjcooper Seems to be an interesting issue surround the 0-arity factory and value functions. Opened a topic on the swift forums: https://forums.swift.org/t/ambiguous-use-of-function-with-trailing-closures/41144

@s-hocking 's workaround is the correct way to fix for now. The Cleanse library itself successfully compiles, I'll go ahead and fix the tests to disambiguate. I won't make any API changes for now and open a new issue to help any future consumers who run into a similar problems until we can figure out if this new compiler change is "correct" in marking these two functions as ambiguous with each other.

sebastianv1 commented 3 years ago

Merged updating the repo to compile & test using XC 12. Will open another issue to address the ambiguous function issue.