[Pitch] Add a `.taskLocal` trait to Swift Testing

Strong +1 to the proposal. As it mentions, I called this out as a future direction in ST-0007 so that's perhaps unsurprising.

The only reason I didn't pursue it after ST-0007 was because I was under the impression that the problem described in Macro symbol visibility to other macros affected all macro usages. But it turns out that only affects macros declared in the same module as the reference, so that addresses what was my main concern. I'd still like to see progress on resolving that issue, but the scope of the problem seems small enough that this ought to proceed in the meantime.

I did have one suggestion, which I added as a comment on the implementation PR as well: I think the value the task local is bound to ought to be lazily-evaluated. A couple reasons for that:

  • If the test is skipped (e.g. it has a .disabled trait) the value expression will never be evaluated. That can be useful if the evaluation has observable side effects you only want to occur if the test actually runs, especially if that work is expensive.
  • The value obtained lazily during provideScope() can be released and discarded once the test finishes, which again can be important if the value occupies significant memory or otherwise needs to be deallocated once it's no longer needed.

The PR comment goes into a bit more detail and suggests using @autoclosure, but I wanted to surface this topic here for other reviewers' consideration too.

3 Likes