Skip to content

[4.2] migrator: support migration of raw representable initializer calls without explicit labels. #17715

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
Jul 4, 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
13 changes: 9 additions & 4 deletions lib/Migrator/APIDiffMigratorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
}

// Change attribute(rawValue: "value") to "value"
// Change attribute("value") to "value"
if (auto *CE = dyn_cast<CallExpr>(E)) {
auto Found = false;
if (auto *CRC = dyn_cast<ConstructorRefCallExpr>(CE->getFn())) {
Expand All @@ -1040,10 +1041,14 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
return false;
std::vector<CallArgInfo> AllArgs =
getCallArgInfo(SM, CE->getArg(), LabelRangeEndAt::LabelNameOnly);
if (AllArgs.size() == 1 && AllArgs.front().LabelRange.str() == "rawValue") {
Editor.replace(CE->getSourceRange(), Lexer::getCharSourceRangeFromSourceRange(SM,
AllArgs.front().ArgExp->getSourceRange()).str());
return true;
if (AllArgs.size() == 1) {
auto Label = AllArgs.front().LabelRange.str();
if (Label == "rawValue" || Label.empty()) {
Editor.replace(CE->getSourceRange(),
Lexer::getCharSourceRangeFromSourceRange(SM,
AllArgs.front().ArgExp->getSourceRange()).str());
return true;
}
}
}
return false;
Expand Down
4 changes: 3 additions & 1 deletion test/Migrator/Inputs/Cities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public enum FontWeight: Int {
}

public struct AwesomeCityAttribute: RawRepresentable {
public init?(rawValue: String) { self.rawValue = rawValue }
public init(rawValue: String) { self.rawValue = rawValue }
public init(_ rawValue: String) { self.rawValue = rawValue }
public var rawValue: String
public typealias RawValue = String
}
Expand All @@ -86,6 +87,7 @@ public class Wrapper {
public struct Attribute: RawRepresentable {
public static let KnownAttr = Wrapper.Attribute(rawValue: "")
public init(rawValue: String) { self.rawValue = rawValue }
public init(_ rawValue: String) { self.rawValue = rawValue }
public var rawValue: String
public typealias RawValue = String
}
Expand Down
4 changes: 4 additions & 0 deletions test/Migrator/string-representable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ class C: BarForwardDeclaredClass {}
func revert(_ a: AwesomeCityAttribute, b: Wrapper.Attribute) {
_ = AwesomeCityAttribute(rawValue: "somevalue")
_ = AwesomeCityAttribute.init(rawValue: "somevalue")
_ = AwesomeCityAttribute("somevalue")
_ = AwesomeCityAttribute.init("somevalue")
_ = a.rawValue
_ = Wrapper.Attribute(rawValue: "somevalue")
_ = Wrapper.Attribute.init(rawValue: "somevalue")
_ = b.rawValue
_ = Wrapper.Attribute.KnownAttr.rawValue
_ = Wrapper.Attribute("somevalue")
_ = Wrapper.Attribute.init("somevalue")
}


Expand Down
4 changes: 4 additions & 0 deletions test/Migrator/string-representable.swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ func foo(_ c: Container) -> String {
class C: BarForwardDeclaredClass {}

func revert(_ a: AwesomeCityAttribute, b: Wrapper.Attribute) {
_ = "somevalue"
_ = "somevalue"
_ = "somevalue"
_ = "somevalue"
_ = a
_ = "somevalue"
_ = "somevalue"
_ = b
_ = NewAttributeWrapper.NewKnownAttr
_ = "somevalue"
_ = "somevalue"
}


Expand Down