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-5295] "type of expression is ambiguous" when using key-path in generic context #47870

Closed
natecook1000 opened this issue Jun 24, 2017 · 7 comments
Assignees
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 type checker Area → compiler: Semantic analysis

Comments

@natecook1000
Copy link
Member

Previous ID SR-5295
Radar rdar://problem/33142120
Original Reporter @natecook1000
Type Bug
Status Resolved
Resolution Done
Environment

swift/master

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

md5: c814eec522b03be8a333d4c3202012f7

Issue Description:

When I try to define a generic sorting algorithm on MutableCollection I get an unexpected error:

extension MutableCollection {
  mutating func sort<T: Comparable>(by path: KeyPath<Element, T>) {
    self.sort(by: { $0[keyPath: path] < $1[keyPath: path] })
  }
}
// error: type of expression is ambiguous without more context
//      self.sort(by: { $0[keyPath: path] < $1[keyPath: path] })
//                      ~~^~~~~~~~~~~~~~~

Oddly, a similar function compiles without error as a Sequence extension:

extension Sequence {
  func sorted<T: Comparable>(by path: KeyPath<Element, T>) -> [Element] {
    return sorted(by: { $0[keyPath: path] < $1[keyPath: path] })
  }
}
@belkadan
Copy link
Contributor

@rudkx, @xedin?

@rudkx
Copy link
Member

rudkx commented Jul 5, 2017

@swift-ci create

@rudkx
Copy link
Member

rudkx commented Jul 11, 2017

@jckarter Do you have a dup for this?

@jckarter
Copy link
Member

Not this particular one.

@rudkx
Copy link
Member

rudkx commented Jul 13, 2017

The issue here is that the extension is missing where Self : RandomAccessCollection

It would be great if we could give a better diagnostic for this.

@xedin
Copy link
Member

xedin commented Apr 23, 2019

Looks like this has been improved in 5.0:

 error: referencing instance method 'sort(by:)' on 'MutableCollection' requires that 'Self' conform to 'RandomAccessCollection'
              self.sort(by: { $0[keyPath: path] < $1[keyPath: path] })
                   ^

and even further improved on master:

 error: referencing instance method 'sort(by:)' on 'MutableCollection' requires that 'Self' conform to 'RandomAccessCollection'
      self.sort(by: { $0[keyPath: path] < $1[keyPath: path] })
           ^
Swift.MutableCollection:1:11: note: where 'Self' = 'Self'
extension MutableCollection where Self : RandomAccessCollection {
          ^

@xedin
Copy link
Member

xedin commented Apr 23, 2019

Please verify using 5.0 release and/or master nightly snapshot.

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants