Skip to content

Generated code doesn't compile in Swift 6 Language Mode #91

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

Open
1 task done
aeigenmann opened this issue Feb 19, 2025 · 2 comments
Open
1 task done

Generated code doesn't compile in Swift 6 Language Mode #91

aeigenmann opened this issue Feb 19, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@aeigenmann
Copy link

Is there an existing issue?

Build info

  • ObjectBox version: 4.1.0-beta.2
  • OS: macOS 15.3.1
  • Device/chipset: Apple M1 Pro

Steps to reproduce

Entity:

// objectbox: entity
struct Recipe: Identifiable, Codable {
    var id: Id = 0
    var name: String
}

Generated code:

extension Recipe: ObjectBox.EntityInspectable {
    internal typealias EntityBindingType = RecipeBinding

    /// Generated metadata used by ObjectBox to persist the entity.
    internal static var entityInfo = ObjectBox.EntityInfo(name: "Recipe", id: 1)

    internal static var entityBinding = EntityBindingType()
...
}

Compilation Errors:

Static property 'entityInfo' is not concurrency-safe because it is nonisolated global shared mutable state
Static property 'entityBinding' is not concurrency-safe because it is nonisolated global shared mutable state

Expected behavior

The generated code should be compilable.

Actual behavior

Compilation error

@aeigenmann aeigenmann added the bug Something isn't working label Feb 19, 2025
@greenrobot
Copy link
Member

greenrobot commented Feb 20, 2025

I guess these must be immutable. By using static let instead of static var, you’re making the properties immutable and inherently thread-safe, which satisfies Swift’s concurrency rules.

@aeigenmann
Copy link
Author

aeigenmann commented Feb 20, 2025

Unfortunately, static let alone is not enough. This leads to the error messages:

Static property 'entityInfo' is not concurrency-safe because non-'Sendable' type 'EntityInfo' may have shared mutable state
Static property 'entityBinding' is not concurrency-safe because non-'Sendable' type 'Recipe.EntityBindingType' (aka 'RecipeBinding') may have shared mutable state

As a workaround, a nonisolated(unsafe) helps for the time being.

    /// Generated metadata used by ObjectBox to persist the entity.
    nonisolated(unsafe) internal static var entityInfo = ObjectBox.EntityInfo(name: "Recipe", id: 1)

    nonisolated(unsafe) internal static var entityBinding = EntityBindingType()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants