Skip to content

migrator: avoid adding conversion functions when expression types change as well. #17482

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
Jun 25, 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
23 changes: 23 additions & 0 deletions lib/Migrator/APIDiffMigratorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
std::vector<ConversionFunctionInfo> HelperFuncInfo;
SourceLoc FileEndLoc;

/// For a given expression, check whether the type of this expression is
/// name alias type, and the name alias type is known to change to raw
/// representable type.
bool isRecognizedTypeAliasChange(Expr *E) {
if (auto Ty = E->getType()) {
if (auto *NT = dyn_cast<NameAliasType>(Ty.getPointer())) {
for (auto Item: getRelatedDiffItems(NT->getDecl())) {
if (auto CI = dyn_cast<CommonDiffItem>(Item)) {
if (CI->DiffKind == NodeAnnotation::TypeAliasDeclToRawRepresentable) {
return true;
}
}
}
}
}
return false;
}

APIDiffMigratorPass(EditorAdapter &Editor, SourceFile *SF,
const MigratorOptions &Opts):
ASTMigratorPass(Editor, SF, Opts), DiffStore(Diags),
Expand Down Expand Up @@ -395,6 +413,11 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
if (count > 1)
continue;
assert(count == 1);

// A conversion function will be redundant if the expression will change
// from type alias to raw-value representable.
if (isRecognizedTypeAliasChange(Cur.ExpressionToWrap))
continue;
auto FuncName = Cur.getFuncName();

// Avoid inserting the helper function if it's already present.
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 @@ -87,4 +87,6 @@ public class Wrapper {
public var rawValue: String
public typealias RawValue = String
}
}
}

public typealias AliasAttribute = String
11 changes: 11 additions & 0 deletions test/Migrator/Inputs/string-representable.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,15 @@
"RightComment": "String",
"ModuleName": "Cities"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "TypeAlias",
"NodeAnnotation": "TypeAliasDeclToRawRepresentable",
"ChildIndex": "0",
"LeftUsr": "s:6Cities14AliasAttributea",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "String",
"ModuleName": "Cities"
},
]
6 changes: 6 additions & 0 deletions test/Migrator/string-representable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ func revert(_ a: AwesomeCityAttribute, b: Wrapper.Attribute) {
_ = Wrapper.Attribute.init(rawValue: "somevalue")
_ = b.rawValue
}


func bar(_ c: Container) {
let attr: AliasAttribute = ""
c.add(single: attr)
}
6 changes: 6 additions & 0 deletions test/Migrator/string-representable.swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func revert(_ a: AwesomeCityAttribute, b: Wrapper.Attribute) {
_ = b
}


func bar(_ c: Container) {
let attr: AliasAttribute = ""
c.add(single: attr)
}

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertToNewAttribute(_ input: String) -> NewAttribute {
return NewAttribute(rawValue: input)
Expand Down