-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Introduce a special DeclBaseName for "deinit" #10965
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
@swift-ci Please test |
@@ -420,7 +420,7 @@ bool Decl::isPrivateStdlibDecl(bool whitelistProtocols) const { | |||
if (auto AFD = dyn_cast<AbstractFunctionDecl>(D)) { | |||
// Hide '~>' functions (but show the operator, because it defines | |||
// precedence). | |||
if (AFD->getNameStr() == "~>") |
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.
The only reference to the ~>
operator I could find was in Policy.swift which stated that it is a workaround for rdar://problem/14011860. Is that still up to date? GenericDispatch.swift seems to test it but it's used nowhere in the standard library.
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, yes, that's gone now that we have protocol extensions. Still, removing it entirely should probably be a separate PR.
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.
OK, I'll have a look how much of it can be removed later on.
@@ -239,7 +239,8 @@ static void printValueDecl(ValueDecl *Decl, raw_ostream &OS) { | |||
|
|||
if (Decl->isOperator()) { | |||
OS << '"' << Decl->getBaseName() << '"'; | |||
} else if (Decl->getBaseName() == "subscript") { | |||
} else if (Decl->getBaseName() == "subscript" || | |||
Decl->getBaseName() == "deinit") { |
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.
I'm not quite sure if we should escape deinit
in SIL if used as a normal identifier. For subscripts we needed to, since otherwise the names of subscript getters/setters could have clashed with the getters/setters of instance variables named "subscript". AFAIK destructors in SIL always have the suffix !destructor
so there's no name clash. Should we strive for consistency between the two keywords or simplicity on deinit
? See the changes in vtables.swift for an example of how the dotted paths look like.
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.
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.
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.
Ah, I forgot @swiftix was out on vacation. He'll be back tomorrow.
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.
I agree with Jordan. Escaping all keywords seems like the right conservative answer.
14f1d5f
to
c13bbbe
Compare
@swift-ci Please test |
Build failed |
Build failed |
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.
Wow, this fell out very nicely. Good work, Alex.
lib/AST/ASTMangler.cpp
Outdated
@@ -548,6 +548,9 @@ void ASTMangler::appendDeclName(const ValueDecl *decl) { | |||
case DeclBaseName::Kind::Subscript: | |||
appendIdentifier("subscript"); | |||
break; | |||
case DeclBaseName::Kind::Destructor: | |||
// FIXME: Is it true that this is unreachable? |
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.
I'm 90% sure this is true but try defining a local type inside a destructor to make sure.
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.
OK, thanks for the reassurance.
@@ -5647,6 +5647,8 @@ class ConstructorDecl : public AbstractFunctionDecl { | |||
GenericParamList *GenericParams, | |||
DeclContext *Parent); | |||
|
|||
Identifier getName() const { return getFullName().getBaseIdentifier(); } |
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 we actually use this one? It's always going to be Id_init
.
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.
I left it in for consistency. You can call getName()
on all Decls that are guaranteed to be backed by Identifiers. But I don't think it's actually used. Long term goal would be to remove it once/if Id_init
becomes a special name as well.
c13bbbe
to
5002fce
Compare
The way the PR description is phrased this sounds like some kind of troll. Just sayin' . |
deinit
deinit
@dabrahams That's certainly not what I wanted. Thanks for the feedback. I've updated it and hope it sounds more serious now. |
418ff0d
to
d18f196
Compare
This name is not used yet
d18f196
to
8a839ed
Compare
@jrose-apple Could you have another look through this? I think all the questions have been answered and your comments taken care of. @swift-ci Please test |
Build failed |
Build failed |
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.
We left off with the question about escaping in SIL, and Joe agreed with me that it would be better to escape everything. But that doesn't have to be part of this patch, which is just about deinit
. Yes, this looks good now!
No longer use the known identifier `deinit` for destructors. This allows classes to have functions called `deinit`.
8a839ed
to
ebd701c
Compare
Ah, I see, I thought by all keywords you meant escaping all keywords that clashed with special decl names. I'll create another PR for it then. @swift-ci Please test and merge |
Test once again before merging since old test is already several days old. @swift-ci Please smoke test and merge |
swift-ci didn't run. Let's try again @swift-ci Please smoke test and merge |
Problem statement
Currently, destructors are represented by the known identifier "deinit", making it impossible to have a function or instance variable named "deinit" in a class. Declaring a function named "deinit" currently results in an error about an invalid redeclaration of
deinit
and adding an instance variable named "deinit" crashes the compiler.Solution
Similar to how special
DeclName
s were introduced for subscripts to avoid the name collision between functions named "subscript" and proper subscripts, introduce a specialDeclName
for destructors and remove "deinit" from the list of known identifiers, making the identifier named "deinit" and the name representing the destructor completely distinct and thus resolving the above issue.