Public extension in module is not visible in file

I have this issue with my code.

I have some code that bridges a C library I wrote.

The code I'm trying to compile is this.

import SharedEngineC
import Shared
import SharedC

open class Game: CObject<GamePtr>, JSONCode {

	convenience init(
		code: Code,
		id: Int,
		randomizer: Randomizer? = nil,
		owner: User
	) throws {
		self.init(
			ptr: try Error.handle({ error in
				return .init(
					code: code.ptr,
					id: UInt64(id),
					randomizer: randomizer?.ptr,
					owner: owner.ptr,
					error: error)
			}))
	}

}

But when I try to compile it, the compiler complains with the error Generic class 'CObject' requires that 'UserPtr' conform to 'CObjectPtr'.

However when I look at the "header"-file for Shared, I can see that it is clearly there and is public.

extension UserPtr : Shared.CObjectPtr, Shared.CJSONCoder, Shared.CHashable, Shared.CEntity {

    public typealias Item = Shared.User
}

But the compiler cannot see it.

If I add it manually in the file like this it will work.

import SharedEngineC
import Shared
import SharedC

extension UserPtr: CObjectPtr {
	public typealias Item = User
}

open class Game: CObject<GamePtr>, JSONCode {

	convenience init(
		code: Code,
		id: Int,
		randomizer: Randomizer? = nil,
		owner: User
	) throws {
		self.init(
			ptr: try Error.handle({ error in
				return .init(
					code: code.ptr,
					id: UInt64(id),
					randomizer: randomizer?.ptr,
					owner: owner.ptr,
					error: error)
			}))
	}

}

What am I doing wrong. Do I need to set some compiler flag in order for the Swift compiler to see the public extension?

I moved this from “Compiler Development” to “Using Swift”.


Your post repeatedly mentions UserPtr, but that does not appear in your code example.

1 Like

UserPtr comes from the C library, so it's a C type.

It is declared like this: typedef void* UserPtr __attribute__((__swift_newtype__(struct)));.

@Nevin To be honest, I think this is a compiler bug.

This is why I think it is a compiler bug.

I've done some refactoring, and now when I compile I get the warning for the above extra extension.

Conformance of 'UserPtr' to protocol 'CObjectPtr' was already stated in the protocol's module 'Shared'

But if I remove the extension again I get the error

Generic class 'CObject' requires that 'UserPtr' conform to 'CObjectPtr'

So it's like the compiler can't decide if it is implemented or not.