Skip to content

swift-api-digester: add specific logic to detect optional dictionary's key changes. #15807

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

Merged
merged 1 commit into from
Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/IDE/DigesterEnums.def
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ NODE_ANNOTATION(NowMutating)
NODE_ANNOTATION(StaticChange)
NODE_ANNOTATION(OwnershipChange)
NODE_ANNOTATION(DictionaryKeyUpdate)
NODE_ANNOTATION(OptionalDictionaryKeyUpdate)

DECL_ATTR(deprecated)
DECL_ATTR(fixedLayout)
Expand Down
1 change: 1 addition & 0 deletions test/api-digester/Inputs/APINotesLeft/APINotesTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ extern int ANTGlobalValue;

@interface AnimalStatusDescriptor
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributes:(nonnull NSDictionary<NSString*, id> *)attributes;
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributes:(nullable NSDictionary<NSString*, id> *)attributes;
@end
1 change: 1 addition & 0 deletions test/api-digester/Inputs/APINotesRight/APINotesTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ typedef NSString * AnimalAttributeName NS_STRING_ENUM;

@interface AnimalStatusDescriptor
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributes:(nonnull NSDictionary<AnimalAttributeName, id> *)attributes;
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributes:(nullable NSDictionary<AnimalAttributeName, id> *)attributes;
@end
1 change: 1 addition & 0 deletions test/api-digester/Outputs/apinotes-diags-3-4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ APINotesTest(APINotesTest.h): TypeAlias AnimalAttributeName(NSString) is now Str

/* Type Changes */
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributes(_:) has parameter 0 type change from [String : Any] to [AnimalAttributeName : Any]
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributes(_:) has parameter 0 type change from [String : Any]? to [AnimalAttributeName : Any]?

/* Decl Attribute changes */
1 change: 1 addition & 0 deletions test/api-digester/Outputs/apinotes-diags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ APINotesTest(APINotesTest.h): Var OldType.oldMember has been renamed to Var NewT

/* Type Changes */
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributes(_:) has parameter 0 type change from [String : Any] to [AnimalAttributeName : Any]
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributes(_:) has parameter 0 type change from [String : Any]? to [AnimalAttributeName : Any]?

/* Decl Attribute changes */
44 changes: 44 additions & 0 deletions test/api-digester/Outputs/apinotes-migrator-gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,50 @@
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "TypeRewritten",
"ChildIndex": "1:0:0",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributes:",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
"ChildIndex": "1",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributes:",
"LeftComment": "",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "TypeRewritten",
"ChildIndex": "1:0:0",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributes:",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
"ChildIndex": "1",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributes:",
"LeftComment": "",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
Expand Down
52 changes: 39 additions & 13 deletions tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ class SDKNode {
ArrayRef<SDKNode*> getChildren() const;
bool hasSameChildren(const SDKNode &Other) const;
unsigned getChildIndex(NodePtr Child) const;
const SDKNode* getOnlyChild() const;
SDKNode* getOnlyChild() const;
SDKContext &getSDKContext() const { return Ctx; }
SDKNodeRoot *getRootNode() const;
template <typename T> const T *getAs() const;
Expand Down Expand Up @@ -575,7 +575,7 @@ unsigned SDKNode::getChildIndex(NodePtr Child) const {
return std::find(Children.begin(), Children.end(), Child) - Children.begin();
}

const SDKNode* SDKNode::getOnlyChild() const {
SDKNode* SDKNode::getOnlyChild() const {
assert(Children.size() == 1 && "more that one child.");
return *Children.begin();
}
Expand Down Expand Up @@ -2693,12 +2693,11 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
return false;
}

bool detectDictionaryKeyChange(SDKNodeType *L, SDKNodeType *R) {
if (!IsVisitingLeft)
return false;
static StringRef detectDictionaryKeyChangeInternal(SDKNodeType *L,
SDKNodeType *R) {
if (L->getTypeKind() != KnownTypeKind::Dictionary ||
R->getTypeKind() != KnownTypeKind::Dictionary)
return false;
return StringRef();
auto *Left = dyn_cast<SDKNodeTypeNominal>(L);
auto *Right = dyn_cast<SDKNodeTypeNominal>(R);
assert(Left && Right);
Expand All @@ -2707,20 +2706,45 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
auto* LKey = dyn_cast<SDKNodeTypeNominal>(*Left->getChildBegin());
auto* RKey = dyn_cast<SDKNodeTypeNominal>(*Right->getChildBegin());
if (!LKey || !RKey)
return false;
return StringRef();
if (LKey->getTypeKind() != KnownTypeKind::String)
return false;
return StringRef();
auto Results = RKey->getRootNode()->getDescendantsByUsr(RKey->getUsr());
if (Results.empty())
return false;
return StringRef();
if (auto DT = dyn_cast<SDKNodeDeclType>(Results.front())) {
if (DT->isConformingTo(KnownProtocolKind::RawRepresentable)) {
L->annotate(NodeAnnotation::DictionaryKeyUpdate);
L->annotate(NodeAnnotation::TypeRewrittenRight,
DT->getFullyQualifiedName());
return true;
return DT->getFullyQualifiedName();
}
}
return StringRef();
}

bool detectDictionaryKeyChange(SDKNodeType *L, SDKNodeType *R) {
if (!IsVisitingLeft)
return false;

// We only care if this the top-level type node.
if (!isa<SDKNodeDecl>(L->getParent()) || !isa<SDKNodeDecl>(R->getParent()))
return false;
StringRef KeyChangedTo;
if (L->getTypeKind() == KnownTypeKind::Optional &&
R->getTypeKind() == KnownTypeKind::Optional) {
// Detect [String: Any]? to [StringRepresentableStruct: Any]? Chnage
KeyChangedTo =
detectDictionaryKeyChangeInternal(L->getOnlyChild()->getAs<SDKNodeType>(),
R->getOnlyChild()->getAs<SDKNodeType>());
} else {
// Detect [String: Any] to [StringRepresentableStruct: Any] Chnage
KeyChangedTo = detectDictionaryKeyChangeInternal(L, R);
}
if (!KeyChangedTo.empty()) {
L->annotate(L->getTypeKind() == KnownTypeKind::Optional ?
NodeAnnotation::OptionalDictionaryKeyUpdate :
NodeAnnotation::DictionaryKeyUpdate);
L->annotate(NodeAnnotation::TypeRewrittenRight, KeyChangedTo);
return true;
}
return false;
}

Expand Down Expand Up @@ -2864,6 +2888,7 @@ class DiffItemEmitter : public SDKNodeVisitor {
static StringRef getRightComment(NodePtr Node, NodeAnnotation Anno) {
switch (Anno) {
case NodeAnnotation::DictionaryKeyUpdate:
case NodeAnnotation::OptionalDictionaryKeyUpdate:
case NodeAnnotation::TypeRewritten:
return Node->getAnnotateComment(NodeAnnotation::TypeRewrittenRight);
case NodeAnnotation::ModernizeEnum:
Expand Down Expand Up @@ -2922,6 +2947,7 @@ class DiffItemEmitter : public SDKNodeVisitor {
NodeAnnotation::Rename,
NodeAnnotation::NowThrowing,
NodeAnnotation::DictionaryKeyUpdate,
NodeAnnotation::OptionalDictionaryKeyUpdate,
});
}

Expand Down