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-10713] The dynamic nature of a protocol's Self type isn't checked when equated with another associated type #53110

hamishknight opened this issue May 17, 2019 · 5 comments


Copy link

@hamishknight hamishknight commented May 17, 2019

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

Swift version 5.1-dev (LLVM 082dec2e22, Swift 397965d3fc)
Target: x86_64-apple-darwin18.5.0

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

md5: 68e52fe5d63fd695a13a186f306cb4bc

Issue Description:

The following unsound code is allowed to compile, which lets us turn a subclass instance into a superclass instance:

protocol P {
  associatedtype X where X == Self
  func foo() -> X

class C : P {
  typealias X = C
  func foo() -> X {
    return C()
class D : C {}

func foo<T : P>(_ x: inout T) {
  x =

var d = D()
print(type(of: d)) // C

When checking the constraint X == Self, we should treat Self as being the dynamic self and therefore reject the conformance.

Copy link

@belkadan belkadan commented May 20, 2019

cc @slavapestov, @DougGregor

Copy link

@slavapestov slavapestov commented May 20, 2019


Copy link

@slavapestov slavapestov commented May 20, 2019

I think the only safe thing to do here is to not allow a class to conform to P at all.

Copy link

@belkadan belkadan commented May 20, 2019

Or only final classes, or something.

Copy link

@slavapestov slavapestov commented Feb 25, 2022


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

No branches or pull requests

3 participants