Skip to content

Commit e09be3b

Browse files
committed
Continue improving initializers in @available(renamed:).
Refinement of d858cc9. Handle members renamed to initializers as well as free functions renamed to initializers.
1 parent 2b876c0 commit e09be3b

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,23 @@ void swift::fixItAvailableAttrRename(TypeChecker &TC,
11151115

11161116
// Continue on to diagnose any argument label renames.
11171117

1118-
} else if (parsed.BaseName == TC.Context.Id_init.str() &&
1119-
parsed.isMember() && CE) {
1118+
} else if (parsed.BaseName == TC.Context.Id_init.str() && CE) {
11201119
// For initializers, replace with a "call" of the context type...but only
11211120
// if we know we're doing a call (rather than a first-class reference).
1122-
diag.fixItReplace(referenceRange, parsed.ContextName);
1121+
if (parsed.isMember()) {
1122+
diag.fixItReplace(CE->getFn()->getSourceRange(), parsed.ContextName);
1123+
1124+
} else {
1125+
auto *dotCall = dyn_cast<DotSyntaxCallExpr>(CE->getFn());
1126+
if (!dotCall)
1127+
return;
1128+
1129+
SourceLoc removeLoc = dotCall->getDotLoc();
1130+
if (removeLoc.isInvalid())
1131+
return;
1132+
1133+
diag.fixItRemove(SourceRange(removeLoc, dotCall->getFn()->getEndLoc()));
1134+
}
11231135

11241136
} else {
11251137
// Just replace the base name.
@@ -1231,11 +1243,13 @@ describeRename(ASTContext &ctx, const AvailableAttr *attr,
12311243
// Only produce special descriptions for renames to
12321244
// - instance members
12331245
// - properties (or global bindings)
1234-
// - class/static methods (and initializers)
1246+
// - class/static methods
1247+
// - initializers, even if unqualified
12351248
// Leave non-member renames alone, as well as renames from top-level types
12361249
// and bindings to member types and class/static properties.
12371250
if (!(parsed.isInstanceMember() || parsed.isPropertyAccessor() ||
1238-
(parsed.isMember() && parsed.IsFunctionName))) {
1251+
(parsed.isMember() && parsed.IsFunctionName) ||
1252+
(parsed.BaseName == ctx.Id_init.str()))) {
12391253
return None;
12401254
}
12411255

test/attr/attr_availability.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,21 @@ func testRenameSetters() {
478478
var x = 0
479479
unavailableSetInstancePropertyInout(a: &x, b: 2) // expected-error{{'unavailableSetInstancePropertyInout(a:b:)' has been replaced by property 'Int.prop'}} {{3-38=x.prop}} {{38-49= = }} {{50-51=}}
480480
}
481+
482+
extension Int {
483+
@available(*, unavailable, renamed: "init(other:)")
484+
static func factory(other: Int) -> Int { return other } // expected-note 2 {{here}}
485+
486+
@available(*, unavailable, renamed: "Int.init(other:)")
487+
static func factory2(other: Int) -> Int { return other } // expected-note 2 {{here}}
488+
489+
static func testFactoryMethods() {
490+
factory(other: 1) // expected-error {{'factory(other:)' has been replaced by 'init(other:)'}} {{none}}
491+
factory2(other: 1) // expected-error {{'factory2(other:)' has been replaced by 'Int.init(other:)'}} {{5-13=Int}}
492+
}
493+
}
494+
495+
func testFactoryMethods() {
496+
Int.factory(other: 1) // expected-error {{'factory(other:)' has been replaced by 'init(other:)'}} {{6-14=}}
497+
Int.factory2(other: 1) // expected-error {{'factory2(other:)' has been replaced by 'Int.init(other:)'}} {{3-15=Int}}
498+
}

0 commit comments

Comments
 (0)