Skip to content

Commit 51f8c93

Browse files
authored
Merge pull request #17918 from rudkx/remove-inc-dec-operators
Remove the '++' and '--' operators.
2 parents 45634bd + 8ee5276 commit 51f8c93

File tree

10 files changed

+62
-37
lines changed

10 files changed

+62
-37
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,28 @@ static bool diagnoseRangeOperatorMisspell(UnresolvedDeclRefExpr *UDRE,
429429
return false;
430430
}
431431

432+
static bool diagnoseIncDecOperator(UnresolvedDeclRefExpr *UDRE,
433+
TypeChecker &TC) {
434+
auto name = UDRE->getName().getBaseIdentifier();
435+
if (!name.isOperator())
436+
return false;
437+
438+
auto corrected = StringRef();
439+
if (name.str() == "++")
440+
corrected = "+= 1";
441+
else if (name.str() == "--")
442+
corrected = "-= 1";
443+
444+
if (!corrected.empty()) {
445+
TC.diagnose(UDRE->getLoc(), diag::use_unresolved_identifier_corrected,
446+
name, true, corrected)
447+
.highlight(UDRE->getSourceRange());
448+
449+
return true;
450+
}
451+
return false;
452+
}
453+
432454
static bool findNonMembers(TypeChecker &TC,
433455
ArrayRef<LookupResultEntry> lookupResults,
434456
DeclRefKind refKind, bool breakOnMember,
@@ -503,6 +525,7 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
503525
// operator misspelling. Otherwise try to diagnose a juxtaposition
504526
// e.g. (x*-4) that needs whitespace.
505527
if (diagnoseRangeOperatorMisspell(UDRE, *this) ||
528+
diagnoseIncDecOperator(UDRE, *this) ||
506529
diagnoseOperatorJuxtaposition(UDRE, DC, *this)) {
507530
return new (Context) ErrorExpr(UDRE->getSourceRange());
508531
}

stdlib/public/core/MigrationSupport.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -369,27 +369,6 @@ extension LazyMapCollection {
369369
}
370370
}
371371

372-
@available(*, unavailable, message: "use += 1")
373-
@discardableResult
374-
public prefix func ++ <F: FloatingPoint>(rhs: inout F) -> F {
375-
fatalError("++ is not available")
376-
}
377-
@available(*, unavailable, message: "use -= 1")
378-
@discardableResult
379-
public prefix func -- <F: FloatingPoint>(rhs: inout F) -> F {
380-
fatalError("-- is not available")
381-
}
382-
@available(*, unavailable, message: "use += 1")
383-
@discardableResult
384-
public postfix func ++ <F: FloatingPoint>(lhs: inout F) -> F {
385-
fatalError("++ is not available")
386-
}
387-
@available(*, unavailable, message: "use -= 1")
388-
@discardableResult
389-
public postfix func -- <F: FloatingPoint>(lhs: inout F) -> F {
390-
fatalError("-- is not available")
391-
}
392-
393372
extension FloatingPoint {
394373
@available(swift, deprecated: 3.1, obsoleted: 4.0, message: "Please use the `abs(_:)` free function")
395374
public static func abs(_ x: Self) -> Self {

stdlib/public/core/Policy.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -377,8 +377,6 @@ precedencegroup BitwiseShiftPrecedence {
377377
//===----------------------------------------------------------------------===//
378378

379379
// Standard postfix operators.
380-
postfix operator ++
381-
postfix operator --
382380
postfix operator ...
383381

384382
// Optional<T> unwrapping operator is built into the compiler as a part of
@@ -387,8 +385,6 @@ postfix operator ...
387385
// postfix operator !
388386

389387
// Standard prefix operators.
390-
prefix operator ++
391-
prefix operator --
392388
prefix operator !
393389
prefix operator ~
394390
prefix operator +

test/Index/kinds.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ prefix func -(a: AStruct) -> AStruct { return a }
185185
// CHECK: [[@LINE-1]]:13 | function/prefix-operator/Swift | -(_:) | s:14swift_ide_test1sopyAA7AStructVADF | Def | rel: 0
186186

187187
// PostfixOperator
188+
postfix operator ++
188189
postfix func ++(a: AStruct) -> AStruct { return a }
189190
// CHECK: [[@LINE-1]]:14 | function/postfix-operator/Swift | ++(_:) | s:14swift_ide_test2ppoPyAA7AStructVADF | Def | rel: 0
190191

test/Parse/optional_chain_lvalues.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ struct T {
2121
var mutT: T?
2222
let immT: T? = nil // expected-note {{change 'let' to 'var' to make it mutable}} {{1-4=var}}
2323

24+
postfix operator ++
25+
prefix operator ++
26+
27+
public postfix func ++ <T>(rhs: inout T) -> T { fatalError() }
28+
public prefix func ++ <T>(rhs: inout T) -> T { fatalError() }
29+
2430
mutT?.mutateT()
2531
immT?.mutateT() // expected-error{{cannot use mutating member on immutable value: 'immT' is a 'let' constant}}
2632
mutT?.mutS?.mutateS()

test/Parse/recovery.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ struct ErrorInFunctionSignatureResultArrayType5 {
489489

490490

491491
struct ErrorInFunctionSignatureResultArrayType11 { // expected-note{{in declaration of 'ErrorInFunctionSignatureResultArrayType11'}}
492-
func foo() -> Int[(a){a++}] { // expected-error {{consecutive declarations on a line must be separated by ';'}} {{29-29=;}} expected-error {{expected ']' in array type}} expected-note {{to match this opening '['}} expected-error {{use of unresolved identifier 'a'}} expected-error {{expected declaration}}
492+
func foo() -> Int[(a){a++}] { // expected-error {{consecutive declarations on a line must be separated by ';'}} {{29-29=;}} expected-error {{expected ']' in array type}} expected-note {{to match this opening '['}} expected-error {{use of unresolved operator '++'; did you mean '+= 1'?}} expected-error {{use of unresolved identifier 'a'}} expected-error {{expected declaration}}
493493
}
494494
}
495495

test/Sema/immutability.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
func markUsed<T>(_ t: T) {}
44

5+
prefix operator ++
6+
public prefix func ++ <T>(rhs: inout T) -> T { fatalError() }
7+
58
let bad_property_1: Int { // expected-error {{'let' declarations cannot be computed properties}} {{1-4=var}}
69
get {
710
return 42

test/SourceKit/CodeComplete/complete_operators.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct MyInt {
1111
var bigPowers: Int { return 1 }
1212
}
1313
func +(x: MyInt, y: MyInt) -> MyInt { return x }
14+
postfix operator ++
1415
postfix func ++(x: inout MyInt) -> MyInt { return x }
1516
func !=(x: MyInt, y: MyInt) -> Bool { return true }
1617

test/decl/func/operator.swift

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ func test_14705150() {
142142

143143
}
144144

145+
postfix operator ++
146+
prefix operator ++
147+
145148
prefix postfix func ++(x: Int) {} // expected-error {{'postfix' contradicts previous modifier 'prefix'}} {{8-16=}}
146149
postfix prefix func ++(x: Float) {} // expected-error {{'prefix' contradicts previous modifier 'postfix'}} {{9-16=}}
147150
postfix prefix infix func ++(x: Double) {} // expected-error {{'prefix' contradicts previous modifier 'postfix'}} {{9-16=}} expected-error {{'infix' contradicts previous modifier 'postfix'}} {{16-22=}}
@@ -355,13 +358,3 @@ class C6 {
355358
if x == x && x = x { } // expected-error{{expression is not assignable: '&&' returns immutable value}}
356359
}
357360
}
358-
359-
_ = 1..<1 // OK
360-
_ = 11 // expected-error {{use of unresolved operator '…'; did you mean '...'?}} {{6-9=...}}
361-
_ = 1.1 // expected-error {{use of unresolved operator '…'; did you mean '...'?}} {{6-9=...}}
362-
_ = 1.1 // expected-error {{use of unresolved operator '.…'; did you mean '...'?}} {{6-10=...}}
363-
_ = 1<1 // expected-error {{use of unresolved operator '…<'; did you mean '..<'?}} {{6-10=..<}}
364-
_ = 1..1 // expected-error {{use of unresolved operator '..'; did you mean '...'?}} {{6-8=...}}
365-
_ = 1....1 // expected-error {{use of unresolved operator '....'; did you mean '...'?}} {{6-10=...}}
366-
_ = 1...<1 // expected-error {{use of unresolved operator '...<'; did you mean '..<'?}} {{6-10=..<}}
367-
_ = 1....<1 // expected-error {{use of unresolved operator '....<'; did you mean '..<'?}} {{6-11=..<}}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
_ = 1..<1 // OK
4+
_ = 11 // expected-error {{use of unresolved operator '…'; did you mean '...'?}} {{6-9=...}}
5+
_ = 1.1 // expected-error {{use of unresolved operator '…'; did you mean '...'?}} {{6-9=...}}
6+
_ = 1.1 // expected-error {{use of unresolved operator '.…'; did you mean '...'?}} {{6-10=...}}
7+
_ = 1<1 // expected-error {{use of unresolved operator '…<'; did you mean '..<'?}} {{6-10=..<}}
8+
_ = 1..1 // expected-error {{use of unresolved operator '..'; did you mean '...'?}} {{6-8=...}}
9+
_ = 1....1 // expected-error {{use of unresolved operator '....'; did you mean '...'?}} {{6-10=...}}
10+
_ = 1...<1 // expected-error {{use of unresolved operator '...<'; did you mean '..<'?}} {{6-10=..<}}
11+
_ = 1....<1 // expected-error {{use of unresolved operator '....<'; did you mean '..<'?}} {{6-11=..<}}
12+
13+
var i = 1
14+
i++ // expected-error {{use of unresolved operator '++'; did you mean '+= 1'?}}
15+
++i // expected-error {{use of unresolved operator '++'; did you mean '+= 1'?}}
16+
i-- // expected-error {{use of unresolved operator '--'; did you mean '-= 1'?}}
17+
--i // expected-error {{use of unresolved operator '--'; did you mean '-= 1'?}}
18+
19+
var d = 1.0
20+
d++ // expected-error {{use of unresolved operator '++'; did you mean '+= 1'?}}
21+
++d // expected-error {{use of unresolved operator '++'; did you mean '+= 1'?}}
22+
d-- // expected-error {{use of unresolved operator '--'; did you mean '-= 1'?}}
23+
--d // expected-error {{use of unresolved operator '--'; did you mean '-= 1'?}}

0 commit comments

Comments
 (0)