Skip to content

Commit 9a0196e

Browse files
committed
migrator: avoid adding conversion functions when expression types change as well.
When inserting conversion functions around expressions, we should check whether the type of the expression is a recognized name alias that is known to be changed to raw-value representable type. We should avoid inserting conversion functions in this case because doing so causes build errors. resolves: rdar://40463990
1 parent 04999aa commit 9a0196e

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,24 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
363363
std::vector<ConversionFunctionInfo> HelperFuncInfo;
364364
SourceLoc FileEndLoc;
365365

366+
/// For a given expression, check whether the type of this expression is
367+
/// name alias type, and the name alias type is known to change to raw
368+
/// representable type.
369+
bool isRecognizedTypeAliasChange(Expr *E) {
370+
if (auto Ty = E->getType()) {
371+
if (auto *NT = dyn_cast<NameAliasType>(Ty.getPointer())) {
372+
for (auto Item: getRelatedDiffItems(NT->getDecl())) {
373+
if (auto CI = dyn_cast<CommonDiffItem>(Item)) {
374+
if (CI->DiffKind == NodeAnnotation::TypeAliasDeclToRawRepresentable) {
375+
return true;
376+
}
377+
}
378+
}
379+
}
380+
}
381+
return false;
382+
}
383+
366384
APIDiffMigratorPass(EditorAdapter &Editor, SourceFile *SF,
367385
const MigratorOptions &Opts):
368386
ASTMigratorPass(Editor, SF, Opts), DiffStore(Diags),
@@ -395,6 +413,11 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
395413
if (count > 1)
396414
continue;
397415
assert(count == 1);
416+
417+
// A conversion function will be redundant if the expression will change
418+
// from type alias to raw-value representable.
419+
if (isRecognizedTypeAliasChange(Cur.ExpressionToWrap))
420+
continue;
398421
auto FuncName = Cur.getFuncName();
399422

400423
// Avoid inserting the helper function if it's already present.

test/Migrator/Inputs/Cities.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,6 @@ public class Wrapper {
8787
public var rawValue: String
8888
public typealias RawValue = String
8989
}
90-
}
90+
}
91+
92+
public typealias AliasAttribute = String

test/Migrator/Inputs/string-representable.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,15 @@
249249
"RightComment": "String",
250250
"ModuleName": "Cities"
251251
},
252+
{
253+
"DiffItemKind": "CommonDiffItem",
254+
"NodeKind": "TypeAlias",
255+
"NodeAnnotation": "TypeAliasDeclToRawRepresentable",
256+
"ChildIndex": "0",
257+
"LeftUsr": "s:6Cities14AliasAttributea",
258+
"LeftComment": "String",
259+
"RightUsr": "",
260+
"RightComment": "String",
261+
"ModuleName": "Cities"
262+
},
252263
]

test/Migrator/string-representable.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ func revert(_ a: AwesomeCityAttribute, b: Wrapper.Attribute) {
5454
_ = Wrapper.Attribute.init(rawValue: "somevalue")
5555
_ = b.rawValue
5656
}
57+
58+
59+
func bar(_ c: Container) {
60+
let attr: AliasAttribute = ""
61+
c.add(single: attr)
62+
}

test/Migrator/string-representable.swift.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ func revert(_ a: AwesomeCityAttribute, b: Wrapper.Attribute) {
5555
_ = b
5656
}
5757

58+
59+
func bar(_ c: Container) {
60+
let attr: AliasAttribute = ""
61+
c.add(single: attr)
62+
}
63+
5864
// Helper function inserted by Swift 4.2 migrator.
5965
fileprivate func convertToNewAttribute(_ input: String) -> NewAttribute {
6066
return NewAttribute(rawValue: input)

0 commit comments

Comments
 (0)