Why do I get invalid redeclaration in one case and not the other

I was exploring some things with the swift language and came across this example:

// Swift Version: 4.1

// ------ Example 1 ---- // No invalid redeclaration error
// I expected this
protocol VersionedProtocol {
associatedtype Version : VersionType
}

protocol VersionType {}
enum Version0x0x0 : VersionType {}
enum Version0x0x1: VersionType {}

extension VersionedProtocol where Version == Version0x0x0 {
func foo() {}
}

extension VersionedProtocol where Version == Version0x0x1 {
func foo() {}
}

// ---- Example 2 ----- // invalid redeclaration Foo
// I did not expect this
protocol VersionedProtocol {
associatedtype Version : VersionType
}

protocol VersionType {}
enum Version0x0x0 : VersionType {}
enum Version0x0x1: VersionType {}

extension VersionedProtocol where Version == Version0x0x0 {
typealias Foo = Int
}

extension VersionedProtocol where Version == Version0x0x1 {
typealias Foo = String
}

// I expected the errors in 3 and 4
// ---- Example 3 ----- // invalid redeclaration foo()
func foo() {}
func foo() {}

// ---- Example 4 ---- // invalid redeclaration Foo
typealias Foo = Int
typealias Foo = String

In example 3 and 4, I expect invalid redeclaration (specifically: invalid redeclaration foo() and invalid redeclaration Foo respectively), that makes sense to me. But why don't I get an invalid redeclaration in example 1 if I get an invalid redeclaration in example 2.

That's a known bug. The generics system is sometimes still unstable on these edge cases related to constrained extensions and conditional conformances. As an alternative, you can do it this way:

protocol VersionType {
    associatedtype Foo
}
protocol VersionedProtocol {
    associatedtype Version: VersionType
    typealias Foo = Version.Foo
}

enum Version0x0x0: VersionType { typealias Foo = Int }
enum Version0x0x1: VersionType { typealias Foo = String }

@Jens Do you happen to have filed this?

1 Like

Ahh I see, thank you very much. Before I wrote this post, I logged it as a bug on bugs.swift.org: https://bugs.swift.org/browse/SR-8268. Since this is a known issue, should I remove my bug report? And do you know the bug post that talks about this issue?

Don't worry, if there is an existing issue filed, which is likely the case, someone will eventually resolve yours as a dupe.