Search code examples
swift

Xcode 16 warning "Extension declares a conformance of imported type ... this will not behave correctly"


With Xcode 16 I'm getting this warning

Extension declares a conformance of imported type 'Date' to imported protocol 'Identifiable'; this will not behave correctly if the owners of 'Foundation' introduce this conformance in the future

for this simple extension

extension Date: Identifiable {
    public var id: TimeInterval { timeIntervalSince1970 } // Warning here
}

The same warning appears for many other conformances like CustomStringConvertible, Equatable, Hashable, Sendable, etc. What should I do with those warnings?


Solution

  • This warning was introduced by SE-0364 and you have four options how to deal with it:

    1. Don't make such kind of extensions. You can wrap the type in question into your own wrapper so you can fully control the conformance of the wrapper to the protocol (won't work with Sendable unfortunately).
    struct MyDate: Identifiable {
        var rawDate: Date
        var id: TimeInterval { rawDate.timeIntervalSinceReferenceDate }
    }
    
    1. Ask the author of the module to add conformance you need (arguably the best option, but also the slowest one)

    2. Mark this conformance with new @retroactive attribute (acknowledging you understand the risks):

    extension Date: @retroactive Identifiable {
        public var id: TimeInterval { timeIntervalSince1970 }
    }
    

    This would be an error with Xcode 15 though (unless you wrap it with #if hasFeature(RetroactiveAttribute)).

    1. Fully qualify the names of the type and the protocol with module names (to acknowledge the risks).
    extension Foundation.Date: Swift.Identifiable {
        public var id: TimeInterval { timeIntervalSince1970 }
    }
    

    Not as fancy as @retroactive, but compatible with previous Xcode versions as is.