Skip to content

Commit 5aeb49f

Browse files
committed
[Sema] Correct fix-its for availability rename
Update to handle multiple trailing closures. rdar://81289222
1 parent 7d6dac6 commit 5aeb49f

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ static void fixItAvailableAttrRename(InFlightDiagnostic &diag,
18721872
auto argList = getOriginalArgumentList(argExpr);
18731873

18741874
size_t numElementsWithinParens = argList.args.size();
1875-
numElementsWithinParens -= argList.hasTrailingClosure;
1875+
numElementsWithinParens -= argList.getNumTrailingClosures();
18761876
if (selfIndex >= numElementsWithinParens)
18771877
return;
18781878

@@ -2120,18 +2120,16 @@ static void fixItAvailableAttrRename(InFlightDiagnostic &diag,
21202120
return;
21212121
}
21222122

2123-
auto argumentLabelsToCheck = llvm::makeArrayRef(argumentLabelIDs);
2124-
// The argument label for a trailing closure is ignored.
2125-
if (argList.hasTrailingClosure)
2126-
argumentLabelsToCheck = argumentLabelsToCheck.drop_back();
2127-
2128-
if (std::equal(argumentLabelsToCheck.begin(), argumentLabelsToCheck.end(),
2129-
argList.labels.begin())) {
2130-
// Already matching.
2131-
return;
2123+
// If any of the argument labels are mismatched, perform label correction.
2124+
for (auto i : indices(argList.args)) {
2125+
// The argument label of an unlabeled trailing closure is ignored.
2126+
if (argList.isUnlabeledTrailingClosureIdx(i))
2127+
continue;
2128+
if (argumentLabelIDs[i] != argList.labels[i]) {
2129+
diagnoseArgumentLabelError(ctx, argExpr, argumentLabelIDs, false, &diag);
2130+
return;
2131+
}
21322132
}
2133-
2134-
diagnoseArgumentLabelError(ctx, argExpr, argumentLabelIDs, false, &diag);
21352133
}
21362134

21372135
// Must be kept in sync with diag::availability_decl_unavailable_rename and

test/attr/attr_availability.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,3 +1128,54 @@ class UnavailableNoArgsSubclassInit: UnavailableNoArgsSuperclassInit {
11281128
// expected-error@-1 {{'init()' is unavailable}}
11291129
// expected-note@-2 {{call to unavailable initializer 'init()' from superclass 'UnavailableNoArgsSuperclassInit' occurs implicitly at the end of this initializer}}
11301130
}
1131+
1132+
struct TypeWithTrailingClosures {
1133+
func twoTrailingClosures(a: () -> Void, b: () -> Void) {}
1134+
func threeTrailingClosures(a: () -> Void, b: () -> Void, c: () -> Void) {}
1135+
func threeUnlabeledTrailingClosures(_ a: () -> Void, _ b: () -> Void, _ c: () -> Void) {}
1136+
func variadicTrailingClosures(a: (() -> Void)..., b: Int = 0, c: Int = 0) {}
1137+
}
1138+
1139+
@available(*, deprecated, renamed: "TypeWithTrailingClosures.twoTrailingClosures(self:a:b:)")
1140+
func twoTrailingClosures(_ x: TypeWithTrailingClosures, a: () -> Void, b: () -> Void) {}
1141+
1142+
@available(*, deprecated, renamed: "TypeWithTrailingClosures.twoTrailingClosures(self:a:b:)")
1143+
func twoTrailingClosuresWithDefaults(x: TypeWithTrailingClosures, y: Int = 0, z: Int = 0, a: () -> Void, b: () -> Void) {}
1144+
1145+
@available(*, deprecated, renamed: "TypeWithTrailingClosures.threeTrailingClosures(self:a:b:c:)")
1146+
func threeTrailingClosures(_ x: TypeWithTrailingClosures, a: () -> Void, b: () -> Void, c: () -> Void) {}
1147+
1148+
@available(*, deprecated, renamed: "TypeWithTrailingClosures.threeTrailingClosures(self:a:b:c:)")
1149+
func threeTrailingClosuresDiffLabels(_: TypeWithTrailingClosures, x: () -> Void, y: () -> Void, z: () -> Void) {}
1150+
1151+
@available(*, deprecated, renamed: "TypeWithTrailingClosures.threeUnlabeledTrailingClosures(self:_:_:_:)")
1152+
func threeTrailingClosuresRemoveLabels(_ x: TypeWithTrailingClosures, a: () -> Void, b: () -> Void, c: () -> Void) {}
1153+
1154+
@available(*, deprecated, renamed: "TypeWithTrailingClosures.variadicTrailingClosures(self:a:b:c:)")
1155+
func variadicTrailingClosures(_ x: TypeWithTrailingClosures, a: (() -> Void)...) {}
1156+
1157+
func testMultipleTrailingClosures(_ x: TypeWithTrailingClosures) {
1158+
twoTrailingClosures(x) {} b: {} // expected-warning {{'twoTrailingClosures(_:a:b:)' is deprecated: replaced by instance method 'TypeWithTrailingClosures.twoTrailingClosures(a:b:)'}}
1159+
// expected-note@-1 {{use 'TypeWithTrailingClosures.twoTrailingClosures(a:b:)' instead}} {{3-22=x.twoTrailingClosures}} {{23-24=}} {{none}}
1160+
x.twoTrailingClosures() {} b: {}
1161+
1162+
twoTrailingClosuresWithDefaults(x: x) {} b: {} // expected-warning {{'twoTrailingClosuresWithDefaults(x:y:z:a:b:)' is deprecated: replaced by instance method 'TypeWithTrailingClosures.twoTrailingClosures(a:b:)'}}
1163+
// expected-note@-1 {{use 'TypeWithTrailingClosures.twoTrailingClosures(a:b:)' instead}} {{3-34=x.twoTrailingClosures}} {{35-39=}} {{none}}
1164+
x.twoTrailingClosures() {} b: {}
1165+
1166+
threeTrailingClosures(x, a: {}) {} c: {} // expected-warning {{'threeTrailingClosures(_:a:b:c:)' is deprecated: replaced by instance method 'TypeWithTrailingClosures.threeTrailingClosures(a:b:c:)'}}
1167+
// expected-note@-1 {{use 'TypeWithTrailingClosures.threeTrailingClosures(a:b:c:)' instead}} {{3-24=x.threeTrailingClosures}} {{25-28=}} {{none}}
1168+
x.threeTrailingClosures(a: {}) {} c: {}
1169+
1170+
threeTrailingClosuresDiffLabels(x, x: {}) {} z: {} // expected-warning {{'threeTrailingClosuresDiffLabels(_:x:y:z:)' is deprecated: replaced by instance method 'TypeWithTrailingClosures.threeTrailingClosures(a:b:c:)'}}
1171+
// expected-note@-1 {{use 'TypeWithTrailingClosures.threeTrailingClosures(a:b:c:)' instead}} {{3-34=x.threeTrailingClosures}} {{35-38=}} {{38-39=a}} {{48-49=c}} {{none}}
1172+
x.threeTrailingClosures(a: {}) {} c: {}
1173+
1174+
threeTrailingClosuresRemoveLabels(x, a: {}) {} c: {} // expected-warning {{'threeTrailingClosuresRemoveLabels(_:a:b:c:)' is deprecated: replaced by instance method 'TypeWithTrailingClosures.threeUnlabeledTrailingClosures(_:_:_:)'}}
1175+
// expected-note@-1 {{use 'TypeWithTrailingClosures.threeUnlabeledTrailingClosures(_:_:_:)' instead}} {{3-36=x.threeUnlabeledTrailingClosures}} {{37-40=}} {{40-43=}} {{50-51=_}} {{none}}
1176+
x.threeUnlabeledTrailingClosures({}) {} _: {}
1177+
1178+
variadicTrailingClosures(x) {} _: {} _: {} // expected-warning {{'variadicTrailingClosures(_:a:)' is deprecated: replaced by instance method 'TypeWithTrailingClosures.variadicTrailingClosures(a:b:c:)'}}
1179+
// expected-note@-1 {{use 'TypeWithTrailingClosures.variadicTrailingClosures(a:b:c:)' instead}} {{3-27=x.variadicTrailingClosures}} {{28-29=}} {{none}}
1180+
x.variadicTrailingClosures() {} _: {} _: {}
1181+
}

0 commit comments

Comments
 (0)