Skip to content

Using a non-keypath as a keypath leads to 'unable to infer closure type without a type annotation" #69795

Open
@stackotter

Description

@stackotter

Description
Using something which isn't a keypath as a keypath (by accident, don't ask...) leads to the compiler failing with a very useless error message if you do it inside a closure. The compiler blames a lack of closure type annotations (even if there are type annotations) instead of the invalid keyPath.

// error: unable to infer closure type without a type annotation
let x: (Int) -> Void = { (y: Int) -> Void in
//                     ^~~~~~~~~~~~~~~~~~~~~
  y[keyPath: 5]
  //         ^ the real issue is of course that `5` isn't a keypath
}

The redundant type annotation in the closure itself isn't necessary to reproduce the issue, but just shows that the closure definitely has a type annotation.

Feel free to rename the issue; I wasn't exactly sure what the most helpful name would be because the issue is so weird.

Steps to reproduce
Running swiftc snippet.swift (with the above snippet) reproduces the issue. I have reproduced this on the 2023-10-19-a toolchain but haven't tried anything newer.

Expected behavior
The error message should probably be something equivalent to, 5 isn't a keypath, which is the real cause of the error. But at the very least it should point to the line with the invalid keypath.

Environment

  • Swift compiler version info: swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.2 clang-1500.0.40.1) (but also happens on 2023-10-19-a)
  • Xcode version info: 15.0 build version 15A5219j
  • Deployment target: macOS 14.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.closuresFeature: closuresdiagnostics QoIBug: Diagnostics Quality of Implementationkey pathsFeature: key paths (both native and Objective-C)swift 5.9type 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