I'm quite sympathetic towards the following being an error:
protocol P {}
struct S : P {}
extension S : P {} // error: Redundant conformance of 'S' to protocol 'P'
as it's most likely a sign that the user's trying to conform the type S
to the protocol P
in two different ways, which isn't something that's well-formed. This is most apparent when you consider conditional conformances, where we don't allow multiple conformances to the same protocol, even if the constraints are clearly disjoint:
protocol P {
func foo()
}
struct S<T> {}
// error: Redundant conformance of 'S<T>' to protocol 'P'
extension S : P where T == Int {
func foo() {}
}
extension S : P where T == String {
func foo() {}
}
However, if the duplicate conformance shows up in the same inheritance clause, I think it's reasonable to downgrade to a warning, as it's fairly clear what the user's intent is. The diagnostic for duplicate inherited types in an inheritance clause (note this is separate from the above "redundant conformance" diag) is emitted in TypeChecker::checkInheritanceClause
, which is called when the decl is validated – so I think we should be okay to emit a warning instead, but I'm not entirely sure.
I do agree that we should emit a warning for things like P & P
, though I'm not so sure about things like P & Q
where protocol Q : P {}
– I think there are valid arguments for and against.
Should we open a separate thread to discuss the extent to which we should diagnose such redundancies? If possible, I would really like to keep this thread focussed on why the existing diagnostics for the following:
struct S : P, P {}
protocol Q : AnyObject, AnyObject {}
struct S1 : Any, Any {}
are errors rather than warnings, such that I can make sure [Sema][GSB] Warn about redundant ': Any' conformances & constraints by hamishknight · Pull Request #17425 · swiftlang/swift · GitHub has both correct and clean diagnostic behaviour for duplicate Any
conformances.
Like Huon, I also didn't find this while searching – thanks for the link! :)