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-2687] @discardableResult won't work when Optional chaining + Optional return value #45292

Open
swift-ci opened this issue Sep 18, 2016 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2687
Radar None
Original Reporter inamiy (JIRA User)
Type Bug
Environment

Xcode 8

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

md5: 15cd9b5dacd13a919a798211f1ade407

Issue Description:

@discardableResult won't work in Swift 3 (Xcode 8) when:

1. Optional chaining (already solved in old issues)
2. Optional return value (probably new)

Please see below gist for bug reproduction.
https://gist.github.com/inamiy/d4afae28fad9d206a1f8d5dcac871d4c

// MARK: Minimal code from ReactiveCocoa/ReactiveSwift

public protocol SignalProtocol {
    associatedtype Value
    associatedtype Error: Swift.Error

}
extension SignalProtocol where Error == NoError {
    /// - Note: Return value is Optional + non-Void type.
    @discardableResult
    public func observe() -> Bool? {
        return true
    }
}

public enum NoError: Swift.Error { }

public final class Signal<V, E: Swift.Error>: SignalProtocol {
    public typealias Value = V
    public typealias Error = E

    public init() {}
}

// MARK: Main

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let signal = Signal<(), NoError>()
        signal.observe() // OK, no warning

        let optSignal: Signal<(), NoError>? = Signal<(), NoError>()
        optSignal?.observe()    // Bad, unused warning in Swift 3 (Xcode 8)

        return true
    }

}
@keith
Copy link
Collaborator

keith commented Oct 10, 2016

Looks like this might be a duplicate of https://bugs.swift.org/browse/SR-1929

@swift-ci
Copy link
Collaborator Author

swift-ci commented Feb 8, 2017

Comment by Ray Lillywhite (JIRA)

I think this is the opposite problem of SR-1929. Presumably when SR-1929 was fixed, it caused this problem instead. And this is still a problem in Xcode 8.2 (8C38).

@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.
Projects
None yet
Development

No branches or pull requests

2 participants