Crash when protocol conformance is defined in other modules

I have 3 modules:

Module 1:

struct Apple {}

Module 2:

protocol Fruit{}
extension Apple: Fruit {}

Module 3:

let any: Any = Apple()
let fruit = any as! Fruit // <- force conversion failed, any doesn't conform to Fruit

struct Basket where Content: Fruit {}
let appleBasket: Basket<Apple> = .init() // another crash by EXEC_BAD_ACCESS, but it passes compiling :thinking:

if I move

extension Apple: Fruit {}

to the module 3, the crashes are all gone.

Could someone explain to me how extension works in Swift?

This thread may help: Retroactive Conformances vs. Swift-in-the-OS Doing retroactive conformance from other modules is problematic, as you discovered.

1 Like

Please include your various visibility modifiers. But in general I don’t think the casting will work if the conformance isn’t publicly available and imported.