Simplify the expansion of @Test by eliminating the isolated thunk.#1592
Open
Simplify the expansion of @Test by eliminating the isolated thunk.#1592
@Test by eliminating the isolated thunk.#1592Conversation
This PR removes one of the thunks we emit when expanding `@Test`. For example, given the following test function: ```swift @test func foo() {} ``` We currently emit, approximately: ```swift @available(*, deprecated, message: "This function is an implementation detail of the testing library. Do not use it directly.") @sendable private func $s12TestingTests3foo4TestfMp_17Z152ec26a56a4f672fMu_() async throws -> Void { @sendable func $s12TestingTests3foo4TestfMp_7__localfMu_(_:isolated (any _Concurrency.Actor)?=Testing.__defaultSynchronousIsolationContext) async throws { _ = unsafe try await Testing.__requiringUnsafe(Testing.__requiringTry(Testing.__requiringAwait(foo()))) } try await $s12TestingTests3foo4TestfMp_7__localfMu_() } @available(*, deprecated, message: "This property is an implementation detail of the testing library. Do not use it directly.") @sendable private func $s12TestingTests3foo4TestfMp_25generator152ec26a56a4f672fMu_() async -> Testing.Test { return .__function( named: "foo()", in: nil as Swift.Never.Type?, xcTestCompatibleSelector: nil, traits: [],sourceBounds: Testing.__SourceBounds(__uncheckedLowerBound: Testing.SourceLocation(__uncheckedFileID: "TestingTests/ZipTests.swift", filePath: "/Volumes/Dev/Source/swift-testing-public/Tests/TestingTests/ZipTests.swift", line: 30, column: 2), upperBound: (30, 20)), parameters: [], testFunction: $s12TestingTests3foo4TestfMp_17Z152ec26a56a4f672fMu_ ) } @section("__DATA_CONST,__swift5_tests") @used @available(*, deprecated, message: "This property is an implementation detail of the testing library. Do not use it directly.") private nonisolated let $s12TestingTests3foo4TestfMp_33testContentRecord152ec26a56a4f672fMu_: Testing.__TestContentRecord = ( 0x74657374, /* 'test' */ 0, { outValue, type, _, _ in Testing.Test.__store($s12TestingTests3foo4TestfMp_25generator152ec26a56a4f672fMu_, into: outValue, asTypeAt: type) }, 0, 0 ) ``` Note that the first thunk function has a nested thunk function that serves only to impose actor isolation on the actual test function. This change replaces that thunk with `nonisolated(nonsending)` in appropriate locations throughout the macro target and library target. After this change, we emit a simpler expansion: ```swift @available(*, deprecated, message: "This function is an implementation detail of the testing library. Do not use it directly.") @sendable private nonisolated(nonsending) func $s12TestingTests3foo4TestfMp_17Z152ec26a56a4f672fMu_() async throws -> Void { _ = unsafe try await Testing.__requiringUnsafe(Testing.__requiringTry(Testing.__requiringAwait(foo()))) } @available(*, deprecated, message: "This function is an implementation detail of the testing library. Do not use it directly.") @sendable private nonisolated(nonsending) func $s12TestingTests3foo4TestfMp_25generator152ec26a56a4f672fMu_() async -> Testing.Test { return .__function( named: "foo()", in: nil as Swift.Never.Type?, xcTestCompatibleSelector: nil, traits: [],sourceBounds: Testing.__SourceBounds(__uncheckedLowerBound: Testing.SourceLocation(__uncheckedFileID: "TestingTests/ZipTests.swift", filePath: "/Volumes/Dev/Source/swift-testing-public/Tests/TestingTests/ZipTests.swift", line: 30, column: 2), upperBound: (30, 20)), parameters: [], testFunction: $s12TestingTests3foo4TestfMp_17Z152ec26a56a4f672fMu_ ) } @section("__DATA_CONST,__swift5_tests") @used @available(*, deprecated, message: "This property is an implementation detail of the testing library. Do not use it directly.") private nonisolated let $s12TestingTests3foo4TestfMp_33testContentRecord152ec26a56a4f672fMu_: Testing.__TestContentRecord = ( 0x74657374, /* 'test' */ 0, { outValue, type, _, _ in Testing.Test.__store($s12TestingTests3foo4TestfMp_25generator152ec26a56a4f672fMu_, into: outValue, asTypeAt: type) }, 0, 0 ) ```
674bad0 to
9608c1d
Compare
| #expect(i == j) | ||
| } | ||
| } | ||
|
|
Contributor
There was a problem hiding this comment.
nitpick: this seems unnecessary
harlanhaskins
approved these changes
Feb 25, 2026
Contributor
harlanhaskins
left a comment
There was a problem hiding this comment.
It's quite nice to see language features obviating the need for workarounds in our code. Very nice
stmontgomery
approved these changes
Feb 26, 2026
Contributor
Author
|
Compiler angry! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR removes one of the thunks we emit when expanding
@Test. For example, given the following test function:We currently emit, approximately:
Note that the first thunk function has a nested thunk function that serves only to impose actor isolation on the actual test function. This change replaces that thunk with
nonisolated(nonsending)in appropriate locations throughout the macro target and library target. After this change, we emit a simpler expansion:Checklist: