Skip to content

Commit 443af4a

Browse files
committed
In the deprecation warnings for ++/-- on index types, emit a fixit hint to rewrite into
the correct call to .successor() or .predecessor(). This wraps up <rdar://problem/23708702> Emit deprecation warnings for ++/-- in Swift 2.2
1 parent 2da0f60 commit 443af4a

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

include/swift/Parse/Lexer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,9 @@ class Lexer {
264264
/// resides.
265265
///
266266
/// \param SR The source range
267-
static CharSourceRange getCharSourceRangeFromSourceRange(const SourceManager &SM,
268-
const SourceRange &SR) {
267+
static CharSourceRange
268+
getCharSourceRangeFromSourceRange(const SourceManager &SM,
269+
const SourceRange &SR) {
269270
return CharSourceRange(SM, SR.Start, getLocForEndOfToken(SM, SR.End));
270271
}
271272

lib/Sema/MiscDiagnostics.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,19 +1113,30 @@ bool AvailabilityWalker::diagnoseIncDecDeprecation(const ValueDecl *D,
11131113

11141114
// If the expression type is integer or floating point, then we can rewrite it
11151115
// to "lvalue += 1".
1116-
if (isIntegerOrFloatingPointType(call->getType(), DC, TC)) {
1117-
const char *addition = isInc ? " += 1" : " -= 1";
1118-
1116+
std::string replacement;
1117+
if (isIntegerOrFloatingPointType(call->getType(), DC, TC))
1118+
replacement = isInc ? " += 1" : " -= 1";
1119+
else {
1120+
// Otherwise, it must be an index type. Rewrite to:
1121+
// "lvalue = lvalue.successor()".
1122+
auto &SM = TC.Context.SourceMgr;
1123+
auto CSR = Lexer::getCharSourceRangeFromSourceRange(SM,
1124+
call->getArg()->getSourceRange());
1125+
replacement = " = " + SM.extractText(CSR).str();
1126+
replacement += isInc ? ".successor()" : ".predecessor()";
1127+
}
1128+
1129+
if (!replacement.empty()) {
11191130
// If we emit a deprecation diagnostic, produce a fixit hint as well.
11201131
TC.diagnoseDeprecated(R, DC, Attr, D->getFullName(),
11211132
[&](InFlightDiagnostic &diag) {
11221133
if (isa<PrefixUnaryExpr>(call)) {
11231134
// Prefix: remove the ++ or --.
11241135
diag.fixItRemove(call->getFn()->getSourceRange());
1125-
diag.fixItInsertAfter(call->getArg()->getEndLoc(), addition);
1136+
diag.fixItInsertAfter(call->getArg()->getEndLoc(), replacement);
11261137
} else {
11271138
// Postfix: replace the ++ or --.
1128-
diag.fixItReplace(call->getFn()->getSourceRange(), addition);
1139+
diag.fixItReplace(call->getFn()->getSourceRange(), replacement);
11291140
}
11301141
});
11311142

test/expr/expressions.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,10 @@ func swift22_deprecation_increment_decrement() {
819819
_ = f-- // expected-warning {{'--' is deprecated: it will be removed in Swift 3}}
820820

821821

822-
++si // expected-warning {{'++' is deprecated: it will be removed in Swift 3}}
823-
--si // expected-warning {{'--' is deprecated: it will be removed in Swift 3}}
822+
++si // expected-warning {{'++' is deprecated: it will be removed in Swift 3}} {{3-5=}} {{7-7= = si.successor()}}
823+
--si // expected-warning {{'--' is deprecated: it will be removed in Swift 3}} {{3-5=}} {{7-7= = si.predecessor()}}
824+
si++ // expected-warning {{'++' is deprecated: it will be removed in Swift 3}} {{5-7= = si.successor()}}
825+
si-- // expected-warning {{'--' is deprecated: it will be removed in Swift 3}} {{5-7= = si.predecessor()}}
824826
_ = --si // expected-warning {{'--' is deprecated: it will be removed in Swift 3}}
825827
}
826828

0 commit comments

Comments
 (0)