Skip to content

[4.2-04-30] Cherry-pick migrator changes #16672

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
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
20 changes: 16 additions & 4 deletions include/swift/IDE/DigesterEnums.def
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ NODE_ANNOTATION(NowThrowing)
NODE_ANNOTATION(NowMutating)
NODE_ANNOTATION(StaticChange)
NODE_ANNOTATION(OwnershipChange)
NODE_ANNOTATION(RawTypeLeft)
NODE_ANNOTATION(RawTypeRight)

NODE_ANNOTATION_CHANGE_KIND(ImplicitOptionalToOptional)
NODE_ANNOTATION_CHANGE_KIND(OptionalToImplicitOptional)
Expand All @@ -71,17 +73,26 @@ NODE_ANNOTATION_CHANGE_KIND(WrapImplicitOptional)
NODE_ANNOTATION_CHANGE_KIND(UnwrapOptional)
NODE_ANNOTATION_CHANGE_KIND(GetterToProperty)
NODE_ANNOTATION_CHANGE_KIND(SetterToProperty)
NODE_ANNOTATION_CHANGE_KIND(TypeRewritten)
NODE_ANNOTATION_CHANGE_KIND(ModernizeEnum)
NODE_ANNOTATION_CHANGE_KIND(UnwrapUnmanaged)
NODE_ANNOTATION_CHANGE_KIND(Rename)
NODE_ANNOTATION_CHANGE_KIND(DictionaryKeyUpdate)
NODE_ANNOTATION_CHANGE_KIND(OptionalDictionaryKeyUpdate)
NODE_ANNOTATION_CHANGE_KIND(ArrayMemberUpdate)
NODE_ANNOTATION_CHANGE_KIND(OptionalArrayMemberUpdate)
NODE_ANNOTATION_CHANGE_KIND(SimpleStringRepresentableUpdate)
NODE_ANNOTATION_CHANGE_KIND(SimpleOptionalStringRepresentableUpdate)

NODE_ANNOTATION_CHANGE_KIND(RevertDictionaryKeyUpdate)
NODE_ANNOTATION_CHANGE_KIND(RevertOptionalDictionaryKeyUpdate)
NODE_ANNOTATION_CHANGE_KIND(RevertArrayMemberUpdate)
NODE_ANNOTATION_CHANGE_KIND(RevertOptionalArrayMemberUpdate)
NODE_ANNOTATION_CHANGE_KIND(RevertSimpleStringRepresentableUpdate)
NODE_ANNOTATION_CHANGE_KIND(RevertSimpleOptionalStringRepresentableUpdate)

NODE_ANNOTATION_CHANGE_KIND(ModernizeEnum)
NODE_ANNOTATION_CHANGE_KIND(UnwrapUnmanaged)
NODE_ANNOTATION_CHANGE_KIND(Rename)

// Keep type rewritten the last one.
NODE_ANNOTATION_CHANGE_KIND(TypeRewritten)

DECL_ATTR(deprecated)
DECL_ATTR(fixedLayout)
Expand Down Expand Up @@ -115,6 +126,7 @@ KNOWN_TYPE(Function)
KNOWN_TYPE(Dictionary)
KNOWN_TYPE(String)
KNOWN_TYPE(Array)
KNOWN_TYPE(Int)

KNOWN_PROTOCOL(RawRepresentable)

Expand Down
24 changes: 15 additions & 9 deletions lib/Migrator/APIDiffMigratorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,13 +722,15 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
return false;
std::string Rename;
Optional<NodeAnnotation> Kind;
StringRef LeftComment;
StringRef RightComment;
for (auto *Item: getRelatedDiffItems(RD)) {
if (isSimpleReplacement(Item, Rename)) {
} else if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
if (CI->isStringRepresentableChange() &&
CI->NodeKind == SDKNodeKind::DeclVar) {
Kind = CI->DiffKind;
LeftComment = CI->LeftComment;
RightComment = CI->RightComment;
}
}
Expand All @@ -737,7 +739,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
return false;
if (Kind && !isNilExpr(WrapperTarget)) {
SmallString<256> Buffer;
auto Func = insertHelperFunction(*Kind, RightComment, Buffer, FromString);
auto Func = insertHelperFunction(*Kind, LeftComment, RightComment, Buffer,
FromString);
Editor.insert(WrapperTarget->getStartLoc(), (Twine(Func) + "(").str());
Editor.insertAfterToken(WrapperTarget->getEndLoc(), ")");
}
Expand Down Expand Up @@ -768,7 +771,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
return wrapAttributeReference(E, E, false);
}

StringRef insertHelperFunction(NodeAnnotation Anno, StringRef NewType,
StringRef insertHelperFunction(NodeAnnotation Anno, StringRef RawType,
StringRef NewType,
SmallString<256> &Buffer, bool FromString) {
llvm::raw_svector_ostream OS(Buffer);
OS << "\n";
Expand All @@ -780,13 +784,13 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
StringRef guard = "\tguard let input = input else { return nil }\n";
switch(Anno) {
case NodeAnnotation::OptionalArrayMemberUpdate:
Segs = {"Optional", "Array", "[String]?"};
Segs = {"Optional", "Array", (Twine("[") + RawType + "]?").str()};
Segs.push_back((Twine("[") + NewType +"]?").str());
Segs.push_back((Twine(guard) + "\treturn input.map { key in " + NewType +"(key) }").str());
Segs.push_back((Twine(guard) + "\treturn input.map { key in key.rawValue }").str());
break;
case NodeAnnotation::OptionalDictionaryKeyUpdate:
Segs = {"Optional", "Dictionary", "[String: Any]?"};
Segs = {"Optional", "Dictionary", (Twine("[") + RawType + ": Any]?").str()};
Segs.push_back((Twine("[") + NewType +": Any]?").str());
Segs.push_back((Twine(guard) +
"\treturn Dictionary(uniqueKeysWithValues: input.map"
Expand All @@ -796,27 +800,27 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
" {key, value in (key.rawValue, value)})").str());
break;
case NodeAnnotation::ArrayMemberUpdate:
Segs = {"", "Array", "[String]"};
Segs = {"", "Array", (Twine("[") + RawType + "]").str()};
Segs.push_back((Twine("[") + NewType +"]").str());
Segs.push_back((Twine("\treturn input.map { key in ") + NewType +"(key) }").str());
Segs.push_back("\treturn input.map { key in key.rawValue }");
break;
case NodeAnnotation::DictionaryKeyUpdate:
Segs = {"", "Dictionary", "[String: Any]"};
Segs = {"", "Dictionary", (Twine("[") + RawType + ": Any]").str()};
Segs.push_back((Twine("[") + NewType +": Any]").str());
Segs.push_back((Twine("\treturn Dictionary(uniqueKeysWithValues: input.map"
" { key, value in (") + NewType + "(rawValue: key), value)})").str());
Segs.push_back("\treturn Dictionary(uniqueKeysWithValues: input.map"
" {key, value in (key.rawValue, value)})");
break;
case NodeAnnotation::SimpleStringRepresentableUpdate:
Segs = {"", "", "String"};
Segs = {"", "", RawType};
Segs.push_back(NewType);
Segs.push_back((Twine("\treturn ") + NewType + "(rawValue: input)").str());
Segs.push_back("\treturn input.rawValue");
break;
case NodeAnnotation::SimpleOptionalStringRepresentableUpdate:
Segs = {"Optional", "", "String?"};
Segs = {"Optional", "", (Twine(RawType) +"?").str()};
Segs.push_back((Twine(NewType) +"?").str());
Segs.push_back((Twine(guard) + "\treturn " + NewType + "(rawValue: input)").str());
Segs.push_back((Twine(guard) + "\treturn input.rawValue").str());
Expand Down Expand Up @@ -850,12 +854,14 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
Editor.disableCache();
SWIFT_DEFER { Editor.enableCache(); };
NodeAnnotation Kind;
StringRef RawType;
StringRef NewAttributeType;
uint8_t ArgIdx;
for (auto Item: getRelatedDiffItems(FD)) {
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
if (CI->isStringRepresentableChange()) {
Kind = CI->DiffKind;
RawType = CI->LeftComment;
NewAttributeType = CI->RightComment;
assert(CI->getChildIndices().size() == 1);
ArgIdx = CI->getChildIndices().front();
Expand All @@ -880,7 +886,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
}
assert(WrapTarget);
SmallString<256> Buffer;
auto FuncName = insertHelperFunction(Kind, NewAttributeType, Buffer,
auto FuncName = insertHelperFunction(Kind, RawType, NewAttributeType, Buffer,
FromString);
Editor.insert(WrapTarget->getStartLoc(), (Twine(FuncName) + "(").str());
Editor.insertAfterToken(WrapTarget->getEndLoc(), ")");
Expand Down
38 changes: 19 additions & 19 deletions test/Migrator/Inputs/string-representable.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"NodeAnnotation": "SimpleStringRepresentableUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities9ContainerC5ValueSSvp",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "NewAttribute",
"ModuleName": "Cities"
Expand All @@ -16,7 +16,7 @@
"NodeAnnotation": "DictionaryKeyUpdate",
"ChildIndex": "1",
"LeftUsr": "s:6Cities9ContainerC16addingAttributesyys10DictionaryVySSypGF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "Cities.Container.Attribute",
"ModuleName": "Cities"
Expand All @@ -27,7 +27,7 @@
"NodeAnnotation": "DictionaryKeyUpdate",
"ChildIndex": "1",
"LeftUsr": "s:6Cities9ContainerC6adding10attributesys10DictionaryVySSypG_tF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -38,7 +38,7 @@
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
"ChildIndex": "1",
"LeftUsr": "s:6Cities9ContainerC6adding18optionalAttributesys10DictionaryVySSypGSg_tF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -49,7 +49,7 @@
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
"ChildIndex": "1",
"LeftUsr": "s:6Cities9ContainerC18optionalAttributesACs10DictionaryVySSypGSg_tcfc",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -60,7 +60,7 @@
"NodeAnnotation": "ArrayMemberUpdate",
"ChildIndex": "1",
"LeftUsr": "s:6Cities9ContainerC6adding9attrArrayySaySSG_tF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -71,7 +71,7 @@
"NodeAnnotation": "OptionalArrayMemberUpdate",
"ChildIndex": "1",
"LeftUsr": "s:6Cities9ContainerC17optionalAttrArrayACSaySSGSg_tcfc",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -82,7 +82,7 @@
"NodeAnnotation": "SimpleStringRepresentableUpdate",
"ChildIndex": "1",
"LeftUsr": "s:6Cities9ContainerC3add6singleySS_tF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -93,7 +93,7 @@
"NodeAnnotation": "SimpleOptionalStringRepresentableUpdate",
"ChildIndex": "1",
"LeftUsr": "s:6Cities9ContainerC3add14singleOptionalySSSg_tF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -104,7 +104,7 @@
"NodeAnnotation": "SimpleStringRepresentableUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities9ContainerC13getSingleAttrSSyF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -115,7 +115,7 @@
"NodeAnnotation": "SimpleOptionalStringRepresentableUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities9ContainerC21getOptionalSingleAttrSSSgyF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -126,7 +126,7 @@
"NodeAnnotation": "ArrayMemberUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities9ContainerC12getAttrArraySaySSGyF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -137,7 +137,7 @@
"NodeAnnotation": "OptionalArrayMemberUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities9ContainerC20getOptionalAttrArraySaySSGSgyF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -148,7 +148,7 @@
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities9ContainerC25getOptionalAttrDictionarys0F0VySSypGSgyF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -159,7 +159,7 @@
"NodeAnnotation": "DictionaryKeyUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities9ContainerC17getAttrDictionarys0E0VySSypGyF",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -170,7 +170,7 @@
"NodeAnnotation": "DictionaryKeyUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities9ContainerC8attrDicts10DictionaryVySSypGvp",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -181,7 +181,7 @@
"NodeAnnotation": "ArrayMemberUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities9ContainerC7attrArrSaySSGvp",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -192,7 +192,7 @@
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities9ContainerC16optionalAttrDicts10DictionaryVySSypGSgvp",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "SimpleAttribute",
"ModuleName": "Cities"
Expand All @@ -203,7 +203,7 @@
"NodeAnnotation": "SimpleStringRepresentableUpdate",
"ChildIndex": "0",
"LeftUsr": "s:6Cities15GlobalAttributeSSvp",
"LeftComment": "",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "NewAttribute",
"ModuleName": "Cities"
Expand Down
Loading