-
Notifications
You must be signed in to change notification settings - Fork 146
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 1 commit
1912d84
91213c6
c27dc5c
8d51562
51456e9
5d3e5d0
a5b1498
9bdd79d
3d6b072
90e47ed
78e75ae
5316889
542a027
ced014e
b64e134
04fa487
988f08f
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -171,7 +171,12 @@ struct AttributeInfo { | |||||
| if let testFunctionArguments { | ||||||
| arguments += testFunctionArguments.map { argument in | ||||||
| var copy = argument | ||||||
| copy.expression = .init(ClosureExprSyntax { argument.expression.trimmed }) | ||||||
| let argumentExpr = argument.expression.trimmed | ||||||
| if let contextualType = _contextualTypeForLiteralArgument(for: argumentExpr, among: testFunctionArguments) { | ||||||
| copy.expression = .init(ClosureExprSyntax { "\(argumentExpr) as \(raw: contextualType)" as ExprSyntax }) | ||||||
| } else { | ||||||
| copy.expression = .init(ClosureExprSyntax { argumentExpr }) | ||||||
| } | ||||||
| return copy | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -180,4 +185,38 @@ 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 a single array literal, | ||||||
| /// reconstruct the array type from the test function's parameters so the | ||||||
| /// literal retains enough contextual type information after lazy wrapping. | ||||||
| private func _contextualTypeForLiteralArgument( | ||||||
| for expression: ExprSyntax, | ||||||
| among testFunctionArguments: [Argument] | ||||||
| ) -> String? { | ||||||
|
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.
Suggested change
|
||||||
| guard let functionDecl = declaration.as(FunctionDeclSyntax.self) else { | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| let parameters = functionDecl.signature.parameterClause.parameters | ||||||
| guard !parameters.isEmpty else { | ||||||
|
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.
Suggested change
|
||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| if testFunctionArguments.count == 1, expression.is(ArrayExprSyntax.self) { | ||||||
|
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. It should be safe to generalize this so that the argument count only needs to match the parameter count. |
||||||
| 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 "[\(parameter.baseTypeName)]" | ||||||
|
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. Tokens from the original source need to be trimmed. |
||||||
| } | ||||||
| let elementType = parameters.map(\.baseTypeName).joined(separator: ", ") | ||||||
|
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. Can we construct an |
||||||
| return "[(\(elementType))]" | ||||||
| } | ||||||
|
|
||||||
| return nil | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify this part of the diff by modifying
argumentExprbefore creating the closure wrapper.