Why are opaque C pointers not imported as distinct types?

It’s hard to say. Swift versions apply on a per-library basis, so that you can use Swift 6 while still depending on a Swift 5 package. But what happens when the Swift 5 library vends an API that uses one of these OpaquePointer typedefs? The Swift 6 client had better see that as OpaquePointer, or it won’t find the write symbol name in the library binary. So we’d be talking about two Swift users of the same C API potentially treating the C API differently, within a single compilation.

I will note that if you control the C library in question, annotating the typedef as swift_newtype(struct) will give you a distinct type with OpaquePointer as its raw value. Not at all a complete answer, maybe not even a good one, but it’s there.

EDIT: swift_wrapper is the preferred way to spell it, not swift_newtype! My bad. (This is the attribute under NS_EXTENSIBLE_STRING_ENUM.)

1 Like