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-10066] Unhelpful diagnostic when keypath types don't match #52468

Closed
hamishknight opened this issue Mar 8, 2019 · 3 comments
Closed

[SR-10066] Unhelpful diagnostic when keypath types don't match #52468

hamishknight opened this issue Mar 8, 2019 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation key paths Feature: key paths (both native and Objective-C) type checker Area → compiler: Semantic analysis

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-10066
Radar None
Original Reporter @hamishknight
Type Bug
Status Closed
Resolution Done
Environment

Swift version 5.0-dev (LLVM 94d957ca75, Swift a699f84c0f)
Target: x86_64-apple-darwin18.2.0

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

md5: 32d36056c6f2a654e480dd34153dafee

Issue Description:

For the following code:

struct C {
  func get<Root, Value>(
    _ root: Root, _ keyPath: KeyPath<Root, Value?>
  ) -> Value? {
    return nil
  }
}

struct S {
  var foo: Int {
    return C().get(self, \.foo)
  }
}

We emit the following diagnostic:

/Users/hamishknight/Desktop/Stochastic Projects/lets build associated objects/lets build associated objects/main.swift:22:28: error: type '_' has no member 'foo'
    return C().get(self, \.foo)
                           ^
Swift.Bool:1:15: note: did you mean 'Bool'?
public struct Bool {

This could be improved to point to the actual source of the error, which is that we're trying to pass a KeyPath<C, Int> argument to a KeyPath<C, Value?> parameter.

@belkadan
Copy link
Contributor

cc @xedin

@LucianoPAlmeida
Copy link
Collaborator

@hamishknight This seems fixed on master
Here is what I'm seeing on the latest snapshot

struct C {
  func get<Root, Value>(
    _ root: Root, _ keyPath: KeyPath<Root, Value?>
  ) -> Value? {
    return nil
  }
}

struct S {
  var foo: Int {
    return C().get(self, \.foo) //Key path value type 'Int' cannot be converted to contextual type 'Int?' 
    // Cannot convert return expression of type 'Int?' to return type 'Int'
  }
}

@hamishknight
Copy link
Collaborator Author

Thanks for checking @LucianoPAlmeida!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 diagnostics QoI Bug: Diagnostics Quality of Implementation key paths Feature: key paths (both native and Objective-C) type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants