Skip to content

[SR-14096] KeyPath when used as a multi-argument function type #56482

Closed
@swift-ci

Description

@swift-ci
Previous ID SR-14096
Radar rdar://problem/73600325
Original Reporter artdivin (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Swift 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)

Xcode 12.4 (12D4e)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee artdivin (JIRA)
Priority Medium

md5: 23293c02146f4ec64ff505bd8d184b25

Issue Description:

Hello!

I have found an undefined behaviour when compiler doesn't generate correct error messages, i.e. compilation succeeds if the type is omitted, but when the type is specified the compilation error is generated correctly.

See cases #2 and #3 in the attached screenshot.

I will add textual representation into the description as well:

import Foundation


extension String {
    
    var filterOut : (Self) throws -> Bool {
        { $0.contains("a") }
    }
    
    var myLocalizedCompared : (Self, Self) throws -> Bool {
        {
            $0.localizedStandardCompare($1) == .orderedAscending
        }
    }
    
}


var unsorted : [String] = [ "asd", "bcd", "def" ]


// 1. compiles, provides output "asd"
print(unsorted.filter { $0.contains("a") })


// 2. compiles, provides empty output
print(unsorted.filter(\.filterOut))


// 3. error: Cannot convert value of type 'KeyPath<String, (String) throws -> Bool>'
// to expected argument type '(String) throws -> Bool'
//print(unsorted.filter(\String.filterOut))


// 4. compiles: however, filter closure signature: (T) throws -> Bool
// and we are passing (T, T) throws -> Bool
// output is empty
print(unsorted.filter(\.myLocalizedCompared))


// 5. error: Cannot convert key path into
// a multi-argument function type '(String, String) throws -> Bool'
//unsorted.sort(by: \.myLocalizedCompared)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions