Skip to content

Diagnose when using a non-escapable type as suite. #988

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 5 commits into from
Mar 3, 2025

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Mar 2, 2025

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

@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:

@Suite struct MyTests: ~Escapable {
  @Suite struct EndToEndTests {
    ...
  }
}

Checklist:

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

This PR enables using non-escapable types as suites. For example:

```swift
@suite struct NumberOfBeesTests: ~Escapable {
  @test borrowing func countBees() { ... }
}
```

Non-escapable types have a number of constraints in Swift, and those constraints
aren't lifted in a test target, but generally speaking a non-escapable type
should be able to do all the things any other type can do _as a test suite_.
@grynspan grynspan added the enhancement New feature or request label Mar 2, 2025
@grynspan grynspan added this to the Swift 6.x milestone Mar 2, 2025
@grynspan grynspan self-assigned this Mar 2, 2025
@grynspan
Copy link
Contributor Author

grynspan commented Mar 2, 2025

@swift-ci test

@grynspan
Copy link
Contributor Author

grynspan commented Mar 2, 2025

@swift-ci test

@grynspan grynspan changed the title Allow using non-escapable types as suites. Diagnose when using a non-escapable type as suite. Mar 3, 2025
@grynspan
Copy link
Contributor Author

grynspan commented Mar 3, 2025

@swift-ci test

@grynspan grynspan merged commit d2a1019 into main Mar 3, 2025
3 checks passed
@grynspan grynspan deleted the jgrynspan/non-escapable-types-as-suites branch March 3, 2025 18:52
grynspan added a commit that referenced this pull request Mar 3, 2025
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 added a commit that referenced this pull request 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**: #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
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