SwiftData crash when using a @Query on macOS 15.3.x

We use @Query macro in our App. After we got macOS 15.3 update, our App crashes at @Query line.

SwiftData/Schema.swift:305: Fatal error: KeyPath \Item.<computed 0x0000000100599e54 (Vec3D)>.x points to a field (<computed 0x0000000100599e54 (Vec3D)>) that is unknown to Item and cannot be used.

This problem occurs only when the build configuration is "Release", and only when I use @Query macro with sort: parameter. The App still works fine on macOS 14.7.3.

This issue seems similar to what has already been reported in the forum. It looks like a regression on iOS 18.3. https://developer.apple.com/forums/thread/773308

Item.swift

import Foundation
import SwiftData


public struct Vec3D {
    let x,y,z: Int
}

extension Vec3D: Codable { }

@Model
final class Item {
    var timestamp: Date
    var vec: Vec3D
    init(timestamp: Date) {
        self.timestamp = timestamp
        self.vec = Vec3D(x: 0, y: 0, z: 0)
    }
}

ContentView.Swift

import SwiftUI
import SwiftData

struct ContentView: View {
    
    @Environment(\.modelContext)
    private var modelContext
    
    @Query(sort: \Item.vec.x) // Crash
    private var items: [Item]

    var body: some View {
        NavigationSplitView {
            List {
                ForEach(items) { item in
                    NavigationLink {
                        Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))")
                    } label: {
                        Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))
                    }
                }
                .onDelete(perform: deleteItems)
            }
            .navigationSplitViewColumnWidth(min: 180, ideal: 200)
            .toolbar {
                ToolbarItem {
                    Button(action: addItem) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
            }
        } detail: {
            Text("Select an item")
        }
    }

    private func addItem() {
        withAnimation {
            let newItem = Item(timestamp: Date())
            modelContext.insert(newItem)
        }
    }

    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            for index in offsets {
                modelContext.delete(items[index])
            }
        }
    }
}

This issue seems similar to what has already been reported in the forum. It looks like a regression on iOS 18.3. https://developer.apple.com/forums/thread/773308

Yeah, I believe so, and would suggest that you file your own feedback report to voice that your app is impacted.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData crash when using a @Query on macOS 15.3.x
 
 
Q