Skip to content

Remove the '++' and '--' operators. #17918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,28 @@ static bool diagnoseRangeOperatorMisspell(UnresolvedDeclRefExpr *UDRE,
return false;
}

static bool diagnoseIncDecOperator(UnresolvedDeclRefExpr *UDRE,
TypeChecker &TC) {
auto name = UDRE->getName().getBaseIdentifier();
if (!name.isOperator())
return false;

auto corrected = StringRef();
if (name.str() == "++")
corrected = "+= 1";
else if (name.str() == "--")
corrected = "-= 1";

if (!corrected.empty()) {
TC.diagnose(UDRE->getLoc(), diag::use_unresolved_identifier_corrected,
name, true, corrected)
.highlight(UDRE->getSourceRange());

return true;
}
return false;
}

static bool findNonMembers(TypeChecker &TC,
ArrayRef<LookupResultEntry> lookupResults,
DeclRefKind refKind, bool breakOnMember,
Expand Down Expand Up @@ -503,6 +525,7 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
// operator misspelling. Otherwise try to diagnose a juxtaposition
// e.g. (x*-4) that needs whitespace.
if (diagnoseRangeOperatorMisspell(UDRE, *this) ||
diagnoseIncDecOperator(UDRE, *this) ||
diagnoseOperatorJuxtaposition(UDRE, DC, *this)) {
return new (Context) ErrorExpr(UDRE->getSourceRange());
}
Expand Down
21 changes: 0 additions & 21 deletions stdlib/public/core/MigrationSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -369,27 +369,6 @@ extension LazyMapCollection {
}
}

@available(*, unavailable, message: "use += 1")
@discardableResult
public prefix func ++ <F: FloatingPoint>(rhs: inout F) -> F {
fatalError("++ is not available")
}
@available(*, unavailable, message: "use -= 1")
@discardableResult
public prefix func -- <F: FloatingPoint>(rhs: inout F) -> F {
fatalError("-- is not available")
}
@available(*, unavailable, message: "use += 1")
@discardableResult
public postfix func ++ <F: FloatingPoint>(lhs: inout F) -> F {
fatalError("++ is not available")
}
@available(*, unavailable, message: "use -= 1")
@discardableResult
public postfix func -- <F: FloatingPoint>(lhs: inout F) -> F {
fatalError("-- is not available")
}

extension FloatingPoint {
@available(swift, deprecated: 3.1, obsoleted: 4.0, message: "Please use the `abs(_:)` free function")
public static func abs(_ x: Self) -> Self {
Expand Down
6 changes: 1 addition & 5 deletions stdlib/public/core/Policy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -377,8 +377,6 @@ precedencegroup BitwiseShiftPrecedence {
//===----------------------------------------------------------------------===//

// Standard postfix operators.
postfix operator ++
postfix operator --
postfix operator ...

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

// Standard prefix operators.
prefix operator ++
prefix operator --
prefix operator !
prefix operator ~
prefix operator +
Expand Down
1 change: 1 addition & 0 deletions test/Index/kinds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ prefix func -(a: AStruct) -> AStruct { return a }
// CHECK: [[@LINE-1]]:13 | function/prefix-operator/Swift | -(_:) | s:14swift_ide_test1sopyAA7AStructVADF | Def | rel: 0

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

Expand Down
6 changes: 6 additions & 0 deletions test/Parse/optional_chain_lvalues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ struct T {
var mutT: T?
let immT: T? = nil // expected-note {{change 'let' to 'var' to make it mutable}} {{1-4=var}}

postfix operator ++
prefix operator ++

public postfix func ++ <T>(rhs: inout T) -> T { fatalError() }
public prefix func ++ <T>(rhs: inout T) -> T { fatalError() }

mutT?.mutateT()
immT?.mutateT() // expected-error{{cannot use mutating member on immutable value: 'immT' is a 'let' constant}}
mutT?.mutS?.mutateS()
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/recovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ struct ErrorInFunctionSignatureResultArrayType5 {


struct ErrorInFunctionSignatureResultArrayType11 { // expected-note{{in declaration of 'ErrorInFunctionSignatureResultArrayType11'}}
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}}
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}}
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/Sema/immutability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

prefix operator ++
public prefix func ++ <T>(rhs: inout T) -> T { fatalError() }

let bad_property_1: Int { // expected-error {{'let' declarations cannot be computed properties}} {{1-4=var}}
get {
return 42
Expand Down
1 change: 1 addition & 0 deletions test/SourceKit/CodeComplete/complete_operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct MyInt {
var bigPowers: Int { return 1 }
}
func +(x: MyInt, y: MyInt) -> MyInt { return x }
postfix operator ++
postfix func ++(x: inout MyInt) -> MyInt { return x }
func !=(x: MyInt, y: MyInt) -> Bool { return true }

Expand Down
13 changes: 3 additions & 10 deletions test/decl/func/operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ func test_14705150() {

}

postfix operator ++
prefix operator ++

prefix postfix func ++(x: Int) {} // expected-error {{'postfix' contradicts previous modifier 'prefix'}} {{8-16=}}
postfix prefix func ++(x: Float) {} // expected-error {{'prefix' contradicts previous modifier 'postfix'}} {{9-16=}}
postfix prefix infix func ++(x: Double) {} // expected-error {{'prefix' contradicts previous modifier 'postfix'}} {{9-16=}} expected-error {{'infix' contradicts previous modifier 'postfix'}} {{16-22=}}
Expand Down Expand Up @@ -355,13 +358,3 @@ class C6 {
if x == x && x = x { } // expected-error{{expression is not assignable: '&&' returns immutable value}}
}
}

_ = 1..<1 // OK
_ = 1…1 // expected-error {{use of unresolved operator '…'; did you mean '...'?}} {{6-9=...}}
_ = 1….1 // expected-error {{use of unresolved operator '…'; did you mean '...'?}} {{6-9=...}}
_ = 1.…1 // expected-error {{use of unresolved operator '.…'; did you mean '...'?}} {{6-10=...}}
_ = 1…<1 // expected-error {{use of unresolved operator '…<'; did you mean '..<'?}} {{6-10=..<}}
_ = 1..1 // expected-error {{use of unresolved operator '..'; did you mean '...'?}} {{6-8=...}}
_ = 1....1 // expected-error {{use of unresolved operator '....'; did you mean '...'?}} {{6-10=...}}
_ = 1...<1 // expected-error {{use of unresolved operator '...<'; did you mean '..<'?}} {{6-10=..<}}
_ = 1....<1 // expected-error {{use of unresolved operator '....<'; did you mean '..<'?}} {{6-11=..<}}
23 changes: 23 additions & 0 deletions test/decl/func/operator_suggestions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-typecheck-verify-swift

_ = 1..<1 // OK
_ = 1…1 // expected-error {{use of unresolved operator '…'; did you mean '...'?}} {{6-9=...}}
_ = 1….1 // expected-error {{use of unresolved operator '…'; did you mean '...'?}} {{6-9=...}}
_ = 1.…1 // expected-error {{use of unresolved operator '.…'; did you mean '...'?}} {{6-10=...}}
_ = 1…<1 // expected-error {{use of unresolved operator '…<'; did you mean '..<'?}} {{6-10=..<}}
_ = 1..1 // expected-error {{use of unresolved operator '..'; did you mean '...'?}} {{6-8=...}}
_ = 1....1 // expected-error {{use of unresolved operator '....'; did you mean '...'?}} {{6-10=...}}
_ = 1...<1 // expected-error {{use of unresolved operator '...<'; did you mean '..<'?}} {{6-10=..<}}
_ = 1....<1 // expected-error {{use of unresolved operator '....<'; did you mean '..<'?}} {{6-11=..<}}

var i = 1
i++ // expected-error {{use of unresolved operator '++'; did you mean '+= 1'?}}
++i // expected-error {{use of unresolved operator '++'; did you mean '+= 1'?}}
i-- // expected-error {{use of unresolved operator '--'; did you mean '-= 1'?}}
--i // expected-error {{use of unresolved operator '--'; did you mean '-= 1'?}}

var d = 1.0
d++ // expected-error {{use of unresolved operator '++'; did you mean '+= 1'?}}
++d // expected-error {{use of unresolved operator '++'; did you mean '+= 1'?}}
d-- // expected-error {{use of unresolved operator '--'; did you mean '-= 1'?}}
--d // expected-error {{use of unresolved operator '--'; did you mean '-= 1'?}}