Skip to content

Commit 9e7f784

Browse files
committed
Fix #57354: Fix-it for deprecated initializers removes the .init part from self.init
1 parent 2f0ae44 commit 9e7f784

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,16 +2179,22 @@ static void fixItAvailableAttrRename(InFlightDiagnostic &diag,
21792179
} else if (parsed.BaseName == "init" && isa_and_nonnull<CallExpr>(call)) {
21802180
auto *CE = cast<CallExpr>(call);
21812181

2182-
// For initializers, replace with a "call" of the context type...but only
2183-
// if we know we're doing a call (rather than a first-class reference).
2182+
// If it is a call to an initializer (rather than a first-class reference):
2183+
21842184
if (parsed.isMember()) {
2185+
// replace with a "call" to the type (instead of writing `.init`)
21852186
diag.fixItReplace(CE->getFn()->getSourceRange(), parsed.ContextName);
21862187
} else if (auto *dotCall = dyn_cast<DotSyntaxCallExpr>(CE->getFn())) {
2187-
SourceLoc removeLoc = dotCall->getDotLoc();
2188-
if (removeLoc.isInvalid())
2189-
return;
2190-
2191-
diag.fixItRemove(SourceRange(removeLoc, dotCall->getFn()->getEndLoc()));
2188+
// if it's a dot call, and the left side is a type (and not, for example,
2189+
// `self`, just remove the dot and the right side, again in order to make
2190+
// it a "call" to the type
2191+
if (isa<TypeExpr>(dotCall->getBase())) {
2192+
SourceLoc removeLoc = dotCall->getDotLoc();
2193+
if (removeLoc.isInvalid())
2194+
return;
2195+
2196+
diag.fixItRemove(SourceRange(removeLoc, dotCall->getFn()->getEndLoc()));
2197+
}
21922198
} else if (!isa<ConstructorRefCallExpr>(CE->getFn())) {
21932199
return;
21942200
}

test/attr/attr_availability.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,29 @@ func testFactoryMethods() {
590590
Int.factory2(other: 1) // expected-error {{'factory2(other:)' has been replaced by 'Int.init(other:)'}} {{3-15=Int}}
591591
}
592592

593+
class DeprecatedInitBase {
594+
@available(*, deprecated, renamed: "init(new:)")
595+
init(old: Int) {}
596+
597+
init(new: Int) {}
598+
599+
convenience init(testSelf: Int) {
600+
self.init(old: testSelf) // expected-warning {{'init(old:)' is deprecated: replaced by 'init(new:)'}} expected-note {{use 'init(new:)' instead}} {{15-18=new}}
601+
}
602+
603+
init(testSuper: Int) {}
604+
}
605+
606+
class DeprecatedInitSub1: DeprecatedInitBase {
607+
override init(testSuper: Int) {
608+
super.init(old: testSuper) // expected-warning {{'init(old:)' is deprecated: replaced by 'init(new:)'}} expected-note {{use 'init(new:)' instead}} {{16-19=new}}
609+
}
610+
}
611+
612+
class DeprecatedInitSub2: DeprecatedInitBase { }
613+
614+
_ = DeprecatedInitSub2(old: 0) // expected-warning {{'init(old:)' is deprecated}}
615+
593616
class Base {
594617
@available(*, unavailable)
595618
func bad() {} // expected-note {{here}}
@@ -1048,14 +1071,6 @@ struct UnavailableAccessors {
10481071
}
10491072
}
10501073

1051-
class BaseDeprecatedInit {
1052-
@available(*, deprecated) init(bad: Int) { }
1053-
}
1054-
1055-
class SubInheritedDeprecatedInit: BaseDeprecatedInit { }
1056-
1057-
_ = SubInheritedDeprecatedInit(bad: 0) // expected-warning {{'init(bad:)' is deprecated}}
1058-
10591074
// Should produce no warnings.
10601075
enum SR8634_Enum: Int {
10611076
case a

0 commit comments

Comments
 (0)