-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Introduce special decl names #9989
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
Introduce special decl names #9989
Conversation
@swift-ci please test |
8105cb6
to
3d1f716
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.
Looking pretty good—all the comments are small. I think you could have split this out a little more (a big chunk of the Serialization and ClangImporter changes don't depend on special names existing yet), but it's all right.
We do still have one issue for down the road, which is that there's no syntax for #selector(getter:NSDictionary.subscript)
, which would refer to a property named "subscript". Maybe we'll have to go with something like #selector(getter:NSDictionary[])
lib/AST/ASTMangler.cpp
Outdated
appendIdentifier(decl->getBaseName().getIdentifier().str()); | ||
// FIXME: Should a mangled subscript name contain the string "subscript"? | ||
switch (decl->getBaseName().getKind()) { | ||
case DeclBaseName::Kind::Normal: |
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.
Nitpick: Swift/LLVM style doesn't indent case labels.
lib/ClangImporter/SwiftLookupTable.h
Outdated
@@ -45,6 +45,102 @@ class TypedefNameDecl; | |||
|
|||
namespace swift { | |||
|
|||
/// A name from a SwiftLookupTable that has not been deserialized into a | |||
/// DeclBaseName yet |
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.
Nitpick: missing period.
lib/ClangImporter/SwiftLookupTable.h
Outdated
} | ||
|
||
/// Return a string representation of the name that can be used for sorting | ||
StringRef sortStr() const { |
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.
Nitpick: I think this should be private
for now. This partially comes from me still not being a fan of using the "str" abbreviation with any kind of description.
(also, missing period again)
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.
This was meant to be in sync with DeclBaseName
, so I'll change the name to userFacingName
. Shall we still make it private or keep it public then?
@@ -400,8 +400,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo { | |||
} | |||
|
|||
SmallVector<char, 64> Buf; | |||
// TODO: Handle special names | |||
StringRef Name = (VD->getBaseName().getIdentifier().str() + | |||
StringRef Name = (VD->getBaseName().userFacingName() + | |||
Twine(Kind)).toStringRef(Buf); |
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.
@adrian-prantl, this is the no-functionality-change implementation, and I think it does make sense to land that first, but it's worth considering whether this makes sense to begin with. Subscripts can be overloaded.
switch (P.Tok.getKind()) { | ||
case tok::kw_subscript: | ||
P.consumeToken(); | ||
FullName.push_back(DeclBaseName::createSubscript()); |
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.
I don't know if it answers the question but maybe I'll just comment on what's going on here:
Previously, subscripts were backed by the identifier "subscript" so no distinction between properties named "subscript" and proper subscripts was necessary. Now we need distinguish between the two. As per #6848 (comment) I decided to represent a property named "subscript" as "`subscript`" in a SIL dotted path while plain "subscript" (without the backticks) still refers to a proper subscript.
So what I'm doing here is checking if a component in a SIL dotted path is the keyword subscript in order to translate it to a special DeclBaseName for subscripts. If "subscript" is spelled in backticks, the token kind will be "backtick", the default branch is taken and parseSILIdentifier
will parse "`subscript`"
as an identifier named "subscript", producing a dotted path that refers to a property named "subscript".
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.
@jckarter says this solution sounds good.
@@ -85,16 +85,21 @@ namespace { | |||
using offset_type = unsigned; | |||
|
|||
hash_value_type ComputeHash(key_type_ref key) { | |||
assert(!key.empty()); |
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.
Please keep this assertion in the Normal branch; it's caught bugs before. :-)
lib/Serialization/Serialization.cpp
Outdated
IdentifiersToWrite.push_back(ident.getIdentifier()); | ||
return id; | ||
switch (ident.getKind()) { | ||
case DeclBaseName::Kind::Normal: { |
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.
Nitpick: indented switch again.
@@ -1138,6 +1138,7 @@ void SwiftLangSupport::getDocInfo(llvm::MemoryBuffer *InputBuf, | |||
if (Error) | |||
Consumer.failed("Error occurred"); | |||
return; | |||
|
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.
Spurious change?
@@ -1,4 +1,4 @@ | |||
// RUN: not --crash %target-swift-frontend %s -typecheck | |||
// RUN: not %target-swift-frontend %s -typecheck | |||
// REQUIRES: asserts |
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.
Nitpick: No more "REQUIRES: asserts" needed.
test/Sema/subscripting.swift
Outdated
@@ -0,0 +1,16 @@ | |||
// RUN: %target-run-simple-swift | %FileCheck %s |
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.
Nitpick: test/Sema isn't supposed to have execution tests. Please move this to test/Interpreter (which has a number of non-interpreter execution tests at the moment) and add "REQUIRES: executable_test".
6ff4737
to
710dd93
Compare
@swift-ci Please test |
Build failed |
Build failed |
710dd93
to
ded6aed
Compare
Resolved merge conflicts @swift-ci Please test |
Build failed |
Build failed |
@jrose-apple I've addressed all the comments in your last review and the PR is ready from my side. Could you review the changes if you've got time? |
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.
Just some small comments on this last version, but other than these this looks good to me! No need to wait for me after fixing this round.
include/swift/AST/Identifier.h
Outdated
@@ -206,15 +208,45 @@ namespace swift { | |||
|
|||
/// Wrapper that may either be an Identifier or a special name | |||
/// (e.g. for subscripts) | |||
/// | |||
/// The raw values of this enum are used in serialization by both |
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.
This comment should no longer be necessary, right? (Also, it belongs on the enum, not the class!)
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 still using them for serialisation in ClangImporter but you said that they don't need to be stable for that, correct?
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.
Yep, that's correct.
@@ -90,7 +90,6 @@ IDENTIFIER(simd) | |||
IDENTIFIER(some) | |||
IDENTIFIER(storage) | |||
IDENTIFIER(stringValue) | |||
IDENTIFIER(subscript) |
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.
🎉
@@ -540,8 +540,15 @@ void ASTMangler::appendDeclName(const ValueDecl *decl) { | |||
break; | |||
} | |||
} else if (decl->hasName()) { | |||
// TODO: Handle special names | |||
appendIdentifier(decl->getBaseName().getIdentifier().str()); | |||
// FIXME: Should a mangled subscript name contain the string "subscript"? |
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.
Something @eeckstein may have an opinion on, but can be changed in a follow-up commit.
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 wanted to start a thread about that once the changes have been landed
|
||
// CHECK-LABEL: sil_vtable SubscriptAsFunction { | ||
// CHECK-NOT: #SubscriptAsFunction.subscript | ||
// CHECK: #SubscriptAsFunction.`subscript`!1 |
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 should be a SIL parser test for this too, even if just a basic one.
lib/Serialization/Serialization.cpp
Outdated
out << key.getIdentifier().str(); | ||
endian::Writer<little> writer(out); | ||
switch (key.getKind()) { | ||
case DeclBaseName::Kind::Normal: |
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.
Nitpick: LLVM style does not indent switch cases.
Special DeclNames represent names that do not have an identifier in the surface language. This implies serializing the information about whether a name is special together with its identifier (if it is not special) in both the module file and the swift lookup table.
ded6aed
to
2dd670b
Compare
@swift-ci Please test |
Build failed |
Build failed |
…iptExprs. Interactions with the rest of the type checker cause use of functions named `subscript` with backtick-quotes to become ambiguous. This is more fully and robustly solved in master by swiftlang#9989, but that's too invasive a change for 4.0, and we need a spot fix to prevent source compatibility regressions such as SR-5513.
After a long way we're finally here: The PR to introduce special names for subscripts and fix SR-2575.
Special DeclNames represent names that do not have an identifier in the surface language. This implies serializing the information about whether a name is special together with its identifier (if it is not special) in both the module file and the swift lookup table.