Skip to content

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

@YOCKOW

Description

@YOCKOW

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

Metadata

Metadata

Assignees

Labels

bug🪲 Something isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions