Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-13236] Improve diagnostic when a protocol composition type is incorrectly used where a class type is expected #55677

Open
typesanitizer opened this issue Jul 16, 2020 · 2 comments
Assignees
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation improvement type checker Area → compiler: Semantic analysis

Comments

@typesanitizer
Copy link

Previous ID SR-13236
Radar rdar://64672825
Original Reporter @typesanitizer
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, TypeChecker
Assignee @theblixguy
Priority Medium

md5: f95bcbb282da0e6d9131672cb0f3190a

Issue Description:

import Foundation
class C<T: NSObject> { // note: requirement specified as 'T' : 'NSObject' [with T = NSObject & P]
    var t: T?
}
protocol P {}
typealias TA = C<NSObject & P> // error: 'C' requires that 'NSObject & P' inherit from 'NSObject'

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:

class C<T: AnyObject> {
    var t: T?
}
protocol P {}
typealias TA = C<AnyObject & P> // error: 'C' requires that 'AnyObject & P' be a class type 

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.

@typesanitizer
Copy link
Author

@theblixguy just double-checking here: is there a PR for this/are you working on it locally?

@theblixguy
Copy link
Collaborator

Yeah, I am working on it. I'll put up a PR soon.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation improvement type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants