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-15270] Combine compactMap passes nil downstream #57592

Open
swift-ci opened this issue Oct 1, 2021 · 0 comments
Open

[SR-15270] Combine compactMap passes nil downstream #57592

swift-ci opened this issue Oct 1, 2021 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Oct 1, 2021

Previous ID SR-15270
Radar None
Original Reporter Darko (JIRA User)
Type Bug
Environment

Xcode Version 13.0 (13A233)
macOS 11.6 (20G165)

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

md5: 7bb5845d0edc8498ca4240cdb1ec5c96

Issue Description:

I get an unexpected result by using Combine's assign(to: directly after compactMap. Here the code, it's 100% reproducible for me in Xcodes Playground, Xcode Version 13.0 (13A233).

import Combine
import Foundation

class Test {
    @Published var current: String?
    @Published var latest1: String?
    @Published var latest2: String?
    @Published var latest3: String?

    init() {
        // compactMap passes nil downstream
        self.$current
           .compactMap { $0 }
           .assign(to: &self.$latest1) 
     
        // Fixed by specifying the transform closure type explicitly
        self.$current
            .compactMap { value -> String? in value }
            .assign(to: &self.$latest2)
 
         // Fixed by an additional map inbetween
         self.$current
            .compactMap { $0 }
            .map { $0 }
            .assign(to: &self.$latest3)
    }
}

let obj = Test()

obj.current = "success"

print("current: \(String(describing: obj.current))")
print("latest1: \(String(describing: obj.latest1))")
print("latest2: \(String(describing: obj.latest2))")
print("latest3: \(String(describing: obj.latest3))")
print("")

obj.current = nil

print("current: \(String(describing: obj.current))")
print("latest1: \(String(describing: obj.latest1))") // nil shouldn't arrive here
print("latest2: \(String(describing: obj.latest2))")
print("latest3: \(String(describing: obj.latest3))")

// Output:
//current: Optional("success")
//latest1: Optional("success")
//latest2: Optional("success")
//latest3: Optional("success")
//
//current: nil
//latest1: nil
//latest2: Optional("success")
//latest3: Optional("success")

I assume some type inference bug because .sink and .assign(to:on are not affected.

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

No branches or pull requests

1 participant