swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.46k stars 10.35k forks source link

[SR-13274] Incorrect diagnostic "inherits from itself" on using CRTP #55714

Open typesanitizer opened 4 years ago

typesanitizer commented 4 years ago
Previous ID SR-13274
Radar rdar://problem/66034611
Original Reporter @typesanitizer
Type Bug
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, TypeChecker | |Assignee | None | |Priority | Medium | md5: d62c6c6302980168d341cac388f1d0b1

Issue Description:

Consider the code below:

protocol P {
  associatedtype AT: P
}

class B<T : P> : P {
  typealias AT = T.AT
}

class C : B<C> {
  //  ^---|-- error: 'C' inherits from itself
  //      ^----- error: type 'C' does not conform to protocol 'P'
  typealias AT = C
} 

It's not super obvious to me whether this code should compile or not, because this probably requires a complex notion of consistency-checking. But the diagnostic error: 'C' inherits from itself is inaccurate, C is not inheriting from itself.

typesanitizer commented 4 years ago

cc @AnthonyLatsis – not sure if this is related to your upcoming work in https://github.com/apple/swift/pull/31092

typesanitizer commented 4 years ago

@swift-ci create

typesanitizer commented 4 years ago

(I don't have an actual use case for this, I was trying to test how far I could stretch the recursion, since basic examples seemed to work fine.)

AnthonyLatsis commented 4 years ago

Yeah, this is a separate issue. We recurse into SuperclassTypeRequest while looking up C: P for B<C>. We need a way to ask for the unspecialized conformance to avoid the circularity.
Reduced case:

protocol P {}

class B<T: P> : P {}

class C: B<C> {}