sindresorhus / Defaults

💾 Swifty and modern UserDefaults
https://swiftpackageindex.com/sindresorhus/Defaults/documentation/defaults
MIT License
1.97k stars 117 forks source link

Cannot conform to protocol on Xcode 13.3 beta - macOS 12.3 #93

Closed Wouter01 closed 1 year ago

Wouter01 commented 2 years ago

Yesterday I updated Xcode to version 13.3, and now there are a number of errors about DefaultsSerializable. When creating a new project and adding the example code from here, I get the same error.

image
sindresorhus commented 2 years ago

This looks like a Swift regression: https://bugs.swift.org/projects/SR/issues/SR-15807

ibash commented 2 years ago

Workaround:

struct MyBridge<Value: Codable>: DefaultsCodableBridge {}

extension User: Defaults.Serializable {
  static let bridge = MyBridge<User>()
}
quantamrhino commented 2 years ago

Any workaround for enums?

sindresorhus commented 2 years ago

@hank121314 It doesn't look like Swift will fix this in time for Xcode 13.3 final. Any idea how we could work around this in Defaults for now?

hank121314 commented 2 years ago

This issue seems to occur when using the type which has multiple protocol conformance across module. The temporary workaround might be declared the protocol conformance in the same module. Here are my workaround steps:

  1. Create Defaults+WorkAround.swift in the module which is using Defaults.
  2. Copy the codes below into Defaults+WorkAround.swift
    
    import Defaults
    import Foundation

extension Defaults.Serializable where Self: Codable { public static var bridge: Defaults.TopLevelCodableBridge { Defaults.TopLevelCodableBridge() } }

extension Defaults.Serializable where Self: Codable & NSSecureCoding { public static var bridge: Defaults.CodableNSSecureCodingBridge { Defaults.CodableNSSecureCodingBridge() } }

extension Defaults.Serializable where Self: Codable & NSSecureCoding & Defaults.PreferNSSecureCoding { public static var bridge: Defaults.NSSecureCodingBridge { Defaults.NSSecureCodingBridge() } }

extension Defaults.Serializable where Self: Codable & RawRepresentable { public static var bridge: Defaults.RawRepresentableCodableBridge { Defaults.RawRepresentableCodableBridge() } }

extension Defaults.Serializable where Self: Codable & RawRepresentable & Defaults.PreferRawRepresentable { public static var bridge: Defaults.RawRepresentableBridge { Defaults.RawRepresentableBridge() } }

extension Defaults.Serializable where Self: RawRepresentable { public static var bridge: Defaults.RawRepresentableBridge { Defaults.RawRepresentableBridge() } } extension Defaults.Serializable where Self: NSSecureCoding { public static var bridge: Defaults.NSSecureCodingBridge { Defaults.NSSecureCodingBridge() } }

extension Defaults.CollectionSerializable where Element: Defaults.Serializable { public static var bridge: Defaults.CollectionBridge { Defaults.CollectionBridge() } }

extension Defaults.SetAlgebraSerializable where Element: Defaults.Serializable & Hashable { public static var bridge: Defaults.SetAlgebraBridge { Defaults.SetAlgebraBridge() } }



And to achieve this workaround we might also need to public some bridge initializers.
Not sure whether this is an acceptable solution. 😢 
sindresorhus commented 2 years ago

Yeah, that's a good enough workaround.

nab0y4enko commented 2 years ago

Have same issue on Release Xcode 13.3 version 😢

hank121314 commented 2 years ago

Hi @nab0y4enko. I am working on this, sorry for the inconvenience 😞 .

RoyRao2333 commented 2 years ago

Hi @nab0y4enko. I am working on this, sorry for the inconvenience 😞 .

Still buggy in 13.3 stable release...

It's always a mess after upgrading Xcode, especially with a newer Swift version... Thanks for the hard work anyway!

Hoping to see this issue to be fixed soon...

sindresorhus commented 2 years ago

I recommend anyone looking here to also report this through Feedback Assistant. Just link to the Swift issue.

sindresorhus commented 2 years ago

https://github.com/sindresorhus/Defaults/releases/tag/v6.2.1

owenzhao commented 2 years ago

Workaround + v6.2.1 worked.😄😄

Maschina commented 2 years ago

To whom it may concern that have custom Codable struct/class and looking for workaround (using the example of NSRect):

extension NSRect: Defaults.Serializable {
    // TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
    public static var bridge: Defaults.TopLevelCodableBridge<NSRect> { Defaults.TopLevelCodableBridge() }
}
sindresorhus commented 2 years ago

@hank121314 Congrats on getting your pull request merged on the Swift repo: https://github.com/apple/swift/pull/42293 And thank you so much for fixing this issue in Swift 🎉

sindresorhus commented 1 year ago

Closing as Xcode 14 is out now.