-
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
Open
ojun9
wants to merge
17
commits into
swiftlang:main
Choose a base branch
from
ojun9:fix/macro-literal-contextual-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−6
Open
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1912d84
Preserve contextual type for literal arguments wrapped by @Test macro
ojun9 91213c6
review feedback
ojun9 c27dc5c
review feedback
ojun9 8d51562
review feedback
ojun9 51456e9
Merge branch 'main' into fix/macro-literal-contextual-type
ojun9 5d3e5d0
Untrim baseType
ojun9 a5b1498
Remove redundant trimming
ojun9 9bdd79d
Handle dictionary literal contextual types
ojun9 3d6b072
Add literal contextual typing fixtures
ojun9 90e47ed
Merge branch 'main' into fix/macro-literal-contextual-type
ojun9 78e75ae
Merge branch 'main' into fix/macro-literal-contextual-type
ojun9 5316889
Update Sources/TestingMacros/Support/AttributeDiscovery.swift
ojun9 542a027
Update Tests/TestingTests/Test.Case.ArgumentTests.swift
ojun9 ced014e
Update Tests/TestingTests/Test.Case.ArgumentTests.swift
ojun9 b64e134
cast contextual @Test arguments
ojun9 04fa487
qualify KeyValuePairs with Swift
ojun9 988f08f
inline parameter base type return
ojun9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -169,9 +169,24 @@ 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 | ||
| ) | ||
| ) | ||
| } | ||
| let lazyExpression = expr.trimmed | ||
|
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. Already trimmed by this point. (New nodes created programmatically are de facto trimmed if you don't explicitly specify trivia). |
||
| copy.expression = .init(ClosureExprSyntax { lazyExpression }) | ||
| return copy | ||
| } | ||
| } | ||
|
|
@@ -180,4 +195,70 @@ 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 expression.is(ArrayExprSyntax.self) else { | ||
| return nil | ||
| } | ||
|
|
||
| guard let functionDecl = declaration.as(FunctionDeclSyntax.self) else { | ||
| return nil | ||
| } | ||
|
|
||
| let parameters = Array(functionDecl.signature.parameterClause.parameters) | ||
| if parameters.isEmpty { | ||
| return nil | ||
| } | ||
|
|
||
| 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)) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Avoid trimming both here and in the call sites as it's somewhat expensive. I would leave it untrimmed here so that it can still be used for diagnostic attribution, but either choice is fine.