# Why do we prohibit redundant declarations of conformance to a protocol?

**URL:** <https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882>\
**Category:** Discussion\
**Tags:** protocols, conformance\
**Created:** [March 24, 2024, 10:15pm UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882 "2024-03-24T22:15:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [March 24, 2024, 10:15pm UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/1 "2024-03-24T22:15:19Z")

</div>

I was just experimenting with some ideas relating to serialization and I got blocked by a compilation error that doesn't seem necessary.

Here's the upshot:

```swift
typealias Foo = Codable & ProtocolA & ProtocolB
typealias Bar = Codable & ProtocolC & ProtocolD

struct Baz: Foo, Bar { // Redundant conformance of 'Baz' to protocol 'Decodable'

}

```

I found [this related post](https://forums.swift.org/t/redundant-conformance-error-using-protocol-composition/21415) with this comment:

> [@Redundant conformance error using protocol composition](https://forums.swift.org/t/redundant-conformance-error-using-protocol-composition/21415/4):
>
> We just managed to fix this with [[5.1] [Name lookup] Deduplicate nominal declarations found via resolveTypeDeclsToNominal by DougGregor · Pull Request #26177 · apple/swift · GitHub ](https://github.com/apple/swift/pull/26177).
> 
> Doug

which I think indicates that duplicates should be permitted (but as I'm describing, that doesn't seem to be true).

Is there a good reason that we don't allow redundant declarations of protocol conformance?

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [March 25, 2024, 1:38am UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/2 "2024-03-25T01:38:38Z")

</div>

Are you speaking exclusively about redundancy to the right of the colon, or would you expect to be able to declare two separate conformances to Foo and Bar?

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [March 25, 2024, 5:10am UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/3 "2024-03-25T05:10:10Z")

</div>

OTOH this is fine:

```swift
protocol ProtocolA {}
protocol ProtocolB {}
protocol ProtocolC {}
protocol ProtocolD {}

protocol Foo: Codable, ProtocolA, ProtocolB {}
protocol Bar: Codable, ProtocolC, ProtocolD {}

struct Baz: Foo, Bar {} // ✅

```

Whether this disparity is a bug or a feature I don't know.

---

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [March 25, 2024, 10:34am UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/4 "2024-03-25T10:34:47Z")

</div>

I wondering why we can’t redundantly _declare_ the conformance. The fact that we can’t redundantly _implement_ the conformance is no mystery to me.

---

<div class="post-metadata">

**Author:** ![tevelee](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tevelee/32/9775_2.png) [@tevelee](https://forums.swift.org/u/tevelee)\
**Post date:** [March 25, 2024, 11:02am UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/6 "2024-03-25T11:02:01Z")

</div>

FYI Slava recently pointed this out on the [noncopyable generics](https://forums.swift.org/t/se-0427-noncopyable-generics/70525/20) thread.

> [@SE-0427: Noncopyable Generics](https://forums.swift.org/t/se-0427-noncopyable-generics/70525/20):
>
> On the subject of redundant requirements in general:
> 
> > [@SE-0427: Noncopyable Generics](https://forums.swift.org/t/se-0427-noncopyable-generics/70525/9):
> >
> > A question about the new `Copyable` protocol: what happens if you try to manually write a redundant `Copyable` conformance? Would it make sense for this to result in an error or warning?
> 
> So back in the day (Swift 5.7 or 5.8, I forget now) we used to diagnose redundant generic requirements. This was disabled by default at some point and then I removed the code recently. So writing `T: Copyable` anywhere is just silently ignored. The compiler happily accepts this for example even though the entire `where` clause is pointless:
> 
> ```swift
> func f<T: Sequence>(_: T) where T: Sequence, T.Iterator: IteratorProtocol {}
> 
> ```

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [March 25, 2024, 1:40pm UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/7 "2024-03-25T13:40:27Z")

</div>

It’s not a bug or a feature, it’s two different code paths. The inheritance clause of a protocol is a shortcut for a series of `Self: Foo` requirements so redundancy is just ignored there. We could do the same for concrete types, I think the potential confusion from someone declaring a duplicate entry is low.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [March 25, 2024, 4:21pm UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/8 "2024-03-25T16:21:33Z")

</div>

> [@Slava\_Pestov](#):
>
> It’s not a bug or a feature, it’s two different code paths.

If this disparity is intentional - that's a feature, otherwise that's a bug, I can't see how it could be anything else. Could be the case that whoever implemented it didn't have the prescribed intended behaviour to match, and whatever we have now could be retroactively called the intended behaviour, in which case the current behaviour is "a feature".

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [March 25, 2024, 4:45pm UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/9 "2024-03-25T16:45:18Z")

</div>

Sure, but that requires a _decision_ about what the "intended behavior" actually is, which isn't always obvious/trivial.

---

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [March 25, 2024, 5:15pm UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/10 "2024-03-25T17:15:43Z")

</div>

Are there any specific complications that you foresee if we allow the same protocol to appear twice in a conformance declaration?

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [March 25, 2024, 5:23pm UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/11 "2024-03-25T17:23:17Z")

</div>

Not that I see, no. Given that we allow this to be expressed in a `where` clause without issue it seems fine to me to allow it directly in the inheritance clause (particularly when it's incidental by way of two typealiases; IMO that the specific code in your example at top-of-thread gets diagnosed leans toward bug).

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [March 26, 2024, 3:18am UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/12 "2024-03-26T03:18:11Z")

</div>

I think there is an important difference between:

```swift
typealias A = P
typealias B = P

struct S: A, B {}

```

…and:

```swift
struct S: P, P {}

```

The latter _probably_ is an error, because even though it's technically harmless it _could_ mean the user meant to write "P2" for one of the conformances, or somesuch. There's also no good reason (that I can think of) for that syntax (I consider clumsy code generators not a good reason). It'd be wise to warn about it.

But for the former, those declarations can be spread widely apart within the code, might be complicated by the typealiases including - now, in the past, or in the future - other protocols as well, etc. It's simply less likely that it's an error. Conceptually `A` and `B` might be _different_ things even if in practice they overlap or are even identical. And maybe they're _not_ always identical, but it depends on some `#if`s or somesuch, or they _weren't_ identical in the past but became so but can't be removed or consolidated for backwards compatibility reasons, etc.

i.e. the compiler should really only be looking at the _input text_ for this diagnostic, not caring about the semantics. Just checking for the same words in a single protocol list (whether that be `,`-separated or `&`-separated).

---

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [March 26, 2024, 1:22pm UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/14 "2024-03-26T13:22:00Z")

</div>

> [@wadetregaskis](#):
>
> i.e. the compiler should really only be looking at the _input text_ for this diagnostic, not caring about the semantics.

I can get on board with this, except that I would add that I also think the diagnostic for when the same protocol is explicitly written twice should be downgraded to a warning.

---

<div class="post-metadata">

**Author:** ![Geordie\_J](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/geordie_j/32/839_2.png) [@Geordie\_J](https://forums.swift.org/u/Geordie_J)\
**Post date:** [March 31, 2024, 6:11pm UTC](https://forums.swift.org/t/why-do-we-prohibit-redundant-declarations-of-conformance-to-a-protocol/70882/15 "2024-03-31T18:11:56Z")

</div>

Agreed. I think this should probably just be a warning
