Swift compiler crashes with simple SwiftData model

In Xcode 15 beta 6, the swift compiler crashes with a simple SwiftData model. The model compiles OK, but any attempt to instantiate the entity in code, including a unit test, will crash the compiler. A stack track is provided with a note to submit a bug report. Some SwiftData classes work without error, but this example will crash. Cannot find any workaround and am forced to remain on beta 5 for now. Anyone else experiencing this? Works without error in beta 5.

Compiler crashes in a basic unit test on this line, or any other attempt to instantiate the model.

	let note = Note(content: "Went for a bike ride this morning!")
public final class Note: Identifiable {
	@Attribute(.unique) public private(set) var id: UUID?
	
	public private(set) var createdDate: Date
	
	public private(set) var updatedDate: Date
	
	/// Optional title for this note
	public var title: String? {
		didSet { updatedDate = Date() }
	}
	
	/// Note content
	public var content: String? {
		didSet { updatedDate = Date() }
	}
	
	public init(title: String? = nil, content: String) {
		self.id = UUID()
		self.createdDate = Date()
		self.updatedDate = Date()
		self.title = title
		self.content = content
	}
}

I don't see '@Model' above the class definition.

@Model
public final class Note: Identifiable {...

Sorry, there is a @Model macro, but it was cut off when I coped the code sample.

iOS beta 6 was released today and the notes indicate a long list of SwiftData issues fixed. But no update to Xcode beta 7, so I don't see any way to test the iOS SwiftData improvements in the simulator or debug on a device until Xcode is updated?

I can't edit my code snippet above, but there was a @Model macro and I created a complete Package project to reproduce this compile crash issue and submitted to Apple. The model compiles fine in Xcode beta 5 and the unit test runs successfully. Looks like I may need to wait another week for Xcode beta 7?

Can you try without the Identifiable protocol? I believe this is already applied via the @Model macro (PersistentModel -> Identifiable) so maybe that causes the problem (it shouldn't but you never know in Beta)

Good idea, but I removed Identifiable and the project still crashes. What's especially strange is that this example was part of a larger model that started crashing with beta 6. Other entities compile and run successfully. I isolated this example into a new Package with a simple unit test. The compile stack track includes this:

  1. While finishing conformance for protocol conformance to 'PersistentModel' (in module 'SwiftData') for type 'Note'
  2. *** DESERIALIZATION FAILURE ***

*** If any module named here was modified in the SDK, please delete the *** *** new swiftmodule files from the SDK and keep only swiftinterfaces. *** module 'TestNote', builder version '5.9(5.9)/Apple Swift version 5.9 (swiftlang-5.9.0.128.2 clang-1500.0.40.1)', built from source, non-resilient, loaded from '/Users/dcarlson/Library/Developer/Xcode/DerivedData/TestNote-bfabsndeemmksbfnkuqgjhrdwipm/Build/Products/Debug-iphonesimulator/TestNote.swiftmodule/arm64-apple-ios-simulator.swiftmodule' serialized conformances do not match requirement signature

  1. Try removing didSet and try it
  2. Try removing private set

One way to isolate the problem is to keep commenting out and simplifying your model till it no longer crashes and then you can isolate the problem.

Then file a feedback with the isolated problem.

I removed didSet, private set, and made every attribute optional. Cleaned the build folder, exited and restarted Xcode. And the compile still crashes. This is a very simple example, but within a Swift Package with a single unit test (where the compile crashes). I'll try embedding this package into a simple app. This is all that's left in the model:

@Model

public final class Note {

public var id: UUID?

public var createdDate: Date?

public var updatedDate: Date?

public var title: String?

public var content: String?

public init(title: String? = nil, content: String) {
	self.id = UUID()
	self.createdDate = Date()
	self.updatedDate = Date()
	self.title = title
	self.content = content
}

} `

Another easter egg hunt suggestion. Try removing the UUID or change it to a UUID.String.

Hi all,

Same issue here. It was crashing on beta 6 and I waited for the beta 7, but still the same issue is happening.

Don't know if it helps the following stack trace.

I tried to delete the line shown and by removing it, it compiles, but, well, I can't use Swift Data!

Any updates on this?

Apple Swift version 5.9 (swiftlang-5.9.0.128.106 clang-1500.0.40.1)
2.	Compiling with the current language version
3.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/andreaciani/Developer/Xcode-Projects/RLScoreTracker/RLScoreTracker/Views/PackageView/PackageListViewModel.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(Counterby.(file).PackageListView extension.ViewModel.createNewPackage(in:)@/Users/andreaciani/Developer/Xcode-Projects/RLScoreTracker/RLScoreTracker/Views/PackageView/PackageListViewModel.swift:31:14)
5.	While type-checking statement at [/Users/andreaciani/Developer/Xcode-Projects/RLScoreTracker/RLScoreTracker/Views/PackageView/PackageListViewModel.swift:31:57 - line:36:9] RangeText="{
            let package = Persistency.SWDPackage(name: "", timestamp: Date(), items: [], goal: 0.0)
//            let package = SWDPackage(name: newPackageName, timestamp: Date(), items: [], goal: 0)
//            context.insert(package)
//            self.newPackageName = ""
        "
6.	While type-checking declaration 0x14485abe0 (at /Users/andreaciani/Developer/Xcode-Projects/RLScoreTracker/RLScoreTracker/Views/PackageView/PackageListViewModel.swift:32:13)
7.	While evaluating request PatternBindingEntryRequest((unknown decl), 0, 0)
8.	While type-checking expression at [/Users/andreaciani/Developer/Xcode-Projects/RLScoreTracker/RLScoreTracker/Views/PackageView/PackageListViewModel.swift:32:27 - line:32:99] RangeText="Persistency.SWDPackage(name: "", timestamp: Date(), items: [], goal: 0.0"
9.	While type-checking-target starting at /Users/andreaciani/Developer/Xcode-Projects/RLScoreTracker/RLScoreTracker/Views/PackageView/PackageListViewModel.swift:32:39
10.	While reading from module 'Persistency', builder version '5.9(5.9)/Apple Swift version 5.9 (swiftlang-5.9.0.128.106 clang-1500.0.40.1)', built from source, non-resilient, loaded from '/Users/andreaciani/Library/Developer/Xcode/DerivedData/RLScoreTracker-hahiewpmlftmcygiutlddbpypydu/Build/Products/Debug/Persistency.swiftmodule/arm64-apple-macos.swiftmodule'
11.	While finishing conformance for protocol conformance to 'PersistentModel' (in module 'SwiftData') for type 'SWDPackage'
12.	*** DESERIALIZATION FAILURE ***
*** If any module named here was modified in the SDK, please delete the ***
*** new swiftmodule files from the SDK and keep only swiftinterfaces.   ***
module 'Persistency', builder version '5.9(5.9)/Apple Swift version 5.9 (swiftlang-5.9.0.128.106 clang-1500.0.40.1)', built from source, non-resilient, loaded from '/Users/andreaciani/Library/Developer/Xcode/DerivedData/RLScoreTracker-hahiewpmlftmcygiutlddbpypydu/Build/Products/Debug/Persistency.swiftmodule/arm64-apple-macos.swiftmodule'
serialized conformances do not match requirement signature

Exactly the same issue here, I even tried with a very simple model as below. Trying to create the below in a unit test crashes with the same output as above. Tested on Xcode 15 beta 7.

@Model
class TestModel {
    var front: String

    init(front: String) {
        self.front = front
    }
}

Then in unit tests...

func testSwiftDataImplementation() throws {

    let schema = Schema([TestModel.self])

    let config = ModelConfiguration(isStoredInMemoryOnly: true)
    let container = try ModelContainer(for: schema, configurations: [config])

    let testModel = TestModel(front: "123")
}

Stack trace output very similar to above

1.	Apple Swift version 5.9 (swiftlang-5.9.0.128.2 clang-1500.0.40.1)
2.	Compiling with the current language version
3.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/timrichardson/Documents/projects/FloorPlanner/FloorPlannerTests/FloorPlannerTests.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(FloorPlannerTests.(file).FloorPlannerTests.testSwiftDataImplementation()@/Users/timrichardson/Documents/projects/FloorPlanner/FloorPlannerTests/FloorPlannerTests.swift:19:10)
5.	While type-checking statement at [/Users/timrichardson/Documents/projects/FloorPlanner/FloorPlannerTests/FloorPlannerTests.swift:19:47 - line:27:5] RangeText="{

        let schema = Schema([TestModel.self])

        let config = ModelConfiguration(isStoredInMemoryOnly: true)
        let container = try ModelContainer(for: schema, configurations: [config])

        let testModel = TestModel(front: "123")
    "
6.	While type-checking declaration 0x14f009478 (at /Users/timrichardson/Documents/projects/FloorPlanner/FloorPlannerTests/FloorPlannerTests.swift:26:9)
7.	While evaluating request PatternBindingEntryRequest((unknown decl), 0, 0)
8.	While type-checking expression at [/Users/timrichardson/Documents/projects/FloorPlanner/FloorPlannerTests/FloorPlannerTests.swift:26:25 - line:26:47] RangeText="TestModel(front: "123""
9.	While type-checking-target starting at /Users/timrichardson/Documents/projects/FloorPlanner/FloorPlannerTests/FloorPlannerTests.swift:26:25
10.	While reading from module 'FloorPlanner', builder version '5.9(5.9)/Apple Swift version 5.9 (swiftlang-5.9.0.128.2 clang-1500.0.40.1)', built from source, non-resilient, loaded from '/Users/timrichardson/Library/Developer/Xcode/DerivedData/FloorPlanner-cabknwrmxgbhkbcieyflghtkspqb/Build/Products/Debug-iphonesimulator/FloorPlanner.swiftmodule/arm64-apple-ios-simulator.swiftmodule'
11.	While finishing conformance for protocol conformance to 'PersistentModel' (in module 'SwiftData') for type 'TestModel'
12.	*** DESERIALIZATION FAILURE ***
*** If any module named here was modified in the SDK, please delete the ***
*** new swiftmodule files from the SDK and keep only swiftinterfaces.   ***
module 'FloorPlanner', builder version '5.9(5.9)/Apple Swift version 5.9 (swiftlang-5.9.0.128.2 clang-1500.0.40.1)', built from source, non-resilient, loaded from '/Users/timrichardson/Library/Developer/Xcode/DerivedData/FloorPlanner-cabknwrmxgbhkbcieyflghtkspqb/Build/Products/Debug-iphonesimulator/FloorPlanner.swiftmodule/arm64-apple-ios-simulator.swiftmodule'
serialized conformances do not match requirement signature

I was original poster for this thread. After finding the problem still exists in beta 7 and discovered the issue is caused by defining a SwiftData model in a Package. The compilation error occurs when the model package is imported into a different package, including unit test.

@qmay provided this confirmation, with an awkward workaround. Check this GitHub issue — qmay 2 days ago

I submitted a Feedback FB13034576 including sample to reproduce.

I also confirmed that copying the exact swift file directly into an app project will compile and run without error. For now, I've been forced to abandon package modularity and copy may SwiftData code directly into my app project.

I've submitted FB13085924 and noted that it likely duplicates the issue in FB13034576.

For anyone else seeing this, from the related Github thread, it seems to be due to the custom init defined.

Temporarily making the init only accessible via a static method seems to be a temporary bypass to avoid the build error

https://github.com/apple/swift/issues/67823#issuecomment-1673842437

add this inside your swift model, and only instantiate the model via a static method when calling from the parent package.

public static func create(value: String) -> Note {
    Note(value)
}

hopefully this is fixed in the next release.

I recieved a response on FB13085924: "Investigation complete - Works as currently designed". Not the answer I was hoping to get.

Swift compiler crashes with simple SwiftData model
 
 
Q