Skip to content

Conversation

@stmontgomery
Copy link
Contributor

This changes #expect(throws:) and similar macros so that their closure parameters accept any type instead of Void specifically.

Motivation:

This makes testing trivial throwing expressions of non-Void types easier, by not requiring syntax to discard the return value. For example:

func mightThrowXOrReturnY() throws -> Y { ... }
#expect(throws: X.self) {
  try mightThrowXOrReturnY() // ⚠️ Result of call to 'mightThrowXOrReturnY()' is unused
}

Modifications:

  • Change closure parameters of relevant macro-related declarations to use generic R type instead of Void.
  • Change implementation of relevant __check... functions to include a description of return value.
  • Add new unit tests.
  • Update documentation to mention that return values are always discarded.

Result:

The changes in this PR mean the warning above will no longer be emitted by the compiler. These changes also allow printing a description of the return value if the expectation fails and the expression does not throw an error, providing a more detailed and actionable failure message.

@stmontgomery
Copy link
Contributor Author

@swift-ci please test

@stmontgomery
Copy link
Contributor Author

@swift-ci please test

@stmontgomery
Copy link
Contributor Author

@swift-ci please test Linux

@stmontgomery stmontgomery merged commit 4b63948 into swiftlang:main Sep 25, 2023
@stmontgomery stmontgomery deleted the expect-throws-type branch September 25, 2023 18:14
@stmontgomery stmontgomery added the enhancement New feature or request label Oct 3, 2023
@stmontgomery stmontgomery added the macros 🔭 Related to Swift macros such as @Test or #expect label Jun 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request macros 🔭 Related to Swift macros such as @Test or #expect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants