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-6668] Protocol property returning Self triggers "can only be used as a generic constraint" error #49217

Closed
ole opened this issue Dec 27, 2017 · 5 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@ole
Copy link
Contributor

ole commented Dec 27, 2017

Previous ID SR-6668
Radar rdar://57092093
Original Reporter @ole
Type Bug
Environment

Swift version 4.0.3, Xcode 9.2 (9C40b), macOS 10.13.2 (17C88)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @slavapestov
Priority Medium

md5: cf582c140c3c743b71f1044e04c0d262

is duplicated by:

  • SR-11748 Compiler does not reject invalid code

Issue Description:

As @slavapestov explained to me in the comments, a protocol with a Self requirement in covariant position (i.e. as a method return type) doesn't prohibit the use of the protocol as an existential.

Example:

protocol MethodBuilder {
    func builder() -> Self
}
var methodBuilders: [MethodBuilder] = []
// no error

The same should be true for property requirements returning Self, but that is currently (Swift 4.1) not implemented.

So this code fails but shouldn't:

protocol PropertyBuilder {
    var builder: Self { get }
}
var propertyBuilders: [PropertyBuilder] = []
// error: protocol 'PropertyBuilder' can only be used as a generic constraint because it has Self or associated type requirements

Notes:
I saw this in Patterns for Working With Associated Types by Benedikt Terhechte (search for "once you use a method instead of a property").

@slavapestov
Copy link
Member

We allow Self in covariant position. So actually the protocol containing the get-only property should be accepted as an existential type also, however this isn't implemented yet.

@ole
Copy link
Contributor Author

ole commented Apr 1, 2018

@slavapestov Thanks a lot for the explanation. So I got this exactly wrong.

@ole
Copy link
Contributor Author

ole commented Apr 1, 2018

I rewrote the issue to (hopefully) make it correct.

@ole
Copy link
Contributor Author

ole commented Mar 27, 2020

Just to give an update, this issue still exists in Swift 5.2.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@ole
Copy link
Contributor Author

ole commented Apr 26, 2022

Verified as fixed in Swift 5.6 (Xcode 13.3.1). I'm not sure in which Swift version it was fixed.

@ole ole closed this as completed Apr 26, 2022
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