-
Notifications
You must be signed in to change notification settings - Fork 54
Add Entry macro support #520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Introduce a new OpenSwiftUIMacros executable target and accompanying test target, and register them in Package.swift. Add a Macro folder in OpenSwiftUI with EntryMacro and __EntryDefaultValue macro declarations that reference the new OpenSwiftUIMacros module. Implement the macros in Sources/OpenSwiftUIMacros (EntryMacro, EntryDefaultValueMacro, and plugin entry point) to generate accessor and peer key types for @entry and to provide a getter-only expansion for @__EntryDefaultValue. Add comprehensive tests (OpenSwiftUIMacrosTests) to verify macro expansions for string and integer defaults.
The generated EnvironmentKey structs were emitted outside the EnvironmentValues extension, which did not match expected structure. Move the private key types into the extension and adjust their defaultValue to computed properties (with getter) to satisfy the macro/test expectations. This ensures myCustomValue and intValue keys are scoped correctly and tests reflect the updated shape.
Add tests for two new cases: inferred type ("@entry var inferType = 0") and optional type ("@entry var optionalType: Int?"). Update the macro implementation to accept bindings that have either a type annotation or an initializer, infer simple literal types from initializers (Int, Double, String, Bool), and provide a default nil value for optional type annotations. This fixes failing tests by allowing @entry to work with type inference and Optional default nil semantics, and by returning clear errors for unsupported cases.
Add support for type inference when using @entry with simple initializer calls like CustomType(). The macro previously only inferred basic literals (String/Bool) and rejected other expressions; this caused tests like adding a CustomType struct with @entry var inferCustomType = CustomType() to fail. Now the macro recognizes FunctionCallExpr with a DeclReferenceExpr callee and uses the referenced identifier as the inferred type. Also add a unit test (testEntryMacroWithCustomTypeInference) that verifies expansion for the CustomType example, and update the error message for complex expressions to instruct users to provide explicit types when necessary.
Improve @entry macro handling for function-call initializers by validating and heuristically inferring types for different call forms. Add a new test to assert behavior for member function calls (A.b()) and emit a clear diagnostic when the macro cannot reliably infer a return type. This change also tightens error messages for complex expressions and ensures optional/member call cases either get a sensible heuristic or require an explicit type annotation so tests reflect the macro's limitations.
…ault value Refine Entry macro expansion to stop attempting fragile, case-by-case type inference from initializers (especially member function calls). Instead: always preserve the initializer expression as the defaultValue when present, handle the "only type annotation" case by producing nil for optional types or emitting an error for non-optional types, and emit the generated PatternBinding correctly. This fixes an incorrect emitted type like "static var defaultValue: B = A.B()" and updates tests to assert the expanded form (ignore @__EntryDefaultValue for now) and add a positive test for A.B() returning C.
Add a dedicated testEntryMacros map and several new assertMacroExpansion cases to follow the testEntryMacroExpansion example. These additions cover a basic @entry expansion with a custom default value, and a case ensuring member function call inference works when assigning A.B() as a default. This change was needed to extend test coverage for EntryMacro type inference and annotation generation and to isolate the Entry macro mapping for these tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for the @Entry macro in OpenSwiftUI, which provides a convenient way to define environment values with automatic key generation. The macro transforms annotated properties into computed properties that access environment keys, similar to SwiftUI's environment system.
- Implements
EntryMacroandEntryDefaultValueMacrofor transforming environment value declarations - Adds comprehensive test coverage for various type scenarios and edge cases
- Integrates macro infrastructure into the package structure with proper dependencies
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| Sources/OpenSwiftUIMacros/EntryMacro.swift | Core macro implementation that generates accessors and peer key structs |
| Sources/OpenSwiftUIMacros/EntryDefaultValueMacro.swift | Helper macro for generating computed property accessors from stored properties |
| Sources/OpenSwiftUIMacros/OpenSwiftUIMacrosPlugin.swift | Macro plugin registration for compiler integration |
| Sources/OpenSwiftUICore/Macro/EntryMacro.swift | Public macro declarations with external macro references |
| Tests/OpenSwiftUIMacrosTests/EntryMacroTests.swift | Comprehensive test suite covering various macro expansion scenarios |
| Package.swift | Package configuration updates to include macro targets and dependencies |
| CLAUDE.md | Documentation update noting XCTest requirement for macro tests |
| """, | ||
| macros: testMacros | ||
| ) | ||
| assertMacroExpansion( |
Copilot
AI
Sep 30, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test functions should not include 'test' prefix according to coding guidelines. The function should be named 'entryMacroExpansion' instead of 'testEntryMacroExpansion'.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #520 +/- ##
==========================================
- Coverage 24.23% 24.15% -0.08%
==========================================
Files 496 499 +3
Lines 28519 28626 +107
==========================================
+ Hits 6912 6916 +4
- Misses 21607 21710 +103 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
No description provided.