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-11176] Covariant 'Self' type cannot be referenced from a stored property initializer #53573

Open
DevAndArtist mannequin opened this issue Jul 22, 2019 · 6 comments
Open
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@DevAndArtist
Copy link
Mannequin

DevAndArtist mannequin commented Jul 22, 2019

Previous ID SR-11176
Radar rdar://problem/66171047
Original Reporter @DevAndArtist
Type Bug
Environment

Apple Swift version 5.1 (swiftlang-1100.0.257.2 clang-1100.0.31.3)
Target: x86_64-apple-darwin19.0.0

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

md5: 7afacc92e505425e8d597bd8233c41f2

is duplicated by:

  • SR-12023 Generics with Self on class type failing compiler
  • SR-11414 Final class type and Self should be treated as equivalent in class body
  • SR-13915 Support covariant self for final classes or final methods

relates to:

  • SR-15448 Cannot use Self as function parameter type in actors

Issue Description:

Shouldn't this be possible on a final class?

final class S {
  // error - Covariant 'Self' type cannot be referenced from a stored property initializer
  var keyPath: KeyPath<Self, Bool> = \Self.bool
  var bool = true
}
@belkadan
Copy link
Contributor

cc @slavapestov

@slavapestov
Copy link
Member

Yes, I think it's reasonable to expect that in a final class, Self should be a synonym for the class type, just like in structs and enums.

To make this work 100% we also want an implicit conversion from the class type to the dynamic Self type, since the latter will still appear if you, eg, override a Self-returning method from the base class.

@iby
Copy link

iby commented Oct 8, 2019

@slavapestov Shouldn't it behave the same with non-final classes too, e.g., be a synonym for the class type? `Self` is a painkiller for all of these annoyances, but stored properties are completely missing out on them. It sounds kind of self-imposing that using `Self` with a stored property initialization can't refer to anything else but the current class – since no inheriting classes are initialized yet.

@belkadan
Copy link
Contributor

belkadan commented Oct 8, 2019

In a non-final class, you need a way to spell the dynamic type, and Self has already claimed that.

@typesanitizer
Copy link

@swift-ci create

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

is it going to be fixed?

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

5 participants