Skip to content

Serialization traits for data dependencies#1232

Open
grynspan wants to merge 17 commits intomainfrom
jgrynspan/dependency-trait
Open

Serialization traits for data dependencies#1232
grynspan wants to merge 17 commits intomainfrom
jgrynspan/dependency-trait

Conversation

@grynspan
Copy link
Contributor

@grynspan grynspan commented Jul 21, 2025

Experimental .serialized(for:) trait. Example use cases:

import Foundation

@Test(.serialized(for: \ProcessInfo.environment))
func `HAS_FREEZER environment variable`() {
  _ = setenv("HAS_FREEZER", "1", 1)
  #expect(FoodTruck.hasFreezer)
  _ = setenv("HAS_FREEZER", "0", 1)
  #expect(!FoodTruck.hasFreezer)
}

@Test(.serialized(for: *))
func `Uses lots of different state, seriously, just scads of it`() { ... }

Resolves rdar://135288463.

Checklist:

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

@grynspan grynspan added this to the Swift 6.x (main) milestone Jul 21, 2025
@grynspan grynspan self-assigned this Jul 21, 2025
@grynspan grynspan added enhancement New feature or request concurrency 🔀 Swift concurrency/sendability issues public-api Affects public API traits Issues and PRs related to the trait subsystem or built-in traits labels Jul 21, 2025
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/dependency-trait branch 2 times, most recently from b81b073 to d54a0ba Compare July 21, 2025 20:02
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/dependency-trait branch from d54a0ba to 0856c1d Compare July 21, 2025 20:17
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/dependency-trait branch 2 times, most recently from 935f2ab to 9066f51 Compare July 21, 2025 22:23
@grynspan grynspan changed the title [WIP] Dependency traits [WIP] Serialization traits for data dependencies Jul 21, 2025
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/dependency-trait branch 6 times, most recently from 15b0eaa to e6e2f85 Compare July 22, 2025 19:08
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/dependency-trait branch from e6e2f85 to eca0abb Compare July 22, 2025 19:28
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/dependency-trait branch 2 times, most recently from 147f34d to 5ffab44 Compare July 22, 2025 21:15
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/dependency-trait branch from 5ffab44 to f57489d Compare July 24, 2025 15:25
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan marked this pull request as ready for review January 16, 2026 18:30
@grynspan grynspan changed the title [WIP] Serialization traits for data dependencies Serialization traits for data dependencies Jan 16, 2026
| `HOME`[\*](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap08.html) | `String` | Used to determine the user's home directory. |
| `SIMULATOR_RUNTIME_BUILD_VERSION`\* | `String` | Used when running in the iOS (etc.) Simulator to determine the simulator's version. |
| `SIMULATOR_RUNTIME_VERSION`\* | `String` | Used when running in the iOS (etc.) Simulator to determine the simulator's version. |
| `SWT_SERIALIZED_TRAIT_APPLIES_GLOBALLY` | `Bool` | Whether or not `.serialized` applies globally or just to its branch of the test graph. |
Copy link
Contributor

Choose a reason for hiding this comment

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

While this is an experimental feature, should the env var name include the word "experimental"?

// has an issue handling trait _and_ some other trait which caused an
// issue to be recorded, the issue handling trait can process the issue
// even though it wasn't recorded by the test function.
try await Test.withCurrent(step.test) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that this _postingTestStartedAndEndedEvents function has been extracted, it looks like both of its call sites are now nested within try await Test.withCurrent(step.test) scopes. If that's true, then can you remove this withCurrent(...) call as redundant, now?

var kind: Kind

/// The key path used to construct this dependency, if any.
nonisolated(unsafe) var originalKeyPath: AnyKeyPath?
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to avoid storing this in case type(_:) of the Kind enum above? Is it because you can't use nonisolated(unsafe) on an enum case associated value?

Comment on lines +121 to +124
case (.some, .some):
// The two traits have different dependencies. Combine them into a single
// .unbounded dependency.
return .serialized(for: *)
Copy link
Contributor

Choose a reason for hiding this comment

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

It'll be good to include discussion of this behavior in any documentation article we eventually write, since I imagine folks will wonder what happens if two dependency serialization traits conflict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

concurrency 🔀 Swift concurrency/sendability issues enhancement New feature or request public-api Affects public API traits Issues and PRs related to the trait subsystem or built-in traits

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants