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-7324] "Overriding" associated types in protocol extensions is dangerous, source-breaking and likely meaningless. #49872

Open
AnthonyLatsis opened this issue Mar 31, 2018 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@AnthonyLatsis
Copy link
Collaborator

Previous ID SR-7324
Radar None
Original Reporter @AnthonyLatsis
Type Bug
Environment

Xcode Version 9.2 (9C40b)

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 85ce0f52f6ac8fa0af9e0c00aab5db93

Issue Description:

I described the issue in [this\|https://forums.swift.org/t/source-breaking-ability-to-override-associated-type-in-protocol-extension/11538] thread.

Briefly,
* The ability to declare type aliases that share names with associated types in protocol extensions results in broken conformances and modified protocols down the whole protocol hierarchy. (All further occurrences of the associated type in the protocol hierarchy will refer to the {{typealias}} instead).

* A similar situation happens when declaring a {{typealias}} in an extension of a type that conforms to a protocol with an equally named and same-type constrained {{associatedtype}}

Code snippets, respectively

protocol P {
    associatedtype E = String
    
    func foo1(_ arg: E)
    func foo2(_ arg: E)
}
 
protocol P1: P \{...} // (*) See bottom line
 
class Foo: P {
    func foo1(_ arg: E) {}
    func foo2(_ arg: E) {}
}
 
extension P \{ typealias E = Bool } // This will break Foo...
// ...and any conformances down the whole protocol hierarchy
 
// (*) All further occurrences of E will refer to the typealias instead.
protocol P {
    associatedtype A = String
    
    func foo(_ arg: A)
}
 
class Foo: P {
    func foo(_ arg: A) {}
}
 
extension Foo {
    typealias A = Bool
}
// No redeclaration error and conformance is broken
@AnthonyLatsis
Copy link
Collaborator Author

@belkadan It looks like a serious bug or, rather, imperfection to me, but maybe it would be more appropriate to pitch this on 'evolution'?

@belkadan
Copy link
Contributor

belkadan commented Apr 2, 2018

That's definitely odd that just declaring P1 makes things different. @huonw, @DougGregor?

@swift-ci
Copy link
Collaborator

Comment by Hack Saw (JIRA)

Is it just me, or is this issue written in Japanese?

@AnthonyLatsis
Copy link
Collaborator Author

usagi did this for no apparent reason.. The funny thing is it looks like he translated the web page and then pasted that in rather than doing so thoughtfully.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

3 participants