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

[SWT-NNNN] Return the thrown error from #expect(throws:) and #require(throws:). #780

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Oct 22, 2024

This PR changes the signatures of the various throws: overloads of #expect and #require so that on success they return the error that was thrown rather than Void. This then allows more ergonomic inspection of the error's properties:

let error = try #require(throws: MyError.self) {
  try f()
}
#expect(error.hasWidget)
#expect(error.userName == "John Smith")

For more information, see the proposal document.

Resolves rdar://138235250.

Further PR Details It is not possible to overload a macro or function solely by return type without the compiler reporting `Ambiguous use of 'f()'`, so we are not able to stage this change in using `@_spi(Experimental)` without breaking test code that already imports our SPI.

This change is potentially source-breaking for tests that inadvertently forward the result of these macro invocations to an enclosing scope. For example, the compiler will start emitting a warning here:

func bar(_ pfoo: UnsafePointer<Foo>) throws { ... }

withUnsafePointer(to: foo) { pfoo in // ⚠️ Result of call to 'withUnsafePointer(to:_:)' is unused
  #expect(throws: BadFooError.self) {
    try bar(pfoo)
  }
}

This warning can be suppressed by assigning the result of #expect (or of withUnsafePointer(to:_:)) to _:

func bar(_ pfoo: UnsafePointer<Foo>) throws { ... }

withUnsafePointer(to: foo) { pfoo in
  _ = #expect(throws: BadFooError.self) {
    try bar(pfoo)
  }
}

Because #expect and #require are macros, they cannot be referenced by name like functions, so you cannot assign them to variables (and then run into trouble with the types of those variables.)

Finally, this change deprecates the variants of #expect and #require that take two closures:

#expect { // ⚠️ 'expect(_:sourceLocation:performing:throws:)' is deprecated: Examine the result of '#expect(throws:)' instead.
  ...
} throws: { error in
  guard let error = error as? FoodTruckError else {
    return false
  }
  return error.napkinCount == 0
}

These variants are no longer needed because you can simply examine the result of the other variants:

let error = #expect(throws: FoodTruckError.self) {
  ...
}
#expect(error?.napkinCount == 0)

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

@grynspan grynspan added enhancement New feature or request public-api Affects public API issue-handling Related to Issue handling within the testing library labels Oct 22, 2024
@grynspan grynspan added this to the Swift 6.1 milestone Oct 22, 2024
@grynspan grynspan self-assigned this Oct 22, 2024
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/return-errors-from-expect-throws branch from e15c39e to 61854c2 Compare October 22, 2024 21:03
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/return-errors-from-expect-throws branch from e58a2ce to 89c147c Compare October 23, 2024 14:47
@grynspan grynspan changed the title [DNM] Return the thrown error from #expect(throws:) and #require(throws:). [SWT-NNNN] Return the thrown error from #expect(throws:) and #require(throws:). Oct 23, 2024
@grynspan grynspan added the api-proposal API proposal PRs (documentation only) label Oct 23, 2024
@grynspan grynspan marked this pull request as ready for review October 23, 2024 15:48
@grynspan
Copy link
Contributor Author

@swift-ci test

1 similar comment
@grynspan
Copy link
Contributor Author

@swift-ci test

@dabrahams
Copy link

Nice!

@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/return-errors-from-expect-throws branch 2 times, most recently from 7cfe55b to 6d4fd83 Compare October 28, 2024 12:35
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan
Copy link
Contributor Author

@swift-ci test Linux

This PR changes the signatures of the various `throws:` overloads of `#expect`
and `#require` so that on success they return the error that was thrown rather
than `Void`. This then allows more ergonomic inspection of the error's
properties:

```swift
let error = try #require(throws: MyError.self) {
  try f()
}
 #expect(error.hasWidget)
 #expect(error.userName == "John Smith")
```

It is not possible to overload a macro or function solely by return type without
the compiler reporting `Ambiguous use of 'f()'`, so we are not able to stage
this change in using `@_spi(Experimental)` without breaking test code that
already imports our SPI.

This change is potentially source-breaking for tests that inadvertently forward
the result of these macro invocations to an enclosing scope. For example, the
compiler will start emitting a warning here:

```swift
func bar(_ pfoo: UnsafePointer<Foo>) throws { ... }

withUnsafePointer(to: foo) { pfoo in // ⚠️ Result of call to 'withUnsafePointer(to:_:)' is unused
  #expect(throws: BadFooError.self) {
    bar(pfoo)
  }
}
```

This warning can be suppressed by assigning the result of `#expect` (or of
`withUnsafePointer(to:_:)`) to `_`:

```swift
func bar(_ pfoo: UnsafePointer<Foo>) throws { ... }

withUnsafePointer(to: foo) { pfoo in
  _ = #expect(throws: BadFooError.self) {
    bar(pfoo)
  }
}
```

Because `#expect` and `#require` are macros, they cannot be referenced by name
like functions, so you cannot assign them to variables (and then run into
trouble with the types of those variables.)
…` only

throws if you try to cast the result to `any Error` (can't instantiate `Never`),
add a way to suppress our diagnostics in our own unit tests.
@grynspan grynspan force-pushed the jgrynspan/return-errors-from-expect-throws branch from 6d4fd83 to ec1fa5a Compare November 2, 2024 17:05
@grynspan
Copy link
Contributor Author

grynspan commented Nov 2, 2024

@swift-ci test

@grynspan grynspan modified the milestones: Swift 6.1, Swift 6.2 Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-proposal API proposal PRs (documentation only) enhancement New feature or request issue-handling Related to Issue handling within the testing library public-api Affects public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants