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

#expect doesn't expect that there may be a sub-expression that has try on the right side of binary operator. #783

Open
YOCKOW opened this issue Oct 25, 2024 · 2 comments · May be fixed by #790
Assignees
Labels
bug Something isn't working
Milestone

Comments

@YOCKOW
Copy link
Member

YOCKOW commented Oct 25, 2024

Description

Although we can't put try on the right side of ==, the right expression can contain a sub-expression that has try:

func nonThrowingFunc() -> Int { 0 }
func throwingFunc() throws -> Int { 0 }
func anotherNonThrowingFunc(_ integer: Int) -> Int { integer }

print(nonThrowingFunc() == try throwingFunc()) // ❌ error: 'try' cannot appear to the right of a non-assignment operator
print(nonThrowingFunc() == anotherNonThrowingFunc(try throwingFunc())) // ✅ OK

However, #expect seems not to expect that there may be a try sub-expression on the right side of ==:

import Testing

struct Foo: Equatable {
  init() {}
  init(_ integer: Int) {}
}

@Test func myTest() throws {
  #expect(try #require(Int("0")) == 0) // ✅ OK
  #expect((Foo() == Foo(try #require(Int("0")))) == true) // ✅ OK
  #expect(Foo() == Foo(try #require(Int("0")))) // ❌ error: call can throw, but it is executed in a non-throwing autoclosure
}

Expanded function named __checkBinaryOperation doesn't have an @autoclosure that throws.

Expected behavior

No error at compile time.

Actual behavior

Described above.

Steps to reproduce

Try to compile the code above.

swift-testing version/commit hash

Testing Library Version: 94 (arm64e-apple-macos13.0)

Swift & OS version (output of swift --version ; uname -a)

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx15.0
Darwin yockow.local 24.0.0 Darwin Kernel Version 24.0.0: Tue Sep 24 23:39:07 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T6000 arm64

@YOCKOW YOCKOW added the bug Something isn't working label Oct 25, 2024
@grynspan
Copy link
Contributor

From syntax alone, we cannot tell try is needed here.

@grynspan grynspan marked this as a duplicate of #162 Oct 25, 2024
@grynspan grynspan closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2024
@grynspan
Copy link
Contributor

Oh hold on. We should be opting out here. I misread—reopening.

@grynspan grynspan reopened this Oct 26, 2024
@grynspan grynspan self-assigned this Oct 26, 2024
@grynspan grynspan added this to the Swift 6.1 milestone Oct 26, 2024
grynspan added a commit that referenced this issue Oct 30, 2024
… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants