[SR-13236] Improve diagnostic when a protocol composition type is incorrectly used where a class type is expected #55677
Labels
compiler
The Swift compiler in itself
diagnostics QoI
Bug: Diagnostics Quality of Implementation
improvement
type checker
Area → compiler: Semantic analysis
Additional Detail from JIRA
md5: f95bcbb282da0e6d9131672cb0f3190a
Issue Description:
The diagnostic here is less than ideal. One might think "why doesn't the type NSObject & P inherit from NSObject, the dynamic type must be a subclass of NSObject, right?".
We already have a special-case diagnostic for AnyObject in this respect:
The diagnostic for the first case should also be changed to be clearer.
I suggest that both these diagnostics be made a bit more verbose.
> "error: 'C' requires that the generic parameter 'T' be a class type, but 'NSObject & P' is a protocol composition type"
(and similarly for the AnyObject case)
The reason this is not permitted is (paraphrasing Slava slightly):
> NSObject & P is a protocol type, not a concrete type. It would introduce soundness issues if we allowed this. For example, imagine if C called an initializer on T. It would not be able to do that at runtime if T was bound to NSObject & P, since there’s no concrete class type to construct.
It would be good to explain this in a small educational note.
The text was updated successfully, but these errors were encountered: