Skip to content

Conversation

@ethanhuang13
Copy link
Contributor

@ethanhuang13 ethanhuang13 commented Jan 11, 2026

Summary

  • Generate partial types for arrays as [Element.PartiallyGenerated] instead of [Element].
  • Decode partial array properties using element partial types.
  • Add GenerableMacro test coverage for array partials.

Problem

When a @Generable struct has an array property (e.g. [CatProfile]), the AnyLanguageModel macro previously synthesized the partially generated property as [CatProfile], not a partially generated array. This breaks SwiftUI usage and diverges from FoundationModels’ macro output.

FoundationModels’ GenerableMacro produces a partially generated property of type [CatProfile.PartiallyGenerated]?, and AnyLanguageModel needed to align with that output.

Example

@Generable
struct CatProfiles {
  @Guide(description: "Cats", .count(5))
  var catProfiles: [CatProfile]
}

extension CatProfiles.PartiallyGenerated: View {
  var body: some View {
    if let catProfiles {
      // Previously needed a workaround: `ForEach(catProfiles.asPartiallyGenerated())`.
      // Now aligned with FoundationModels.
      ForEach(catProfiles) { catProfile in
        // ...
      }
    }
  }
}

Before

  • Generated property type: public var catProfiles: [CatProfile] (not partially generated)
  • Forces ForEach(catProfiles.asPartiallyGenerated()) to get the correct shape for SwiftUI.

After

  • Generated property type: public var catProfiles: [CatProfile.PartiallyGenerated]?
  • Correct SwiftUI usage becomes ForEach(catProfiles) with no extra conversion.

Testing

  • swift test (new GenerableMacro test passes).

@ethanhuang13 ethanhuang13 marked this pull request as ready for review January 11, 2026 09:00
@mattt
Copy link
Owner

mattt commented Jan 15, 2026

Thank you, @ethanhuang13! Taking a look now...

@mattt
Copy link
Owner

mattt commented Jan 15, 2026

Thanks for pushing this through, @ethanhuang13! The array partial type fix is great.

One short‑term issue I noticed: the new PartiallyGenerated property declarations now use .PartiallyGenerated for all types, which inadvertently breaks primitives and dictionaries (e.g. String.PartiallyGenerated doesn’t exist). I made a minimal follow‑up adjustment (13589db) to keep primitives/dicts as‑is, preserve optional element types, and make array/dictionary detection a bit more robust. I also added a couple of focused tests so we don’t regress.

Looking at topLevelColonIndex, I suspect this is all due for a rethink, as this all seems to be reimplementing aspects of SwiftSyntax albeit poorly. Let's push these incremental changes and take that on in a follow-up PR.

Copy link

Copilot AI left a 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 fixes the @Generable macro to generate correct partial types for array properties, aligning with FoundationModels' macro behavior. Previously, arrays like [CatProfile] were synthesized as [CatProfile] in the PartiallyGenerated struct, but now they correctly become [CatProfile.PartiallyGenerated]? for non-primitive element types.

Changes:

  • Added type analysis utilities to distinguish arrays, dictionaries, and primitives
  • Modified partial type generation to recursively handle array elements
  • Updated property decoding to initialize arrays with partially generated element types

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
Tests/AnyLanguageModelTests/GenerableMacroTests.swift Adds test coverage for array properties with both complex and primitive element types
Sources/AnyLanguageModelMacros/GenerableMacro.swift Implements type analysis helpers and updates partial type generation logic for arrays

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 9 comments.

Comments suppressed due to low confidence (1)

Sources/AnyLanguageModelMacros/GenerableMacro.swift:1

  • The depth check at line 194 occurs inside the loop but after the switch statement, which means negative depth from mismatched brackets won't be caught until after processing the current character. For example, with input ]:[String], the function would decrement depth to -1 but continue processing. The check should occur after each depth modification to catch structural errors earlier. Move the depth check inside each case that modifies totalDepth.
import SwiftCompilerPlugin

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mattt
Copy link
Owner

mattt commented Jan 15, 2026

@ethanhuang13 Thanks again for your help. Merging this PR now.

@mattt mattt merged commit 2316ca7 into mattt:main Jan 15, 2026
3 checks passed
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