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-1581] Can not use protocol to fulfill associatedtype requirement where associatedType has protocol constraint #44190

Closed
swift-ci opened this issue May 21, 2016 · 2 comments

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented May 21, 2016

Previous ID SR-1581
Radar None
Original Reporter vishal.panwar (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 10
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: d2a9b745ccd1923da6ccec087b0df3f9

duplicates:

  • SR-55 non-@objc protocol existentials do not conform to their own protocol type

is duplicated by:

  • SR-11340 Compilation error when there shouldn't be one

relates to:

  • SR-55 non-@objc protocol existentials do not conform to their own protocol type
  • SR-2020 Protocol composition doesn't conform to associated type requirement

Issue Description:

The following code sample does not compile.

protocol Bar {   
}

protocol Buz: Bar {   
}

protocol Foo {
    associatedtype SomeType: Bar
    func someOperation(someType: SomeType)
}

struct Quack: Foo {
    func someOperation(someType: Buz) {
    }
}

It throws error:

Inferred type 'Buz' (by matching requirement 'someOperation') is invalid: does not conform to 'Bar'

I am not sure if we are only allowed to use a concrete type for associatedtype with protocol constraint. I don't see any reason for this restriction. If we remove the protocol constraint from associatedtype, then it compiles with no error.

Update 2016/12/12: I am using below workaround to tackle above bug. This seems more logical and makes me feel like its not a bug.

struct Quack<T>: Foo where T: Buz {
    func someOperation(someType: T) {
    }
}

// Usage
struct SomeBuz: Buz {}
let quack: Quack<SomeBuz> = Quack<SomeBuz>()
quack.someOperation(SomeBuz())

The reason this makes more sense to me is that the compiler now has more context on the actual type.

@belkadan
Copy link
Contributor

@belkadan belkadan commented May 23, 2016

Protocols don't always "conform to themselves", but this configuration seems safe. @jckarter?

@karwa
Copy link
Contributor

@karwa karwa commented Jun 28, 2016

+1 for this. The thing that we want to express is safe; if there are nuances with the current syntax we should devise a way to make it possible.

protocol A {}
protocol SubA : A {}

protocol B { associatedtype TypeOfA : A }

struct MyB : B {     // ERROR: `MyB` does not conform to `B`
    typealias TypeOfA = SubA
}

@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
Projects
None yet
Development

No branches or pull requests

3 participants