-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any.Type issue with @Test #76637
Comments
Looks like it can be workaround by explicitly mark Hope the compiler/swift-testing macro can infer it automatically here. @Test(
arguments: [
(type: Int.self as Any.Type, nominalName: "Int"),
(type: String.self as Any.Type, nominalName: "String"),
]
)
func name(type: Any.Type, nominalName: String) |
Type inference here is at the compiler level. Swift Testing receives an array of |
Since we already have the parameter signature here - (Any.Type, String), can we add some explicit type inference hint to the Test macro's arguments variable type? (the variable c in the following example) let a = (Int.self, "A") // Inferred as (Int.Type, String)
let b1 = [
(Int.self, "Int"),
(String.self, "String"),
] // Inferred as [Any] with an error "Heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional"
let b2: [Any] = [
(Int.self, "Int"),
(String.self, "String"),
]
let c: [(Any.Type, String)] = [
(Int.self, "Int"),
(String.self, "String"),
] |
Type inference occurs before the |
Find a solution here - use @Test(
arguments: [
(type: Int.self, nominalName: "Int"),
(type: String.self, nominalName: "String"),
] as [(Any.Type, String)]
)
func name1(type: Any.Type, nominalName: String) {
#expect(API.name(type) == nominalName)
} |
Although the original reporter of this issue identified a workaround (explicitly typing the expression using |
This is not something we can do in Swift Testing today because the failure happens before macro expansion (because the type inference on the array resolves to |
There was some discussion at one point about being able to reference the enclosing @attached(peer)
@_documentation(visibility: private)
public macro Test<C, each ParameterType>(
_ traits: any TestTrait...,
arguments collection: C
) = #externalMacro(module: "TestingMacros", type: "TestDeclarationMacro")
where C: Collection, C.Element = (repeat each ParameterType) and if you attached it to a function like this:
This would all need language design and I don't know if it's worth doing, but I think it could work. |
Description
I'm using name2 pattern at first. But then I thought I should refactor to use name1 pattern. But it will give me a strange compiler error.
Is this an expected unsupported feature like swiftlang/swift-testing#202 or a bug here.
Expected behavior
Compile and test successfully.
Actual behavior
Compile error on language mode v5. (On v6 it is Any is not conform to Sendable)
Steps to reproduce
See description field.
Or use the following demokit package
DemoKit.zip
swift-testing version/commit hash
Xcode 16.0.0
Swift & OS version (output of
swift --version ; uname -a
)Swift 6 compiler with Swift 5 language mode
The text was updated successfully, but these errors were encountered: