Skip to content
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

Do not perform full expansion of #expect() when try or await is present. #790

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Sources/TestingMacros/Support/ConditionArgumentParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,6 @@ private func _exprFromOptionalChainedExpr(_ expr: some ExprSyntaxProtocol) -> (E
///
/// - Returns: An instance of ``Condition`` describing `expr`.
private func _parseCondition(from expr: FunctionCallExprSyntax, for macro: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) -> Condition {
// If the member function call involves the `try` or `await` keywords, assume
// we cannot expand it. This check cannot handle expressions like
// `try #expect(a.b(c))` where `b()` is throwing because the `try` keyword
// is outside the macro expansion. SEE: rdar://109470248
let containsTryOrAwait = expr.tokens(viewMode: .sourceAccurate).lazy
.map(\.tokenKind)
.contains { $0 == .keyword(.try) || $0 == .keyword(.await) }
if containsTryOrAwait {
return Condition(expression: expr)
}

// We do not support function calls with trailing closures because the
// transform required to forward them requires more information than is
// available solely from the syntax tree.
Expand Down Expand Up @@ -477,6 +466,22 @@ private func _parseCondition(negating expr: ExprSyntax, isParenthetical: Bool, f
///
/// - Returns: An instance of ``Condition`` describing `expr`.
private func _parseCondition(from expr: ExprSyntax, for macro: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) -> Condition {
// Handle closures with a single expression in them (e.g. { $0.foo() })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this up so that closure "expansion" (not really but sure why not) still happens. If we ever actually start doing fun things with closures, we'll probably need to move this back down?

if let closureExpr = expr.as(ClosureExprSyntax.self) {
return _parseCondition(from: closureExpr, for: macro, in: context)
}

// If the condition involves the `try` or `await` keywords, assume we cannot
// expand it. This check cannot handle expressions like
// `try #expect(a.b(c))` where `b()` is throwing because the `try` keyword is
// outside the macro expansion. SEE: rdar://109470248
let containsTryOrAwait = expr.tokens(viewMode: .sourceAccurate).lazy
.map(\.tokenKind)
.contains { $0 == .keyword(.try) || $0 == .keyword(.await) }
if containsTryOrAwait {
return Condition(expression: expr)
}

if let infixOperator = expr.as(InfixOperatorExprSyntax.self),
let op = infixOperator.operator.as(BinaryOperatorExprSyntax.self) {
return _parseCondition(from: expr, leftOperand: infixOperator.leftOperand, operator: op, rightOperand: infixOperator.rightOperand, for: macro, in: context)
Expand All @@ -489,11 +494,6 @@ private func _parseCondition(from expr: ExprSyntax, for macro: some Freestanding
return _parseCondition(from: asExpr, for: macro, in: context)
}

// Handle closures with a single expression in them (e.g. { $0.foo() })
if let closureExpr = expr.as(ClosureExprSyntax.self) {
return _parseCondition(from: closureExpr, for: macro, in: context)
}

// Handle function calls and member accesses.
if let functionCallExpr = expr.as(FunctionCallExprSyntax.self) {
return _parseCondition(from: functionCallExpr, for: macro, in: context)
Expand Down
9 changes: 9 additions & 0 deletions Tests/TestingTests/MiscellaneousTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ struct SendableTests: Sendable {

@Test(.hidden, arguments: FixtureData.stringReturningClosureArray)
func parameterizedAcceptingFunction(f: @Sendable () -> String) {}

@Test(.hidden) func throwKeywordInExpectation() throws {
grynspan marked this conversation as resolved.
Show resolved Hide resolved
#expect(UInt() == UInt(try #require(Int("0"))))
}

@Test(.hidden) func asyncKeywordInExpectation() async {
func f() async -> Int { 0 }
#expect(UInt() == UInt(await f()))
}
}

@Suite("Named Sendable test type", .hidden)
Expand Down