Skip to content

Commit 2f742ac

Browse files
authored
Merge pull request #22629 from nkcsgexi/migrator-pro-5.0-cherry
[5.0] migrator: add ! to property access whose definition changes from nonnull to nullable.
2 parents a34907f + 3e753f7 commit 2f742ac

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,25 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
11021102
}
11031103
}
11041104

1105+
// If a property has changed from nonnull to nullable, we should add ! to the
1106+
// reference of the property.
1107+
bool handlePropertyTypeChange(Expr *E) {
1108+
if (auto MRE = dyn_cast<MemberRefExpr>(E)) {
1109+
if (auto *VD = MRE->getReferencedDecl().getDecl()) {
1110+
for (auto *I: getRelatedDiffItems(VD)) {
1111+
if (auto *Item = dyn_cast<CommonDiffItem>(I)) {
1112+
if (Item->DiffKind == NodeAnnotation::WrapOptional &&
1113+
Item->NodeKind == SDKNodeKind::DeclVar) {
1114+
Editor.insertAfterToken(E->getEndLoc(), "!");
1115+
return true;
1116+
}
1117+
}
1118+
}
1119+
}
1120+
}
1121+
return false;
1122+
}
1123+
11051124
bool walkToExprPre(Expr *E) override {
11061125
if (E->getSourceRange().isInvalid())
11071126
return false;
@@ -1115,6 +1134,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
11151134
return false;
11161135
if (handleAttributeReference(E))
11171136
return false;
1137+
if (handlePropertyTypeChange(E))
1138+
return false;
11181139
if (auto *CE = dyn_cast<CallExpr>(E)) {
11191140
auto Fn = CE->getFn();
11201141
auto Args = CE->getArg();

test/Migrator/Inputs/CallExpr.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,16 @@
2020
"RightUsr": "",
2121
"RightComment": "",
2222
"ModuleName": "Cities"
23+
},
24+
{
25+
"DiffItemKind": "CommonDiffItem",
26+
"NodeKind": "Var",
27+
"NodeAnnotation": "WrapOptional",
28+
"ChildIndex": "0",
29+
"LeftUsr": "s:6CitiesAAC4nameSSvp",
30+
"LeftComment": "",
31+
"RightUsr": "",
32+
"RightComment": "",
33+
"ModuleName": "Cities"
2334
}
2435
]

test/Migrator/Inputs/Cities.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
open class Cities {
22
var x: Int
3+
public var name: String = ""
34
public init(x: Int) { self.x = x }
45
public init!(y: Int) { self.x = y }
56
open func mooloolaba(x: Cities, y: Cities?) {}

test/Migrator/call_expr_result.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ func foo() {
1010
let c1 = Cities(x: 3)
1111
_ = Cities.init(x: 3)
1212
_ = c1.noosa()
13-
}
13+
_ = c1.name
14+
bar(c1.name)
15+
}
16+
17+
func bar(_ n: String) {}

test/Migrator/call_expr_result.swift.expected

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ func foo() {
1010
let c1 = Cities(x: 3)!
1111
_ = Cities.init(x: 3)!
1212
_ = c1.noosa()!
13-
}
13+
_ = c1.name!
14+
bar(c1.name!)
15+
}
16+
17+
func bar(_ n: String) {}

0 commit comments

Comments
 (0)