-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Allow stored properties in extensions #61593
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
Conversation
| if (isStoredPropertyInitializer()) | ||
| decl = cast<NominalTypeDecl>(decl->getDeclContext()); | ||
| if (isStoredPropertyInitializer()) { | ||
| if (auto extension = dyn_cast<ExtensionDecl>(decl->getDeclContext())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
decl->getDeclContext()->getSelfNominalTypeDecl()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aha very helpful, thanks!
| if (isStoredPropertyInitializer() || (isPropertyWrapperBackingInitializer() && | ||
| d->getDeclContext()->isTypeContext())) { | ||
| auto *nominal = cast<NominalTypeDecl>(d->getDeclContext()); | ||
| NominalTypeDecl *nominal; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above
| // Stored properties in extensions within the same file are added | ||
| // as members to the defining type, so we shouldn't assert in that | ||
| // case | ||
| || DC->getContextKind() == DeclContextKind::ExtensionDecl); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DC->getSelfNominalTypeDecl() == TheStruct->getAnyNominal()
| // Stored properties in extensions within the same file are | ||
| // added as members to the defining type, so we shouldn't assert | ||
| // in that case | ||
| || DC->getContextKind() == DeclContextKind::ExtensionDecl, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
| NominalTypeDecl *nominal) { | ||
| auto subs = getSubstitutionsForPropertyInitializer(dc, nominal); | ||
|
|
||
| // open question: is there a good "ordered set" type we could use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
llvm::MapVector
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
|
|
||
| if (auto *structDecl = dyn_cast<StructDecl>(field->getDeclContext())) { | ||
| NominalTypeDecl *nominal; | ||
| if (auto extension = dyn_cast<ExtensionDecl>(field->getDeclContext())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
| auto *varPattern = pbd->getPattern(i); | ||
| // A type can also have stored properties that aren't direct members, | ||
| // e.g. if they were defined in an extension. | ||
| for (auto storedProperty : nominal->getStoredProperties()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you still need the first loop over the members?
| // Types can have stored properties that aren't included in | ||
| // `getABIMembers()`, so we have to handle those manually | ||
| for (VarDecl *VD : theType->getStoredProperties()) { | ||
| if (!visitedVarDecls.count(VD)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using a set here, you can just skip stored properties in the first loop
This proof-of-concept PR adds support for defining stored properties in extensions, as long as the extension is defined in the same file as the type being extended. For example:
Here's a pitch thread on the Evolution forums for this change: Allow stored properties in extensions