Skip to content

Commit c2894ed

Browse files
authored
Merge pull request #9850 from nkcsgexi/perperty-migration-4.0
[4.0][migrator] Handle getter function to property change in function overrides.
2 parents 78eecc4 + c3a8c13 commit c2894ed

File tree

4 files changed

+101
-41
lines changed

4 files changed

+101
-41
lines changed

include/swift/IDE/APIDigesterData.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ struct CommonDiffItem: public APIDiffItem {
118118
return false;
119119
}
120120
}
121+
122+
bool isToPropertyChange() const {
123+
switch (DiffKind) {
124+
case NodeAnnotation::GetterToProperty:
125+
case NodeAnnotation::SetterToProperty:
126+
return true;
127+
default:
128+
return false;
129+
}
130+
}
131+
121132
StringRef getNewName() const { assert(isRename()); return RightComment; }
122133
APIDiffItemKind getKind() const override {
123134
return APIDiffItemKind::ADK_CommonDiffItem;

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 82 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -605,54 +605,95 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
605605
return Replacement.contains('&') || Replacement.contains("->");
606606
}
607607

608+
void handleOverridingTypeChange(AbstractFunctionDecl *AFD,
609+
CommonDiffItem *DiffItem) {
610+
assert(AFD);
611+
assert(DiffItem->isTypeChange());
612+
ChildIndexFinder Finder(DiffItem->getChildIndices());
613+
auto Result = Finder.findChild(AFD);
614+
if (!Result.isValid())
615+
return;
616+
617+
switch (DiffItem->DiffKind) {
618+
case ide::api::NodeAnnotation::WrapOptional:
619+
if (Result.Suffixable) {
620+
Editor.insertAfterToken(Result.TokenRange.End, "?");
621+
} else {
622+
Editor.insertWrap("(", Result.TokenRange, ")?");
623+
}
624+
break;
625+
case ide::api::NodeAnnotation::WrapImplicitOptional:
626+
if (Result.Suffixable) {
627+
Editor.insertAfterToken(Result.TokenRange.End, "!");
628+
} else {
629+
Editor.insertWrap("(", Result.TokenRange, (")!"));
630+
}
631+
break;
632+
case ide::api::NodeAnnotation::UnwrapOptional:
633+
if (Result.Optional)
634+
Editor.remove(Result.TokenRange.End);
635+
break;
636+
case ide::api::NodeAnnotation::ImplicitOptionalToOptional:
637+
if (Result.Optional)
638+
Editor.replace(Result.TokenRange.End, "?");
639+
break;
640+
case ide::api::NodeAnnotation::TypeRewritten:
641+
Editor.replace(Result.TokenRange, DiffItem->RightComment);
642+
if (Result.Suffixed && typeReplacementMayNeedParens(DiffItem->RightComment)) {
643+
Editor.insertBefore(Result.TokenRange.Start, "(");
644+
Editor.insertAfterToken(Result.TokenRange.End, ")");
645+
}
646+
break;
647+
default:
648+
break;
649+
}
650+
}
651+
652+
void handleOverridingPropertyChange(AbstractFunctionDecl *AFD,
653+
CommonDiffItem *DiffItem) {
654+
assert(AFD);
655+
assert(DiffItem->isToPropertyChange());
656+
auto FD = dyn_cast<FuncDecl>(AFD);
657+
if (!FD)
658+
return;
659+
660+
switch (DiffItem->DiffKind) {
661+
case NodeAnnotation::GetterToProperty: {
662+
auto FuncLoc = FD->getFuncLoc();
663+
auto ReturnTyLoc = FD->getBodyResultTypeLoc().getLoc();
664+
auto NameLoc = FD->getNameLoc();
665+
if (FuncLoc.isInvalid() || ReturnTyLoc.isInvalid() || NameLoc.isInvalid())
666+
break;
667+
668+
// Replace "func" with "var"
669+
Editor.replaceToken(FuncLoc, "var");
670+
671+
// Replace "() -> " with ": "
672+
Editor.replace(CharSourceRange(SM, Lexer::getLocForEndOfToken(SM, NameLoc),
673+
ReturnTyLoc), ": ");
674+
675+
break;
676+
}
677+
case NodeAnnotation::SetterToProperty: {
678+
// FIXME: we should migrate this case too.
679+
break;
680+
}
681+
default:
682+
llvm_unreachable("should not be handled here.");
683+
}
684+
}
685+
608686
bool walkToDeclPre(Decl *D, CharSourceRange Range) override {
609687
if (D->isImplicit())
610688
return true;
611689
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
612690
handleFuncDeclRename(AFD, Range);
613691
for (auto *Item: getRelatedDiffItems(AFD)) {
614692
if (auto *DiffItem = dyn_cast<CommonDiffItem>(Item)) {
615-
if (!DiffItem->isTypeChange())
616-
continue;
617-
618-
ChildIndexFinder Finder(DiffItem->getChildIndices());
619-
auto Result = Finder.findChild(AFD);
620-
if (!Result.isValid())
621-
return false;
622-
623-
switch (DiffItem->DiffKind) {
624-
case ide::api::NodeAnnotation::WrapOptional:
625-
if (Result.Suffixable) {
626-
Editor.insertAfterToken(Result.TokenRange.End, "?");
627-
} else {
628-
Editor.insertWrap("(", Result.TokenRange, ")?");
629-
}
630-
break;
631-
case ide::api::NodeAnnotation::WrapImplicitOptional:
632-
if (Result.Suffixable) {
633-
Editor.insertAfterToken(Result.TokenRange.End, "!");
634-
} else {
635-
Editor.insertWrap("(", Result.TokenRange, (")!"));
636-
}
637-
break;
638-
case ide::api::NodeAnnotation::UnwrapOptional:
639-
if (Result.Optional)
640-
Editor.remove(Result.TokenRange.End);
641-
break;
642-
case ide::api::NodeAnnotation::ImplicitOptionalToOptional:
643-
if (Result.Optional)
644-
Editor.replace(Result.TokenRange.End, "?");
645-
break;
646-
case ide::api::NodeAnnotation::TypeRewritten:
647-
Editor.replace(Result.TokenRange, DiffItem->RightComment);
648-
if (Result.Suffixed && typeReplacementMayNeedParens(DiffItem->RightComment)) {
649-
Editor.insertBefore(Result.TokenRange.Start, "(");
650-
Editor.insertAfterToken(Result.TokenRange.End, ")");
651-
}
652-
break;
653-
default:
654-
break;
655-
}
693+
if (DiffItem->isTypeChange())
694+
handleOverridingTypeChange(AFD, DiffItem);
695+
else if (DiffItem->isToPropertyChange())
696+
handleOverridingPropertyChange(AFD, DiffItem);
656697
}
657698
}
658699
}

test/Migrator/property.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ func foo(_ a : PropertyUserInterface) {
88
a.setField(1)
99
_ = a.field()
1010
}
11+
12+
class C: PropertyUserInterface {
13+
public override func field() -> Int32 { return 1 }
14+
}

test/Migrator/property.swift.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ func foo(_ a : PropertyUserInterface) {
88
a.Field = 1
99
_ = a.field
1010
}
11+
12+
class C: PropertyUserInterface {
13+
public override var field: Int32 { return 1 }
14+
}

0 commit comments

Comments
 (0)