Skip to content

[SR-16075] map(foo) and map{ foo($0) } return different results #58336

@swift-ci

Description

@swift-ci
Previous ID SR-16075
Radar None
Original Reporter OMGHunter (JIRA User)
Type Bug
Environment

Xcode 13.3 (13E113)

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

md5: 383ad533155d7b964df61e4e1f582e7d

Issue Description:

For the following code snippet, res1 and res2 surprisingly have different values:

func test() {
    let a: String? = nil
    let b: String? = nil


    let foo: (String?) -> Int = { $0 == nil ? 0 : 1}


    let res1 = [a, b].compactMap { $0 }.map(foo)
    let res2 = [a, b].compactMap { $0 }.map { foo($0) }


    print("res1: ", res1)
    print("res2: ", res2)
}

test()
// res1:  [0, 0]
// res2:  []

According to the comment on StackOverflow (https://stackoverflow.com/a/71669873/4770353) it's a result of how the compiler selects the types of [a, b].compactMap based on the downstream operation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselftype checkerArea → compiler: Semantic analysistype inferenceFeature: type inference

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions