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-3038] Cannot pass in a protocol type to a constrained generic parameter #45628

Closed
swift-ci opened this issue Oct 24, 2016 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-3038
Radar None
Original Reporter plivesey (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Swift 3, playground

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 2fb3ab1d24786f3528652546206fc0aa

duplicates:

  • SR-55 non-@objc protocol existentials do not conform to their own protocol type

Issue Description:

If a generic specifies <T: SomeProtocol>, you cannot pass in an object with type SomeProtocol. This doesn't make sense since presumably, a type should conform to itself.

To reproduce, put this in a playground:

protocol P {}

class A: P {}

func foo<T: P>(_ arg: T) {
}

let a = A()
foo(a) // compiles

foo(a as P) // fails to compile

Expected: Compilation succeeds
Actual: Compilation failed

@belkadan
Copy link
Contributor

This is correct in the general case: not all protocols conform to themselves. The simplest case is a protocol with initializer requirements: which concrete type would you construct?

But for protocols that do allow this, is it okay to use them in this way? It's tricky.

@swift-ci
Copy link
Collaborator Author

Comment by Peter Livesey (JIRA)

I did wonder if there was a edge case like that that I hadn't forseen. That does make sense.

Presumably, Swift doesn't currently split between these types of protocols (reasonable). In that case, is it possible to have another error message for this? Like: "Protocol types cannot be used as a variable bound to a generic constraint." Or something like that which is worded better and more correct terminology?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants