Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 6, 2025

Problem

The @SwiftSnapshot macro failed to compile when applied to generic types, producing the error:

Static stored properties not supported in generic types

This occurred because Swift prohibits static stored properties in generic types, but the macro was generating:

@SwiftSnapshot
struct User<T: Codable> {
  let id: Int
  let name: String
  let some: [T]
}
// Error: Static stored properties not supported in generic types

Solution

The fix uses computed properties for all types (both generic and non-generic) to simplify the codebase. Since computed properties work for both cases, this eliminates the need for conditional logic.

For all types (unified approach):

internal static var __swiftSnapshot_folder: String? { nil }
internal static var __swiftSnapshot_properties: [__SwiftSnapshot_PropertyMetadata] {
  [/* property metadata */]
}

Implementation

  • Use computed properties (static var) for all types instead of differentiating between generic and non-generic
  • Removed conditional logic and isGenericType() helper function
  • Simplified generatePropertiesArray() to generate only computed properties
  • Updated all test expectations to reflect the unified approach
  • Computed properties function identically to stored properties at runtime

Testing

  • Added macro expansion test genericStruct() to verify correct code generation
  • Added integration test macroWithGenericType() to verify compilation and runtime behavior
  • All 200 tests pass with the simplified implementation

Benefits

Simpler codebase - Removed ~30 lines of conditional logic
Unified approach - Same property style for all types
Fully compatible - Works for both generic and non-generic types
No breaking changes - Computed properties behave identically to stored properties

Example Usage

@SwiftSnapshot
struct Container<T: Codable> {
  let id: Int
  let items: [T]
}

// Now compiles successfully!
let container = Container(id: 1, items: [1, 2, 3])
try container.exportSnapshot(variableName: "testContainer")

Fixes the issue reported in the problem statement where generic types would fail to compile with the macro.

Original prompt

On this I have an issue while using the macro.

@SwiftSnapshot
struct User<T: Codable> {
let id: Int
let name: String
let some: [T]
}
/var/folders/6_/t11661yd1_b35sbbws0xvggm0000gp/T/swift-generated-sources/@_swiftmacro_9testGuild4User13SwiftSnapshotfMm.swift:1:21 Static stored properties not supported in generic types
In expansion of macro 'SwiftSnapshot' on generic struct 'User' here.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits October 6, 2025 14:08
Co-authored-by: mackoj <661647+mackoj@users.noreply.github.com>
Co-authored-by: mackoj <661647+mackoj@users.noreply.github.com>
Copilot AI changed the title On this I have an issue while using the macro. @SwiftSnapshot struct User<T: Codable> { let id: Int let name: String let some: [T] } /var/folders/6_/t11661yd1_b35sbbws0xvggm0000gp/T/swift-generated-sources/@__swiftmacro_9testGuild4User13SwiftSna... Fix @SwiftSnapshot macro to support generic types Oct 6, 2025
Copilot AI requested a review from mackoj October 6, 2025 14:13
@mackoj
Copy link
Owner

mackoj commented Oct 7, 2025

@copilot Shouldn't we always use the generic style to simplify the codebase ? The generic style work for non generic too ?

Co-authored-by: mackoj <661647+mackoj@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 7, 2025

@copilot Shouldn't we always use the generic style to simplify the codebase ? The generic style work for non generic too ?

You're absolutely right! Using computed properties for all types simplifies the codebase significantly. I've updated the implementation to use static var computed properties for both generic and non-generic types, removing the conditional logic and the isGenericType() helper function. This approach works uniformly for all types and makes the code much cleaner. All 200 tests still pass. Changes in commit 7a2ef68.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • ocsp.apple.com
    • Triggering command: /usr/share/swift/usr/bin/swift-test --filter SwiftSnapshotMacrosTests (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from mackoj October 7, 2025 07:43
@mackoj mackoj marked this pull request as ready for review October 7, 2025 07:52
@mackoj mackoj merged commit 8168e0f into main Oct 7, 2025
@mackoj mackoj deleted the copilot/fix-fc55f167-1336-47f5-87be-96347333ec0f branch October 7, 2025 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants