Skip to content

Commit 9e99423

Browse files
authored
Merge pull request #15817 from nkcsgexi/👦
2 parents 478d846 + 3e00fda commit 9e99423

File tree

7 files changed

+163
-12
lines changed

7 files changed

+163
-12
lines changed

‎include/swift/IDE/DigesterEnums.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ NODE_ANNOTATION(StaticChange)
7676
NODE_ANNOTATION(OwnershipChange)
7777
NODE_ANNOTATION(DictionaryKeyUpdate)
7878
NODE_ANNOTATION(OptionalDictionaryKeyUpdate)
79+
NODE_ANNOTATION(ArrayMemberUpdate)
80+
NODE_ANNOTATION(OptionalArrayMemberUpdate)
7981

8082
DECL_ATTR(deprecated)
8183
DECL_ATTR(fixedLayout)
@@ -108,6 +110,7 @@ KNOWN_TYPE(Unmanaged)
108110
KNOWN_TYPE(Function)
109111
KNOWN_TYPE(Dictionary)
110112
KNOWN_TYPE(String)
113+
KNOWN_TYPE(Array)
111114

112115
KNOWN_PROTOCOL(RawRepresentable)
113116

‎test/api-digester/Inputs/APINotesLeft/APINotesTest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ extern int ANTGlobalValue;
2121
@interface AnimalStatusDescriptor
2222
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributes:(nonnull NSDictionary<NSString*, id> *)attributes;
2323
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributes:(nullable NSDictionary<NSString*, id> *)attributes;
24+
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributesArray:(nonnull NSArray<NSString*> *)attributes;
25+
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributesArray:(nullable NSArray<NSString*> *)attributes;
2426
@end

‎test/api-digester/Inputs/APINotesRight/APINotesTest.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ typedef NSString * AnimalAttributeName NS_STRING_ENUM;
2121
@interface AnimalStatusDescriptor
2222
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributes:(nonnull NSDictionary<AnimalAttributeName, id> *)attributes;
2323
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributes:(nullable NSDictionary<AnimalAttributeName, id> *)attributes;
24+
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributesArray:(nonnull NSArray<AnimalAttributeName> *)attributes;
25+
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributesArray:(nullable NSArray<AnimalAttributeName> *)attributes;
26+
2427
@end

‎test/api-digester/Outputs/apinotes-diags-3-4.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ APINotesTest(APINotesTest.h): TypeAlias AnimalAttributeName(NSString) is now Str
1010

1111
/* Type Changes */
1212
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributes(_:) has parameter 0 type change from [String : Any] to [AnimalAttributeName : Any]
13+
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributesArray(_:) has parameter 0 type change from [String] to [AnimalAttributeName]
1314
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributes(_:) has parameter 0 type change from [String : Any]? to [AnimalAttributeName : Any]?
15+
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributesArray(_:) has parameter 0 type change from [String]? to [AnimalAttributeName]?
1416

1517
/* Decl Attribute changes */

‎test/api-digester/Outputs/apinotes-diags.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ APINotesTest(APINotesTest.h): Var OldType.oldMember has been renamed to Var NewT
1414

1515
/* Type Changes */
1616
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributes(_:) has parameter 0 type change from [String : Any] to [AnimalAttributeName : Any]
17+
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributesArray(_:) has parameter 0 type change from [String] to [AnimalAttributeName]
1718
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributes(_:) has parameter 0 type change from [String : Any]? to [AnimalAttributeName : Any]?
19+
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributesArray(_:) has parameter 0 type change from [String]? to [AnimalAttributeName]?
1820

1921
/* Decl Attribute changes */

‎test/api-digester/Outputs/apinotes-migrator-gen.json

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,94 @@
8787
"RightComment": "AnimalAttributeName",
8888
"ModuleName": "APINotesTest"
8989
},
90+
{
91+
"DiffItemKind": "CommonDiffItem",
92+
"NodeKind": "Function",
93+
"NodeAnnotation": "TypeRewritten",
94+
"ChildIndex": "1:0",
95+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributesArray:",
96+
"LeftComment": "String",
97+
"RightUsr": "",
98+
"RightComment": "AnimalAttributeName",
99+
"ModuleName": "APINotesTest"
100+
},
101+
{
102+
"DiffItemKind": "CommonDiffItem",
103+
"NodeKind": "Function",
104+
"NodeAnnotation": "ArrayMemberUpdate",
105+
"ChildIndex": "1",
106+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributesArray:",
107+
"LeftComment": "",
108+
"RightUsr": "",
109+
"RightComment": "AnimalAttributeName",
110+
"ModuleName": "APINotesTest"
111+
},
112+
{
113+
"DiffItemKind": "CommonDiffItem",
114+
"NodeKind": "Function",
115+
"NodeAnnotation": "TypeRewritten",
116+
"ChildIndex": "1:0",
117+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributesArray:",
118+
"LeftComment": "String",
119+
"RightUsr": "",
120+
"RightComment": "AnimalAttributeName",
121+
"ModuleName": "APINotesTest"
122+
},
123+
{
124+
"DiffItemKind": "CommonDiffItem",
125+
"NodeKind": "Function",
126+
"NodeAnnotation": "ArrayMemberUpdate",
127+
"ChildIndex": "1",
128+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributesArray:",
129+
"LeftComment": "",
130+
"RightUsr": "",
131+
"RightComment": "AnimalAttributeName",
132+
"ModuleName": "APINotesTest"
133+
},
134+
{
135+
"DiffItemKind": "CommonDiffItem",
136+
"NodeKind": "Function",
137+
"NodeAnnotation": "TypeRewritten",
138+
"ChildIndex": "1:0:0",
139+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributesArray:",
140+
"LeftComment": "String",
141+
"RightUsr": "",
142+
"RightComment": "AnimalAttributeName",
143+
"ModuleName": "APINotesTest"
144+
},
145+
{
146+
"DiffItemKind": "CommonDiffItem",
147+
"NodeKind": "Function",
148+
"NodeAnnotation": "OptionalArrayMemberUpdate",
149+
"ChildIndex": "1",
150+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributesArray:",
151+
"LeftComment": "",
152+
"RightUsr": "",
153+
"RightComment": "AnimalAttributeName",
154+
"ModuleName": "APINotesTest"
155+
},
156+
{
157+
"DiffItemKind": "CommonDiffItem",
158+
"NodeKind": "Function",
159+
"NodeAnnotation": "TypeRewritten",
160+
"ChildIndex": "1:0:0",
161+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributesArray:",
162+
"LeftComment": "String",
163+
"RightUsr": "",
164+
"RightComment": "AnimalAttributeName",
165+
"ModuleName": "APINotesTest"
166+
},
167+
{
168+
"DiffItemKind": "CommonDiffItem",
169+
"NodeKind": "Function",
170+
"NodeAnnotation": "OptionalArrayMemberUpdate",
171+
"ChildIndex": "1",
172+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributesArray:",
173+
"LeftComment": "",
174+
"RightUsr": "",
175+
"RightComment": "AnimalAttributeName",
176+
"ModuleName": "APINotesTest"
177+
},
90178
{
91179
"DiffItemKind": "CommonDiffItem",
92180
"NodeKind": "Function",

‎tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,18 +2693,9 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
26932693
return false;
26942694
}
26952695

2696-
static StringRef detectDictionaryKeyChangeInternal(SDKNodeType *L,
2697-
SDKNodeType *R) {
2698-
if (L->getTypeKind() != KnownTypeKind::Dictionary ||
2699-
R->getTypeKind() != KnownTypeKind::Dictionary)
2700-
return StringRef();
2701-
auto *Left = dyn_cast<SDKNodeTypeNominal>(L);
2702-
auto *Right = dyn_cast<SDKNodeTypeNominal>(R);
2703-
assert(Left && Right);
2704-
assert(Left->getChildrenCount() == 2);
2705-
assert(Right->getChildrenCount() == 2);
2706-
auto* LKey = dyn_cast<SDKNodeTypeNominal>(*Left->getChildBegin());
2707-
auto* RKey = dyn_cast<SDKNodeTypeNominal>(*Right->getChildBegin());
2696+
static StringRef getStringRepresentableChange(SDKNode *L, SDKNode *R) {
2697+
auto* LKey = dyn_cast<SDKNodeTypeNominal>(L);
2698+
auto* RKey = dyn_cast<SDKNodeTypeNominal>(R);
27082699
if (!LKey || !RKey)
27092700
return StringRef();
27102701
if (LKey->getTypeKind() != KnownTypeKind::String)
@@ -2720,6 +2711,20 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
27202711
return StringRef();
27212712
}
27222713

2714+
static StringRef detectDictionaryKeyChangeInternal(SDKNodeType *L,
2715+
SDKNodeType *R) {
2716+
if (L->getTypeKind() != KnownTypeKind::Dictionary ||
2717+
R->getTypeKind() != KnownTypeKind::Dictionary)
2718+
return StringRef();
2719+
auto *Left = dyn_cast<SDKNodeTypeNominal>(L);
2720+
auto *Right = dyn_cast<SDKNodeTypeNominal>(R);
2721+
assert(Left && Right);
2722+
assert(Left->getChildrenCount() == 2);
2723+
assert(Right->getChildrenCount() == 2);
2724+
return getStringRepresentableChange(*Left->getChildBegin(),
2725+
*Right->getChildBegin());
2726+
}
2727+
27232728
bool detectDictionaryKeyChange(SDKNodeType *L, SDKNodeType *R) {
27242729
if (!IsVisitingLeft)
27252730
return false;
@@ -2748,6 +2753,47 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
27482753
return false;
27492754
}
27502755

2756+
static StringRef detectArrayMemberChangeInternal(SDKNodeType *L,
2757+
SDKNodeType *R) {
2758+
if (L->getTypeKind() != KnownTypeKind::Array ||
2759+
R->getTypeKind() != KnownTypeKind::Array)
2760+
return StringRef();
2761+
auto *Left = dyn_cast<SDKNodeTypeNominal>(L);
2762+
auto *Right = dyn_cast<SDKNodeTypeNominal>(R);
2763+
assert(Left && Right);
2764+
assert(Left->getChildrenCount() == 1);
2765+
assert(Right->getChildrenCount() == 1);
2766+
return getStringRepresentableChange(Left->getOnlyChild(),
2767+
Right->getOnlyChild());
2768+
}
2769+
2770+
bool detectArrayMemberChange(SDKNodeType* L, SDKNodeType *R) {
2771+
if (!IsVisitingLeft)
2772+
return false;
2773+
// We only care if this the top-level type node.
2774+
if (!isa<SDKNodeDecl>(L->getParent()) || !isa<SDKNodeDecl>(R->getParent()))
2775+
return false;
2776+
StringRef KeyChangedTo;
2777+
if (L->getTypeKind() == KnownTypeKind::Optional &&
2778+
R->getTypeKind() == KnownTypeKind::Optional) {
2779+
// Detect [String]? to [StringRepresentableStruct]? Chnage
2780+
KeyChangedTo =
2781+
detectArrayMemberChangeInternal(L->getOnlyChild()->getAs<SDKNodeType>(),
2782+
R->getOnlyChild()->getAs<SDKNodeType>());
2783+
} else {
2784+
// Detect [String] to [StringRepresentableStruct] Chnage
2785+
KeyChangedTo = detectArrayMemberChangeInternal(L, R);
2786+
}
2787+
if (!KeyChangedTo.empty()) {
2788+
L->annotate(L->getTypeKind() == KnownTypeKind::Optional ?
2789+
NodeAnnotation::OptionalArrayMemberUpdate :
2790+
NodeAnnotation::ArrayMemberUpdate);
2791+
L->annotate(NodeAnnotation::TypeRewrittenRight, KeyChangedTo);
2792+
return true;
2793+
}
2794+
return false;
2795+
}
2796+
27512797
bool isUnhandledCase(SDKNodeType *Node) {
27522798
auto Counter = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeType>();
27532799
return Node->getTypeKind() == KnownTypeKind::Void ||
@@ -2779,6 +2825,7 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
27792825
detectWrapImplicitOptional(Node, Counter)||
27802826
detectUnmanagedUpdate(Node, Counter)||
27812827
detectDictionaryKeyChange(Node, Counter) ||
2828+
detectArrayMemberChange(Node, Counter) ||
27822829
detectTypeRewritten(Node, Counter);
27832830
(void) Result;
27842831
return;
@@ -2887,6 +2934,8 @@ class DiffItemEmitter : public SDKNodeVisitor {
28872934

28882935
static StringRef getRightComment(NodePtr Node, NodeAnnotation Anno) {
28892936
switch (Anno) {
2937+
case NodeAnnotation::ArrayMemberUpdate:
2938+
case NodeAnnotation::OptionalArrayMemberUpdate:
28902939
case NodeAnnotation::DictionaryKeyUpdate:
28912940
case NodeAnnotation::OptionalDictionaryKeyUpdate:
28922941
case NodeAnnotation::TypeRewritten:
@@ -2948,6 +2997,8 @@ class DiffItemEmitter : public SDKNodeVisitor {
29482997
NodeAnnotation::NowThrowing,
29492998
NodeAnnotation::DictionaryKeyUpdate,
29502999
NodeAnnotation::OptionalDictionaryKeyUpdate,
3000+
NodeAnnotation::ArrayMemberUpdate,
3001+
NodeAnnotation::OptionalArrayMemberUpdate,
29513002
});
29523003
}
29533004

0 commit comments

Comments
 (0)