Skip to content

[6.1] Diagnose when using a non-escapable type as suite. #992

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

Merged
merged 1 commit into from
Mar 3, 2025

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Mar 3, 2025

  • Explanation: Presents a custom diagnostic if you try to put a test function in a non-escapable type since that's not supported by the language yet.
  • Scope: Tests in non-escapable types.
  • Issues: N/A
  • Original PRs: Diagnose when using a non-escapable type as suite. #988
  • Risk: Low (shouldn't be any code out there doing this since it doesn't compile.)
  • Testing: Unit test coverage is in place.
  • Reviewers: @briancroom @stmontgomery

This PR introduces a custom compile-time diagnostic when attempting to
use a non-escapable type as a suite. For example:

```swift
@suite struct NumberOfBeesTests: ~Escapable {
  @test borrowing func countBees() { ... } // 🛑 Attribute 'Test' cannot be applied to a function within structure 'NumberOfBeesTests' because its conformance to 'Escapable' has been suppressed
}
```

Values with non-escapable type cannot currently be initialized nor
returned from a function, and we need to be able to do both in order to
correctly implement the `@Test` macro.

This change does not diagnose if `@Suite` is applied to such a type but
does not contain any test functions, because we do compile successfully
in that case and this sort of pattern remains valid:

```swift
@suite struct MyTests: ~Escapable {
  @suite struct EndToEndTests {
    ...
  }
}
```

### 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.
@grynspan grynspan added the enhancement New feature or request label Mar 3, 2025
@grynspan grynspan added this to the Swift 6.1 milestone Mar 3, 2025
@grynspan grynspan self-assigned this Mar 3, 2025
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2025

@swift-ci test

@grynspan grynspan merged commit 336fad6 into release/6.1 Mar 3, 2025
3 checks passed
@grynspan grynspan deleted the jgrynspan/non-escapable-types-as-suites-6.1 branch March 3, 2025 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants