Skip to content

Commit 26c70a5

Browse files
authored
Merge pull request #16672 from nkcsgexi/cherry-migrator-05-16-04-30
[4.2-04-30] Cherry-pick migrator changes
2 parents 4946f41 + 6bc889e commit 26c70a5

File tree

7 files changed

+486
-92
lines changed

7 files changed

+486
-92
lines changed

include/swift/IDE/DigesterEnums.def

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ NODE_ANNOTATION(NowThrowing)
6363
NODE_ANNOTATION(NowMutating)
6464
NODE_ANNOTATION(StaticChange)
6565
NODE_ANNOTATION(OwnershipChange)
66+
NODE_ANNOTATION(RawTypeLeft)
67+
NODE_ANNOTATION(RawTypeRight)
6668

6769
NODE_ANNOTATION_CHANGE_KIND(ImplicitOptionalToOptional)
6870
NODE_ANNOTATION_CHANGE_KIND(OptionalToImplicitOptional)
@@ -71,17 +73,26 @@ NODE_ANNOTATION_CHANGE_KIND(WrapImplicitOptional)
7173
NODE_ANNOTATION_CHANGE_KIND(UnwrapOptional)
7274
NODE_ANNOTATION_CHANGE_KIND(GetterToProperty)
7375
NODE_ANNOTATION_CHANGE_KIND(SetterToProperty)
74-
NODE_ANNOTATION_CHANGE_KIND(TypeRewritten)
75-
NODE_ANNOTATION_CHANGE_KIND(ModernizeEnum)
76-
NODE_ANNOTATION_CHANGE_KIND(UnwrapUnmanaged)
77-
NODE_ANNOTATION_CHANGE_KIND(Rename)
7876
NODE_ANNOTATION_CHANGE_KIND(DictionaryKeyUpdate)
7977
NODE_ANNOTATION_CHANGE_KIND(OptionalDictionaryKeyUpdate)
8078
NODE_ANNOTATION_CHANGE_KIND(ArrayMemberUpdate)
8179
NODE_ANNOTATION_CHANGE_KIND(OptionalArrayMemberUpdate)
8280
NODE_ANNOTATION_CHANGE_KIND(SimpleStringRepresentableUpdate)
8381
NODE_ANNOTATION_CHANGE_KIND(SimpleOptionalStringRepresentableUpdate)
8482

83+
NODE_ANNOTATION_CHANGE_KIND(RevertDictionaryKeyUpdate)
84+
NODE_ANNOTATION_CHANGE_KIND(RevertOptionalDictionaryKeyUpdate)
85+
NODE_ANNOTATION_CHANGE_KIND(RevertArrayMemberUpdate)
86+
NODE_ANNOTATION_CHANGE_KIND(RevertOptionalArrayMemberUpdate)
87+
NODE_ANNOTATION_CHANGE_KIND(RevertSimpleStringRepresentableUpdate)
88+
NODE_ANNOTATION_CHANGE_KIND(RevertSimpleOptionalStringRepresentableUpdate)
89+
90+
NODE_ANNOTATION_CHANGE_KIND(ModernizeEnum)
91+
NODE_ANNOTATION_CHANGE_KIND(UnwrapUnmanaged)
92+
NODE_ANNOTATION_CHANGE_KIND(Rename)
93+
94+
// Keep type rewritten the last one.
95+
NODE_ANNOTATION_CHANGE_KIND(TypeRewritten)
8596

8697
DECL_ATTR(deprecated)
8798
DECL_ATTR(fixedLayout)
@@ -115,6 +126,7 @@ KNOWN_TYPE(Function)
115126
KNOWN_TYPE(Dictionary)
116127
KNOWN_TYPE(String)
117128
KNOWN_TYPE(Array)
129+
KNOWN_TYPE(Int)
118130

119131
KNOWN_PROTOCOL(RawRepresentable)
120132

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -722,13 +722,15 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
722722
return false;
723723
std::string Rename;
724724
Optional<NodeAnnotation> Kind;
725+
StringRef LeftComment;
725726
StringRef RightComment;
726727
for (auto *Item: getRelatedDiffItems(RD)) {
727728
if (isSimpleReplacement(Item, Rename)) {
728729
} else if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
729730
if (CI->isStringRepresentableChange() &&
730731
CI->NodeKind == SDKNodeKind::DeclVar) {
731732
Kind = CI->DiffKind;
733+
LeftComment = CI->LeftComment;
732734
RightComment = CI->RightComment;
733735
}
734736
}
@@ -737,7 +739,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
737739
return false;
738740
if (Kind && !isNilExpr(WrapperTarget)) {
739741
SmallString<256> Buffer;
740-
auto Func = insertHelperFunction(*Kind, RightComment, Buffer, FromString);
742+
auto Func = insertHelperFunction(*Kind, LeftComment, RightComment, Buffer,
743+
FromString);
741744
Editor.insert(WrapperTarget->getStartLoc(), (Twine(Func) + "(").str());
742745
Editor.insertAfterToken(WrapperTarget->getEndLoc(), ")");
743746
}
@@ -768,7 +771,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
768771
return wrapAttributeReference(E, E, false);
769772
}
770773

771-
StringRef insertHelperFunction(NodeAnnotation Anno, StringRef NewType,
774+
StringRef insertHelperFunction(NodeAnnotation Anno, StringRef RawType,
775+
StringRef NewType,
772776
SmallString<256> &Buffer, bool FromString) {
773777
llvm::raw_svector_ostream OS(Buffer);
774778
OS << "\n";
@@ -780,13 +784,13 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
780784
StringRef guard = "\tguard let input = input else { return nil }\n";
781785
switch(Anno) {
782786
case NodeAnnotation::OptionalArrayMemberUpdate:
783-
Segs = {"Optional", "Array", "[String]?"};
787+
Segs = {"Optional", "Array", (Twine("[") + RawType + "]?").str()};
784788
Segs.push_back((Twine("[") + NewType +"]?").str());
785789
Segs.push_back((Twine(guard) + "\treturn input.map { key in " + NewType +"(key) }").str());
786790
Segs.push_back((Twine(guard) + "\treturn input.map { key in key.rawValue }").str());
787791
break;
788792
case NodeAnnotation::OptionalDictionaryKeyUpdate:
789-
Segs = {"Optional", "Dictionary", "[String: Any]?"};
793+
Segs = {"Optional", "Dictionary", (Twine("[") + RawType + ": Any]?").str()};
790794
Segs.push_back((Twine("[") + NewType +": Any]?").str());
791795
Segs.push_back((Twine(guard) +
792796
"\treturn Dictionary(uniqueKeysWithValues: input.map"
@@ -796,27 +800,27 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
796800
" {key, value in (key.rawValue, value)})").str());
797801
break;
798802
case NodeAnnotation::ArrayMemberUpdate:
799-
Segs = {"", "Array", "[String]"};
803+
Segs = {"", "Array", (Twine("[") + RawType + "]").str()};
800804
Segs.push_back((Twine("[") + NewType +"]").str());
801805
Segs.push_back((Twine("\treturn input.map { key in ") + NewType +"(key) }").str());
802806
Segs.push_back("\treturn input.map { key in key.rawValue }");
803807
break;
804808
case NodeAnnotation::DictionaryKeyUpdate:
805-
Segs = {"", "Dictionary", "[String: Any]"};
809+
Segs = {"", "Dictionary", (Twine("[") + RawType + ": Any]").str()};
806810
Segs.push_back((Twine("[") + NewType +": Any]").str());
807811
Segs.push_back((Twine("\treturn Dictionary(uniqueKeysWithValues: input.map"
808812
" { key, value in (") + NewType + "(rawValue: key), value)})").str());
809813
Segs.push_back("\treturn Dictionary(uniqueKeysWithValues: input.map"
810814
" {key, value in (key.rawValue, value)})");
811815
break;
812816
case NodeAnnotation::SimpleStringRepresentableUpdate:
813-
Segs = {"", "", "String"};
817+
Segs = {"", "", RawType};
814818
Segs.push_back(NewType);
815819
Segs.push_back((Twine("\treturn ") + NewType + "(rawValue: input)").str());
816820
Segs.push_back("\treturn input.rawValue");
817821
break;
818822
case NodeAnnotation::SimpleOptionalStringRepresentableUpdate:
819-
Segs = {"Optional", "", "String?"};
823+
Segs = {"Optional", "", (Twine(RawType) +"?").str()};
820824
Segs.push_back((Twine(NewType) +"?").str());
821825
Segs.push_back((Twine(guard) + "\treturn " + NewType + "(rawValue: input)").str());
822826
Segs.push_back((Twine(guard) + "\treturn input.rawValue").str());
@@ -850,12 +854,14 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
850854
Editor.disableCache();
851855
SWIFT_DEFER { Editor.enableCache(); };
852856
NodeAnnotation Kind;
857+
StringRef RawType;
853858
StringRef NewAttributeType;
854859
uint8_t ArgIdx;
855860
for (auto Item: getRelatedDiffItems(FD)) {
856861
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
857862
if (CI->isStringRepresentableChange()) {
858863
Kind = CI->DiffKind;
864+
RawType = CI->LeftComment;
859865
NewAttributeType = CI->RightComment;
860866
assert(CI->getChildIndices().size() == 1);
861867
ArgIdx = CI->getChildIndices().front();
@@ -880,7 +886,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
880886
}
881887
assert(WrapTarget);
882888
SmallString<256> Buffer;
883-
auto FuncName = insertHelperFunction(Kind, NewAttributeType, Buffer,
889+
auto FuncName = insertHelperFunction(Kind, RawType, NewAttributeType, Buffer,
884890
FromString);
885891
Editor.insert(WrapTarget->getStartLoc(), (Twine(FuncName) + "(").str());
886892
Editor.insertAfterToken(WrapTarget->getEndLoc(), ")");

test/Migrator/Inputs/string-representable.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"NodeAnnotation": "SimpleStringRepresentableUpdate",
66
"ChildIndex": "0",
77
"LeftUsr": "s:6Cities9ContainerC5ValueSSvp",
8-
"LeftComment": "",
8+
"LeftComment": "String",
99
"RightUsr": "",
1010
"RightComment": "NewAttribute",
1111
"ModuleName": "Cities"
@@ -16,7 +16,7 @@
1616
"NodeAnnotation": "DictionaryKeyUpdate",
1717
"ChildIndex": "1",
1818
"LeftUsr": "s:6Cities9ContainerC16addingAttributesyys10DictionaryVySSypGF",
19-
"LeftComment": "",
19+
"LeftComment": "String",
2020
"RightUsr": "",
2121
"RightComment": "Cities.Container.Attribute",
2222
"ModuleName": "Cities"
@@ -27,7 +27,7 @@
2727
"NodeAnnotation": "DictionaryKeyUpdate",
2828
"ChildIndex": "1",
2929
"LeftUsr": "s:6Cities9ContainerC6adding10attributesys10DictionaryVySSypG_tF",
30-
"LeftComment": "",
30+
"LeftComment": "String",
3131
"RightUsr": "",
3232
"RightComment": "SimpleAttribute",
3333
"ModuleName": "Cities"
@@ -38,7 +38,7 @@
3838
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
3939
"ChildIndex": "1",
4040
"LeftUsr": "s:6Cities9ContainerC6adding18optionalAttributesys10DictionaryVySSypGSg_tF",
41-
"LeftComment": "",
41+
"LeftComment": "String",
4242
"RightUsr": "",
4343
"RightComment": "SimpleAttribute",
4444
"ModuleName": "Cities"
@@ -49,7 +49,7 @@
4949
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
5050
"ChildIndex": "1",
5151
"LeftUsr": "s:6Cities9ContainerC18optionalAttributesACs10DictionaryVySSypGSg_tcfc",
52-
"LeftComment": "",
52+
"LeftComment": "String",
5353
"RightUsr": "",
5454
"RightComment": "SimpleAttribute",
5555
"ModuleName": "Cities"
@@ -60,7 +60,7 @@
6060
"NodeAnnotation": "ArrayMemberUpdate",
6161
"ChildIndex": "1",
6262
"LeftUsr": "s:6Cities9ContainerC6adding9attrArrayySaySSG_tF",
63-
"LeftComment": "",
63+
"LeftComment": "String",
6464
"RightUsr": "",
6565
"RightComment": "SimpleAttribute",
6666
"ModuleName": "Cities"
@@ -71,7 +71,7 @@
7171
"NodeAnnotation": "OptionalArrayMemberUpdate",
7272
"ChildIndex": "1",
7373
"LeftUsr": "s:6Cities9ContainerC17optionalAttrArrayACSaySSGSg_tcfc",
74-
"LeftComment": "",
74+
"LeftComment": "String",
7575
"RightUsr": "",
7676
"RightComment": "SimpleAttribute",
7777
"ModuleName": "Cities"
@@ -82,7 +82,7 @@
8282
"NodeAnnotation": "SimpleStringRepresentableUpdate",
8383
"ChildIndex": "1",
8484
"LeftUsr": "s:6Cities9ContainerC3add6singleySS_tF",
85-
"LeftComment": "",
85+
"LeftComment": "String",
8686
"RightUsr": "",
8787
"RightComment": "SimpleAttribute",
8888
"ModuleName": "Cities"
@@ -93,7 +93,7 @@
9393
"NodeAnnotation": "SimpleOptionalStringRepresentableUpdate",
9494
"ChildIndex": "1",
9595
"LeftUsr": "s:6Cities9ContainerC3add14singleOptionalySSSg_tF",
96-
"LeftComment": "",
96+
"LeftComment": "String",
9797
"RightUsr": "",
9898
"RightComment": "SimpleAttribute",
9999
"ModuleName": "Cities"
@@ -104,7 +104,7 @@
104104
"NodeAnnotation": "SimpleStringRepresentableUpdate",
105105
"ChildIndex": "0",
106106
"LeftUsr": "s:6Cities9ContainerC13getSingleAttrSSyF",
107-
"LeftComment": "",
107+
"LeftComment": "String",
108108
"RightUsr": "",
109109
"RightComment": "SimpleAttribute",
110110
"ModuleName": "Cities"
@@ -115,7 +115,7 @@
115115
"NodeAnnotation": "SimpleOptionalStringRepresentableUpdate",
116116
"ChildIndex": "0",
117117
"LeftUsr": "s:6Cities9ContainerC21getOptionalSingleAttrSSSgyF",
118-
"LeftComment": "",
118+
"LeftComment": "String",
119119
"RightUsr": "",
120120
"RightComment": "SimpleAttribute",
121121
"ModuleName": "Cities"
@@ -126,7 +126,7 @@
126126
"NodeAnnotation": "ArrayMemberUpdate",
127127
"ChildIndex": "0",
128128
"LeftUsr": "s:6Cities9ContainerC12getAttrArraySaySSGyF",
129-
"LeftComment": "",
129+
"LeftComment": "String",
130130
"RightUsr": "",
131131
"RightComment": "SimpleAttribute",
132132
"ModuleName": "Cities"
@@ -137,7 +137,7 @@
137137
"NodeAnnotation": "OptionalArrayMemberUpdate",
138138
"ChildIndex": "0",
139139
"LeftUsr": "s:6Cities9ContainerC20getOptionalAttrArraySaySSGSgyF",
140-
"LeftComment": "",
140+
"LeftComment": "String",
141141
"RightUsr": "",
142142
"RightComment": "SimpleAttribute",
143143
"ModuleName": "Cities"
@@ -148,7 +148,7 @@
148148
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
149149
"ChildIndex": "0",
150150
"LeftUsr": "s:6Cities9ContainerC25getOptionalAttrDictionarys0F0VySSypGSgyF",
151-
"LeftComment": "",
151+
"LeftComment": "String",
152152
"RightUsr": "",
153153
"RightComment": "SimpleAttribute",
154154
"ModuleName": "Cities"
@@ -159,7 +159,7 @@
159159
"NodeAnnotation": "DictionaryKeyUpdate",
160160
"ChildIndex": "0",
161161
"LeftUsr": "s:6Cities9ContainerC17getAttrDictionarys0E0VySSypGyF",
162-
"LeftComment": "",
162+
"LeftComment": "String",
163163
"RightUsr": "",
164164
"RightComment": "SimpleAttribute",
165165
"ModuleName": "Cities"
@@ -170,7 +170,7 @@
170170
"NodeAnnotation": "DictionaryKeyUpdate",
171171
"ChildIndex": "0",
172172
"LeftUsr": "s:6Cities9ContainerC8attrDicts10DictionaryVySSypGvp",
173-
"LeftComment": "",
173+
"LeftComment": "String",
174174
"RightUsr": "",
175175
"RightComment": "SimpleAttribute",
176176
"ModuleName": "Cities"
@@ -181,7 +181,7 @@
181181
"NodeAnnotation": "ArrayMemberUpdate",
182182
"ChildIndex": "0",
183183
"LeftUsr": "s:6Cities9ContainerC7attrArrSaySSGvp",
184-
"LeftComment": "",
184+
"LeftComment": "String",
185185
"RightUsr": "",
186186
"RightComment": "SimpleAttribute",
187187
"ModuleName": "Cities"
@@ -192,7 +192,7 @@
192192
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
193193
"ChildIndex": "0",
194194
"LeftUsr": "s:6Cities9ContainerC16optionalAttrDicts10DictionaryVySSypGSgvp",
195-
"LeftComment": "",
195+
"LeftComment": "String",
196196
"RightUsr": "",
197197
"RightComment": "SimpleAttribute",
198198
"ModuleName": "Cities"
@@ -203,7 +203,7 @@
203203
"NodeAnnotation": "SimpleStringRepresentableUpdate",
204204
"ChildIndex": "0",
205205
"LeftUsr": "s:6Cities15GlobalAttributeSSvp",
206-
"LeftComment": "",
206+
"LeftComment": "String",
207207
"RightUsr": "",
208208
"RightComment": "NewAttribute",
209209
"ModuleName": "Cities"

0 commit comments

Comments
 (0)