Why does the Swift 6 compiler warn about extension Date: Identifiable in an app, and what does marking it @retroactive change?
answer
- whose type, whose protocol
- conformances are process-wide
- the owner may add it later
- silences the warning, nothing more
basics
~20 sDate and Identifiable both come from other modules, so this is a retroactive conformance: if Foundation later declares the same one, which implementation wins is not under your control. Writing @retroactive just acknowledges the risk and silences the warning.
solid answer
~40 sSince the Swift 6.0 compiler (SE-0364), an extension that conforms an **imported type** to an **imported protocol** draws the 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". Conformances are unique within a process, so if Foundation adds its own, an app built earlier may get either one at run time, and a rebuild warns that yours is redundant and stops using your `id`. `extension Date: @retroactive Identifiable` says you accept that; it changes nothing at run time. The warning does not fire when you own the type or the protocol, or for modules in the same package. Safer options are a wrapper type you own or a protocol you own.
code
plaintext · 2 lineswarning: 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
note: add '@retroactive' to silence this warninggo deeper
Recall when the warning appears: you add a protocol conformance to a type you do not own, for a protocol you also do not own.
Explain why it is risky: a conformance is one fact per process, so a second declaration by the type's owner leaves no single winner.
Show the production cost: identifiers or encodings that silently change after an OS or SDK update, and a wrapper type as the durable fix.
Set where retroactive conformances may live: never in a shared library that every client imports, only in the app target, each one marked and justified.
## What makes a conformance retroactive **Retroactive modeling** is the Swift book's name for extending a type you do not own, for example to adopt a protocol. A **retroactive conformance**, in the sense of Swift Evolution proposal SE-0364, is the narrow case where an extension declares a conformance and: - the **type** was declared in a different module from the extension (here `Date`, from Foundation), and - the **protocol** was declared in a different module from the extension too (here `Identifiable`, from the standard library). Since the Swift 6.0 compiler, that case draws a warning in both Swift 5 and Swift 6 language mode: ```plaintext 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 note: add '@retroactive' to silence this warning ``` ## Why it is risky A **conformance** is a single fact about a type and a protocol, and the proposal puts it bluntly: conformances are globally unique within a process. Two modules declaring `Date: Identifiable` is a conflict with no defined winner. The risk is real on Apple platforms because the type's owner ships with the OS and the SDK: 1. Your app ships `extension Date: Identifiable { var id: TimeInterval { timeIntervalSince1970 } }` and persists those ids. 2. A later Foundation declares the same conformance with a different `id`. 3. Your existing binary may now see either implementation at run time; the proposal calls it indeterminate. 4. When you rebuild with the new SDK, the compiler warns that the conformance "was already stated in the type's module 'Foundation'" and notes that your `id` "will not be used to satisfy the conformance". Foundation's version wins, and your persisted ids no longer match. The Swift 6.4 compiler produced exactly that redundancy warning for a conformance Foundation already has, such as `URLComponents: CustomStringConvertible`. In a **library**, it is worse: the conformance reaches every client that imports the library, and those clients usually do not know it came from you. ## What `@retroactive` does and does not do `extension Date: @retroactive Identifiable` tells the compiler and the next reader that you know the conformance is retroactive. It: - silences the warning; - changes **nothing** at run time, so the risk above remains; - is checked: on a conformance of a type your module declares, the compiler reports that the attribute does not apply, as a warning in Swift 5 mode and an error in Swift 6 mode. For code that must also build with compilers older than 6.0, the proposal's fallback is to fully qualify the names, `extension Foundation.Date: Swift.Identifiable`, which the compiler also accepts without the warning. ## When the warning does not apply | Extension | Warning? | |---|---| | `extension Date: Identifiable` in your app | Yes | | `extension Date: AppLogging` with your own protocol | No: you own the protocol | | `extension LogEntry: Identifiable` with your own type | No: you own the type | | `extension Date { func iso() -> String }` | No: no conformance is declared | | Type and protocol from modules in your own package | No: same-package modules are exempt | The same-package exemption comes from an amendment to SE-0364, and the Swift 6.4 compiler honours it: modules built with the same package name do not trigger the warning. ## Safer alternatives - **Wrap the type**: `struct LoggedAt: Identifiable { let date: Date; var id: TimeInterval { ... } }`. You own `LoggedAt`, so nobody else can declare its conformances. - **Own the protocol**: declare `protocol LogIdentifiable` in your module and conform `Date` to that. - **Keep it at the edge**: if you do need the retroactive conformance, put it in the app target, never in a shared library, and mark it `@retroactive` with a comment on why. ## In an interview Name both conditions (imported type, imported protocol), explain process-wide uniqueness and the OS-update scenario, and be clear that `@retroactive` is an acknowledgement, not a fix.
- What happens to a Swift app's Date: @retroactive Identifiable conformance if Foundation later ships its own?Two cases. A binary already built may meet both conformances at run time, and SE-0364 says it is indeterminate which one wins, so identifiers your app persisted can stop matching. When you rebuild against the new SDK, the compiler warns that the conformance "was already stated in the type's module" and notes that your `id` "will not be used to satisfy the conformance", so Foundation's wins from then on.
- In Swift, what happens if you write @retroactive on a conformance of a type your own module declares?The compiler reports that the 'retroactive' attribute does not apply because the type is declared in this module. In Swift 5 language mode that is a warning; in Swift 6 language mode it is an error. The attribute is only valid where both the type and the protocol come from other modules.
- @retroactive makes your conformance win over one Foundation adds later.
- Any extension of Date, even one that only adds methods, triggers the warning.
- The warning only appears in Swift 6 language mode.
- Conformances are scoped to the module that declares them, so duplicates are harmless.
- Conforming Date to a protocol your app declares also draws the retroactive warning.
Assumes the Swift 6.0 or later compiler (checked on 6.4): SE-0364 added this warning and the @retroactive attribute in 6.0, and it fires in Swift 5 and Swift 6 language mode alike.