-
Notifications
You must be signed in to change notification settings - Fork 80
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
Value not shown in error message when an expectation condition has effects (try/await) #162
Comments
This is expected and is a constraint of macros in Swift. The presence of the await keyword signals to the compiler that some subexpression of the expectation is asynchronous, but it is impossible to know exactly which subexpression from the AST alone. This means it is not possible to correctly decompose the expression as we can with synchronous, non-throwing ones. |
@grynspan Thanks! If I understand the issue correctly, for a test case like @Test func testAsyncExpectation() {
func value() async -> Int { 1 }
#expect(await value() == 2)
} the Can't we work around this in private func _parseCondition by not just looking for
Also, how is this issue different from matching a |
The same issue exists for a Imagine we add support for an This means we'd need additional overloads of every The expanded form of __checkBinaryOperation(
await (x, Testing.__requiringAwait).0,
{ await $0 == $1() },
await (y, Testing.__requiringAwait).0,
...
).__expected() And would have at least 4 and at most 6 suspension points when the original expression had as few as 1. |
Now, it may be possible to simplify that expansion a bit given that the outermost call to |
Thank you for the detailed explanation. I understand the tradeoffs now.
This seems to be the only viable solution to the issue. |
The issue can be avoided by extracting the async subexpression out into a separate expression: let y = await foo()
#expect(x == y) So I'd steer developers toward that solution as preferable. |
Reopening. Now that we have |
Blocked by swiftlang/swift#76930. |
… present. For reasons that have been documented at length (see #162), we aren't able to correctly expand conditions on `#expect()` or `#require()` that have effects (`try` or `await`.) We aren't currently detecting all possible patterns that expand incorrectly. This PR causes us to back out of the full expansion if the `try` or `await` keyword is present _anywhere_ inside an expectation condition expression. Resolves #783.
… present. (#790) For reasons that have been documented at length (see #162), we aren't able to correctly expand conditions on `#expect()` or `#require()` that have effects (`try` or `await`.) We aren't currently detecting all possible patterns that expand incorrectly. This PR causes us to back out of the full expansion if the `try` or `await` keyword is present _anywhere_ inside an expectation condition expression. Resolves #783. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
Tracked internally as rdar://137583415. |
Description
#expect
works differently whenawait
keyword is inside its body.await #expect(expr)
shows a breakdown of the actual value, while#expect(await expr)
does not. I would like to know why the expectation failed even if the result is computed from an async expression.Expected behavior
#expect(await Array(stream) == [1,2])
throws the errortestAll(): Expectation failed: (Array(stream) → [1, 2, 3]) == ([1,2] → [1, 2])
Actual behavior
#expect(await Array(stream) == [1,2])
throws the errortestAll(): Expectation failed: await Array(stream) == [1,2]
Steps to reproduce
No response
swift-testing version/commit hash
3fa4ea0
Swift & OS version (output of
swift --version && uname -a
)swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0
Darwin MBP 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000 arm64
The text was updated successfully, but these errors were encountered: