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-12531] Type-checker fails when determining subclassibility of keypath types #54974

Closed
swift-ci opened this issue Apr 6, 2020 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Apr 6, 2020

Previous ID SR-12531
Radar rdar://problem/62201666
Original Reporter xy (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 0b854524f63fd94b3e636a8512220ff9

Issue Description:

The Problem:

I got this error today in Xcode 11.4. This seems like a compiler bug:

'P.KP' cannot be a subclass of both 'PartialKeyPath<Property.Root>' and 'WritableKeyPath<Property.Root, Property.Value>'

This seems wrong, because WritableKeyPath inherits from KeyPath, which inherits from PartialKeyPath. By definition then, any subclass of WritableKeyPath is a subclass of PartialKeyPath when they share the same root, so what am I missing here?

This also happens on Xcode 11.3 and 10.3.

Here is the underlying code:

protocol AnyPropertyProtocol {
    associatedtype Root = Any
    associatedtype Value = Any
    associatedtype KP: AnyKeyPath
    var key: KP { get }
    var value: Value { get }
}

protocol PartialPropertyProtocol: AnyPropertyProtocol
where KP: PartialKeyPath<Root> {
}

protocol PropertyProtocol: PartialPropertyProtocol
where KP: WritableKeyPath<Root, Value> {
}

extension Dictionary where Value: AnyPropertyProtocol {
    subscript<R, V, P>(key: Key, path: WritableKeyPath<R, V>) -> P? where P: PropertyProtocol, P.Root == R, P.Value == V {
        return self[key] as? P
    }
}

The faulty compiler error is thrown on the subscript declaration.

Current Workaround

Changing the subscript declaration to the below code compiles fine, even though it seems to say the same thing (unless I'm missing something):

subscript<P: PropertyProtocol>(key: Key, path: P.KP) -> P?
@beccadax
Copy link
Contributor

@swift-ci create

@dabrahams
Copy link
Collaborator

I believe this to be another (somewhat simpler) case of the same bug:

protocol Projection {
  associatedtype Root
  associatedtype Value
  associatedtype Path: PartialKeyPath<Root>
{noformat}


{noformat}
  
  var path: Path { get }
}

struct ProjectedCollection<Base: Collection, Part: Projection>
{
  var base: Base
  var part: Part
}

extension ProjectedCollection
  where Part.Root == Base.Element, Part.Path: KeyPath<Part.Root, Part.Value> {
}

The error is

error: 'Part.Path' cannot be a subclass of both 'KeyPath<Part.Root, Part.Value>' and 'PartialKeyPath<Part.Root>'

@slavapestov
Copy link
Member

Seems to have been fixed on the 5.5 branch. I'll add a regression test.

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

No branches or pull requests

4 participants