Skip to content
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

[SR-1692] Custom prefix/postfix increment/decrement operators fail #44301

Open
swift-ci opened this issue Jun 6, 2016 · 2 comments
Open

[SR-1692] Custom prefix/postfix increment/decrement operators fail #44301

swift-ci opened this issue Jun 6, 2016 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jun 6, 2016

Previous ID SR-1692
Radar None
Original Reporter erica (JIRA User)
Type Bug
Environment

Swift 3 on OS X and Linux

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 76ece653521e2f9c0d42a9eb83ea7fe9

Issue Description:

Although ++ is removed from the language, you'd think you could add it back in

@belkadan
Copy link
Contributor

belkadan commented Jun 6, 2016

I'm pretty sure this is an overload ordering question, and I'm not convinced the compiler is wrong. If something is a more specific overload and marked unavailable, silently dropping back to the less-specific overload might be counterintuitive. (It also means the behavior of existing code could change silently if something is marked unavailable between releases.)

@swift-ci
Copy link
Collaborator Author

Comment by erica sadun (JIRA)

This is the behavior under the latest beta:

// THIS WORKS
prefix operator ++*
postfix operator ++*
prefix operator --*
postfix operator --*

@discardableResult
prefix public func ++* <T: Integer>(x: inout T) -> T {
    x = x + 1; return x
}
@discardableResult
postfix public func ++* <T: Integer>(x: inout T) -> T {
    defer{ x = x + 1 }; return x
}
@discardableResult
prefix public func --* <T: Integer>(x: inout T) -> T {
    x = x - 1; return x
}
@discardableResult
postfix public func --* <T: Integer>(x: inout T) -> T {
    defer{ x = x - 1 }; return x
}

And this does not work:

// THIS DOESN'T WORK
prefix operator ++
postfix operator ++
prefix operator --
postfix operator --

@discardableResult
prefix public func ++ <T: Integer>(x: inout T) -> T {
    x = x + 1; return x
}
@discardableResult
postfix public func ++ <T: Integer>(x: inout T) -> T {
    defer{ x = x + 1 }; return x
}
@discardableResult
prefix public func -- <T: Integer>(x: inout T) -> T {
    x = x - 1; return x
}
@discardableResult
postfix public func -- <T: Integer>(x: inout T) -> T {
    defer{ x = x - 1 }; return x
}

// Bug report filed: https://bugs.swift.org/browse/SR-1692
// Uncomment for errors, making sure to comment the version below
let a: Int8 = 5
//let b = ++a
//let c = a++
//print(a, b, c) // 7, 6, 6

However, individual type overloads do work:

// This works
@discardableResult
prefix public func ++(x: inout Int) -> Int { x = x + 1; return x }
@discardableResult
prefix public func ++(x: inout UInt) -> UInt { x = x + 1; return x }
@discardableResult
prefix public func --(x: inout Int) -> Int { x = x - 1; return x }
@discardableResult
prefix public func --(x: inout UInt) -> UInt { x = x - 1; return x }

@discardableResult
postfix public func ++(x: inout Int) -> Int { defer { x = x + 1 }; return x }
@discardableResult
postfix public func ++(x: inout UInt) -> UInt { defer { x = x + 1 }; return x }
@discardableResult
postfix public func --(x: inout Int) -> Int { defer { x = x - 1 }; return x }
@discardableResult
postfix public func --(x: inout UInt) -> UInt { defer { x = x - 1 }; return x }

var j = 5
print(++j, j, j++, j, --j, j, j--, j) // 6 6 6 7 6 6 6 5

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants