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-8791] flatMap -> compactMap deprecation false positive warning #51299

Open
BenchR267 opened this issue Sep 18, 2018 · 3 comments
Open

[SR-8791] flatMap -> compactMap deprecation false positive warning #51299

BenchR267 opened this issue Sep 18, 2018 · 3 comments
Assignees
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

@BenchR267
Copy link
Member

Previous ID SR-8791
Radar None
Original Reporter @BenchR267
Type Bug
Status In Progress
Resolution
Environment

Swift 4.2

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee @gregomni
Priority Medium

md5: 47c333e90d55c58e5ac502989e8c6bb6

Issue Description:

I have code similar to this in my project:

struct Bar {
    func strs() -> [String] {
        return ["foo", "bar"]
    }
}


struct Foo {
    func anArray() -> [Any]? {
        let bars = [Bar(), Bar()]
        return bars.flatMap { $0.strs() }
    }
}

When compiling the code, I get a warning that flatMap is deprecated and I should switch to compactMap:

Untitled.swift:10:15: warning: 'flatMap' is deprecated: Please use compactMap(_:) for the case where closure returns an optional value
                return bars.flatMap { $0.strs() }
                            ^
Untitled.swift:10:15: note: use 'compactMap(_:)' instead
                return bars.flatMap { $0.strs() }
                            ^~~~~~~
                            compactMap

But compactMap can only be used if the closure given returns an Optional, in this case I'm returning an Array (which should be flattened). Replacing flatMap with compactMap actually builds fine although the result doesn't get flattened.

@belkadan
Copy link
Contributor

That sounds like we're picking the wrong overload, i.e. the flatMap version isn't flattening.

cc @rudkx, @xedin

@BenchR267
Copy link
Member Author

Ah true, that's actually producing another bug I'm currently investigating!
I'm working around by creating a temporary local variable btw, if someone else is stepping in to this problem.

@gregomni
Copy link
Collaborator

gregomni commented Oct 8, 2018

Both overloads are ending up with `[Any]` and performing a value-to-optional to convert to `[Any]?`, but before that:

The desired overload has to do a collection-upcast to convert `[String]` to `[Any]` in the closure.

The wrong overload is doing an empty-existential-conversion to convert `[String]` to `Any` in the closure, plus a value-to-optional to convert `Any` to `Any?`.

But the way that scoring in the constraint system is currently set up, having a single collection-upcast scores worse than the empty-existential-conversion + value-to-optional.

@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

3 participants