Skip to content

Commit a8f4675

Browse files
authored
Merge pull request #17715 from nkcsgexi/cherry-pick-41740103
[4.2] migrator: support migration of raw representable initializer calls without explicit labels.
2 parents 9c51a94 + 5a03c32 commit a8f4675

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
10281028
}
10291029

10301030
// Change attribute(rawValue: "value") to "value"
1031+
// Change attribute("value") to "value"
10311032
if (auto *CE = dyn_cast<CallExpr>(E)) {
10321033
auto Found = false;
10331034
if (auto *CRC = dyn_cast<ConstructorRefCallExpr>(CE->getFn())) {
@@ -1040,10 +1041,14 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
10401041
return false;
10411042
std::vector<CallArgInfo> AllArgs =
10421043
getCallArgInfo(SM, CE->getArg(), LabelRangeEndAt::LabelNameOnly);
1043-
if (AllArgs.size() == 1 && AllArgs.front().LabelRange.str() == "rawValue") {
1044-
Editor.replace(CE->getSourceRange(), Lexer::getCharSourceRangeFromSourceRange(SM,
1045-
AllArgs.front().ArgExp->getSourceRange()).str());
1046-
return true;
1044+
if (AllArgs.size() == 1) {
1045+
auto Label = AllArgs.front().LabelRange.str();
1046+
if (Label == "rawValue" || Label.empty()) {
1047+
Editor.replace(CE->getSourceRange(),
1048+
Lexer::getCharSourceRangeFromSourceRange(SM,
1049+
AllArgs.front().ArgExp->getSourceRange()).str());
1050+
return true;
1051+
}
10471052
}
10481053
}
10491054
return false;

test/Migrator/Inputs/Cities.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public enum FontWeight: Int {
7777
}
7878

7979
public struct AwesomeCityAttribute: RawRepresentable {
80-
public init?(rawValue: String) { self.rawValue = rawValue }
80+
public init(rawValue: String) { self.rawValue = rawValue }
81+
public init(_ rawValue: String) { self.rawValue = rawValue }
8182
public var rawValue: String
8283
public typealias RawValue = String
8384
}
@@ -86,6 +87,7 @@ public class Wrapper {
8687
public struct Attribute: RawRepresentable {
8788
public static let KnownAttr = Wrapper.Attribute(rawValue: "")
8889
public init(rawValue: String) { self.rawValue = rawValue }
90+
public init(_ rawValue: String) { self.rawValue = rawValue }
8991
public var rawValue: String
9092
public typealias RawValue = String
9193
}

test/Migrator/string-representable.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ class C: BarForwardDeclaredClass {}
4949
func revert(_ a: AwesomeCityAttribute, b: Wrapper.Attribute) {
5050
_ = AwesomeCityAttribute(rawValue: "somevalue")
5151
_ = AwesomeCityAttribute.init(rawValue: "somevalue")
52+
_ = AwesomeCityAttribute("somevalue")
53+
_ = AwesomeCityAttribute.init("somevalue")
5254
_ = a.rawValue
5355
_ = Wrapper.Attribute(rawValue: "somevalue")
5456
_ = Wrapper.Attribute.init(rawValue: "somevalue")
5557
_ = b.rawValue
5658
_ = Wrapper.Attribute.KnownAttr.rawValue
59+
_ = Wrapper.Attribute("somevalue")
60+
_ = Wrapper.Attribute.init("somevalue")
5761
}
5862

5963

test/Migrator/string-representable.swift.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ func foo(_ c: Container) -> String {
4747
class C: BarForwardDeclaredClass {}
4848

4949
func revert(_ a: AwesomeCityAttribute, b: Wrapper.Attribute) {
50+
_ = "somevalue"
51+
_ = "somevalue"
5052
_ = "somevalue"
5153
_ = "somevalue"
5254
_ = a
5355
_ = "somevalue"
5456
_ = "somevalue"
5557
_ = b
5658
_ = NewAttributeWrapper.NewKnownAttr
59+
_ = "somevalue"
60+
_ = "somevalue"
5761
}
5862

5963

0 commit comments

Comments
 (0)