Thread Safety On Macro and Property Wrappers#24
Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR enhances thread safety across the SwiftEnvironment library, simplifies the GlobalEntry macro by removing the StaticModifier enum and introducing a value wrapper, and updates tests and documentation accordingly.
- Added
atomicRead/atomicWritemethods and dispatch queues for thread-safe global and property-wrapper accesses. - Simplified the
GlobalEntrymacro: dropped the.isolatedparameter, removedStaticModifier, and introduced___ValueWrapperstructs. - Updated tests to reflect macro changes, added a sendable-injection integration test, and bumped the README to version 4.1.4.
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| Tests/SwiftEnvironmentTests/MacroTests.swift | Updated expected macro expansion to use atomicRead and ___ValueWrapper; removed isolated test. |
| Tests/SwiftEnvironmentTests/IntegrationTests.swift | Added test_givenSendableInjection_whenGet_shouldAlwaysReturnNewValue to cover transient sendable entries. |
| Tests/SwiftEnvironmentTests/DummyDependency.swift | Changed sixthDummy to use a sendable dependency, updated initializer, and imported SwiftUICore. |
| Sources/SwiftEnvironmentMacro/Macro/GlobalEntryMacro.swift | Removed StaticModifier logic and nonisolated attribute, now emits a ___ValueWrapper struct and uses atomicRead. |
| Sources/SwiftEnvironment/Macros.swift | Simplified GlobalEntry macro signature—dropped modifier parameter and StaticModifier enum. |
| Sources/SwiftEnvironment/Environment/GlobalValues.swift | Changed atomicRead from private to public for broader accessibility. |
| Sources/SwiftEnvironment/Environment/GlobalEnvironment.swift | Introduced a concurrent accessQueue, added atomicRead/atomicWrite around wrappedValue, and marked as @unchecked Sendable. |
| README.md | Updated package version to 4.1.4 and removed outdated StaticModifier usage example. |
Comments suppressed due to low confidence (4)
README.md:62
- [nitpick] Consider adding a migration note about the removal of the
.isolatedparameter from@GlobalEntry, since users upgrading from earlier versions will need to update calls like@GlobalEntry(.isolated).
### Accessing Global Values
Sources/SwiftEnvironment/Environment/GlobalEnvironment.swift:12
- [nitpick] The name
accessQueueinGlobalEnvironmentcould be confused withGlobalValues.accessQueue. Consider renaming it toenvironmentAccessQueueor similar to clarify its scope.
private let accessQueue = DispatchQueue(label: "GlobalEnvironment.accessQueue", attributes: .concurrent)
Tests/SwiftEnvironmentTests/DummyDependency.swift:35
- The type
DummySendableDependencyis not defined or imported, causing a build error. Define this protocol (e.g.,protocol DummySendableDependency: AnyObject & Sendable { var id: UUID { get } }) or adjust to a known type.
@GlobalEntry var sixthDummy: DummySendableDependency = DummySendableClass()
Tests/SwiftEnvironmentTests/MacroTests.swift:33
- [nitpick] Exact indentation in the expected macro-expansion string can make tests brittle. Consider normalizing indentation or using an indentation-insensitive assertion to improve test resilience.
GlobalValues.atomicRead {
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 pull request introduces significant changes to the
SwiftEnvironmentlibrary, focusing on concurrency safety, simplifying macro usage, and updating documentation. Key updates include the introduction of atomic access methods for thread safety, the removal of theStaticModifierenum, and updates to tests and documentation to reflect these changes.Concurrency and Thread Safety
accessQueuewith concurrent attributes inGlobalEnvironment.swiftto ensure thread-safe access to global values. IntroducedatomicReadandatomicWritemethods for safe concurrent reads and writes. (Sources/SwiftEnvironment/Environment/GlobalEnvironment.swift: [1] [2] [3] [4]GlobalValues.atomicReadpublicly accessible for broader use. (Sources/SwiftEnvironment/Environment/GlobalValues.swift: Sources/SwiftEnvironment/Environment/GlobalValues.swiftL91-R91)Macro Simplification
StaticModifierenum and its associated logic for isolated/nonisolated modifiers. Simplified theGlobalEntrymacro by introducing a new___ValueWrapperstruct to handle value isolation. (Sources/SwiftEnvironment/Macros.swift: [1]Sources/SwiftEnvironmentMacro/Macro/GlobalEntryMacro.swift: [2] [3]Documentation Updates
README.mdto reflect the new version of the library (4.1.4) and removed outdated examples related to theStaticModifierenum. (README.md: [1] [2] [3]Test Updates
StaticModifierenum. (Tests/SwiftEnvironmentTests/DummyDependency.swift: [1]Tests/SwiftEnvironmentTests/MacroTests.swift: [2] [3]Dependency Updates
SwiftUICoreas a new import inDummyDependency.swift. (Tests/SwiftEnvironmentTests/DummyDependency.swift: Tests/SwiftEnvironmentTests/DummyDependency.swiftR11)