-
Notifications
You must be signed in to change notification settings - Fork 145
Preserve contextual type for literal arguments: expressions in @Test
#1627
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
base: main
Are you sure you want to change the base?
Changes from all commits
1912d84
91213c6
c27dc5c
8d51562
51456e9
5d3e5d0
a5b1498
9bdd79d
3d6b072
90e47ed
78e75ae
5316889
542a027
ced014e
b64e134
04fa487
988f08f
7acf321
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -169,9 +169,23 @@ struct AttributeInfo { | |
| // If there are any parameterized test function arguments, wrap each in a | ||
| // closure so they may be evaluated lazily at runtime. | ||
| if let testFunctionArguments { | ||
| arguments += testFunctionArguments.map { argument in | ||
| arguments += testFunctionArguments.enumerated().map { index, argument in | ||
| var copy = argument | ||
| copy.expression = .init(ClosureExprSyntax { argument.expression.trimmed }) | ||
| var expr = copy.expression.trimmed | ||
| if let contextualType = _contextualTypeForLiteralArgument( | ||
| at: index, | ||
| for: expr, | ||
| among: testFunctionArguments | ||
| ) { | ||
| expr = ExprSyntax( | ||
| AsExprSyntax( | ||
| expression: expr, | ||
| asKeyword: .keyword(.as, leadingTrivia: .space, trailingTrivia: .space), | ||
| type: contextualType.trimmed | ||
| ) | ||
| ) | ||
| } | ||
| copy.expression = .init(ClosureExprSyntax { expr }) | ||
| return copy | ||
| } | ||
| } | ||
|
|
@@ -180,4 +194,83 @@ struct AttributeInfo { | |
|
|
||
| return LabeledExprListSyntax(arguments) | ||
| } | ||
|
|
||
| /// The contextual type to explicitly apply to a literal `arguments:` | ||
| /// expression after it is wrapped in a closure for lazy evaluation. | ||
| /// | ||
|
ojun9 marked this conversation as resolved.
|
||
| /// Parameterized `@Test` declarations are modeled in terms of the collection | ||
| /// type supplied to the macro, but macro expansion only sees source syntax. | ||
| /// When the `arguments:` parameter is supplied as an array literal, derive | ||
| /// the corresponding array type from the test function's parameters so the | ||
| /// literal retains enough contextual type information after lazy wrapping. | ||
| /// | ||
| /// This applies to both the single-collection form and the overloads where | ||
| /// each `arguments:` expression corresponds directly to one parameter. | ||
| /// | ||
| /// - Parameters: | ||
| /// - index: The position of `expression` within `testFunctionArguments`. | ||
| /// - expression: The argument expression being wrapped for lazy evaluation. | ||
| /// - testFunctionArguments: The full list of argument expressions supplied | ||
| /// to the parameterized `@Test`. | ||
| /// | ||
| /// - Returns: The array type to apply to `expression`, or `nil` if no | ||
| /// contextual type reconstruction is needed. | ||
| private func _contextualTypeForLiteralArgument( | ||
| at index: Int, | ||
| for expression: ExprSyntax, | ||
| among testFunctionArguments: [Argument] | ||
| ) -> TypeSyntax? { | ||
| guard let functionDecl = declaration.as(FunctionDeclSyntax.self) else { | ||
| return nil | ||
| } | ||
|
|
||
| let parameters = Array(functionDecl.signature.parameterClause.parameters) | ||
| if parameters.isEmpty { | ||
| return nil | ||
| } | ||
|
|
||
| if expression.is(ArrayExprSyntax.self) { | ||
| if testFunctionArguments.count == parameters.count { | ||
| let parameter = parameters[index] | ||
| return TypeSyntax( | ||
| ArrayTypeSyntax(element: parameter.baseType.trimmed) | ||
| ) | ||
| } | ||
|
|
||
| if testFunctionArguments.count == 1 { | ||
| if parameters.count == 1, let parameter = parameters.first { | ||
| // A single-parameter test expects collection elements of the parameter | ||
| // type itself, not tuple-shaped elements. | ||
| return TypeSyntax( | ||
| ArrayTypeSyntax(element: parameter.baseType.trimmed) | ||
| ) | ||
| } | ||
| let elementType = TypeSyntax( | ||
| TupleTypeSyntax(elements: TupleTypeElementListSyntax { | ||
| for parameter in parameters { | ||
| TupleTypeElementSyntax(type: parameter.baseType.trimmed) | ||
| } | ||
| }) | ||
| ) | ||
| return TypeSyntax(ArrayTypeSyntax(element: elementType)) | ||
| } | ||
| } else if expression.is(DictionaryExprSyntax.self) { | ||
| if testFunctionArguments.count == 1, parameters.count == 2 { | ||
| return TypeSyntax( | ||
| MemberTypeSyntax( | ||
| baseType: IdentifierTypeSyntax(name: .identifier("Swift")), | ||
| name: .identifier("KeyValuePairs"), | ||
|
grynspan marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised to see
That changes the semantics some, though: elements with equal keys will run the test multiple times, and the order we dispatch test cases in will be stable, whereas with a dictionary the order is nondeterministic due to hashing. Is switching to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was my suggestion, yes.
Equal keys crash outright with a dictionary when it is initialized.
This change is more consistent with how we treat arrays, yes.
It isn't strictly necessary, but the only user-observable change is that the random order of elements is no longer random (which we never really intended it to be anyway). |
||
| genericArgumentClause: GenericArgumentClauseSyntax( | ||
| arguments: GenericArgumentListSyntax { | ||
| GenericArgumentSyntax(argument: .type(parameters[0].baseType.trimmed)) | ||
| GenericArgumentSyntax(argument: .type(parameters[1].baseType.trimmed)) | ||
| } | ||
| ) | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,11 +165,24 @@ struct ParameterizedTests { | |
| @Test(.hidden, arguments: [("value", 123)]) | ||
| func one2TupleParameter(x: (String, Int)) {} | ||
|
|
||
| @Test(.hidden, arguments: [ | ||
| (nil, 123), | ||
| ("value1", nil), | ||
| ("value2", nil), | ||
| ] as [(String?, Int?)]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this test, and the other new one below, include an explicit I just pulled the branch locally, and when I try this example mentioned in the PR description without an @Test(arguments: [
(nil, 2), // ❌ error: 'nil' cannot initialize specified type 'String'
("c", nil),
("d", nil)
])
func f(s: String?, i: Int?) {}If the outcome of this PR is that a user still needs to either (a) use an explicit |
||
| func contextualArrayLiteral(x: String?, y: Int?) {} | ||
|
|
||
| @Test(.hidden, arguments: ["value": 123]) | ||
| func twoDictionaryElementParameters(x: String, y: Int) {} | ||
|
|
||
| @Test(.hidden, arguments: ["value": 123]) | ||
| func oneDictionaryElementTupleParameter(x: (key: String, value: Int)) {} | ||
|
|
||
| @Test(.hidden, arguments: [ | ||
| "value1": nil, | ||
| "value2": 123, | ||
| ] as KeyValuePairs<String, Int?>) | ||
| func contextualDictionaryLiteral(key: String, value: Int?) {} | ||
|
|
||
| @Test(.disabled(), arguments: [1, 2, 3]) func disabled(x: Int) {} | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.