Skip to content

TaskLocal test trait#3329

Open
mbrandonw wants to merge 26 commits into
swiftlang:mainfrom
pointfreeco:task-local-test-trait
Open

TaskLocal test trait#3329
mbrandonw wants to merge 26 commits into
swiftlang:mainfrom
pointfreeco:task-local-test-trait

Conversation

@mbrandonw

@mbrandonw mbrandonw commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

A proposal for a new .taskLocal test trait for overriding task locals in tests.

View the rendered proposal.

mbrandonw and others added 2 commits June 18, 2026 09:59
Corrected typos and improved clarity in the documentation regarding the task local test trait.
Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
@ktoso ktoso self-requested a review June 18, 2026 23:21

## Alternatives considered

A more general name for this API could be used instead of `.taskLocal`:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the workgroup discussed a couple of other possible names for this trait; please include any you remember here. (I specifically mentioned withValue<T>(_: T, for taskLocal: TaskLocal<T>), and I think somebody else had at least one other suggestion but I don't recall offhand what it was.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion was .binding(T, to: V). The verb "bind" has some precedence in the TaskLocal documentation.

@grynspan grynspan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments only. Please make sure to update the proposal with any corresponding changes in the implementation PR!

@grynspan grynspan added workgroup: needs development This proposal needs to be developed further before it can be reviewed TWG Contains topics under the domain of the Testing Workgroup new proposal Adds a new proposal document labels Jun 18, 2026
stephencelis and others added 3 commits June 18, 2026 17:26
Co-authored-by: Jonathan Grynspan <grynspan@me.com>
Co-authored-by: Jonathan Grynspan <grynspan@me.com>

@ktoso ktoso left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, just a minor nitpick about avoiding the word override (we don't really use that term with task locals).

The naming .taskLocal(..., value) also sounds good to me

Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated

There are two primary motivations for adding this convenience:

* Task locals are by far the [most common] reason to define custom `TestScoping`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Task locals are by far the [most common] reason to define custom `TestScoping`
* Task locals are by far the most common reason to define custom `TestScoping`

not sure why the [], just "the most common ..."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The brackets link just below to some context: https://github.com/search?q=testscoping+language%3ASwift+&type=code

We just found that a lot of the public TestScoping conformances are related to task locals.

Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
stephencelis and others added 8 commits June 18, 2026 23:05
Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
Updated terminology from 'overriding' to 'binding' for clarity in the context of task locals. Adjusted related descriptions to reflect this change throughout the document.
@stephencelis

Copy link
Copy Markdown
Contributor

@grynspan Requested changes applied! Let us know if there's anything else.

Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated

In [ST-0007], test scoping traits were introduced to allow wrapping the execution
of tests so that code could be run before and after each test. The main
motivation for this feature is overriding [`TaskLocal`s][SE-0311: TaskLocal] for tests, and the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this still isn't showing the link in the rendered version of the document. Might be a markdown syntax quirk

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might work better as a standard markdown link instead of the brackets link.

Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
/// A type that binds a task local value for the scope of a test.
///
/// To add this trait to a test, use ``Trait/taskLocal(_:_:)``.
public struct TaskLocalTrait<Value: Sendable>: SuiteTrait, TestScoping, TestTrait {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor stylistic thing, but I'd list TestTrait before TestScoping—it's a trait type first and foremost

Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
Comment thread proposals/testing/NNNN-task-local-test-trait.md
Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
Comment on lines +162 to +163
If Swift macros gain the ability to see symbols defined by other macros, we will be able to drop
the restriction that task locals be defined outside the test target.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand where this comes from, but the phrasing of "…we will be able to drop the restriction…" could be a little confusing: the API itself isn't imposing any such restriction, it's a consequence of current language restrictions outside the scope of this proposal.

I'd suggest tweaking this by saying something like: if this macro limitation were resolved, then this proposed API would become usable in more contexts/situations. The thing I'm looking to clarify is that no further action or change to the API would be required; it would "only" require a change to the language or macro system.

Comment thread proposals/testing/NNNN-task-local-test-trait.md Outdated
Co-authored-by: Stuart Montgomery <smontgomery@apple.com>
stephencelis and others added 11 commits June 19, 2026 14:04
Co-authored-by: Stuart Montgomery <smontgomery@apple.com>
Co-authored-by: Stuart Montgomery <smontgomery@apple.com>
Co-authored-by: Stuart Montgomery <smontgomery@apple.com>
Co-authored-by: Stuart Montgomery <smontgomery@apple.com>
Co-authored-by: Stuart Montgomery <smontgomery@apple.com>
Corrected references to TaskLocal in the motivation section and updated the link formatting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new proposal Adds a new proposal document TWG Contains topics under the domain of the Testing Workgroup workgroup: needs development This proposal needs to be developed further before it can be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants