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-8713] Protocol extension methods invoked from non-final classes are type-checked incorrectly #51225

Closed
jckarter opened this issue Sep 7, 2018 · 3 comments
Labels
bug A deviation from expected or documented behavior Compiler TypeChecker

Comments

@jckarter
Copy link
Member

jckarter commented Sep 7, 2018

Previous ID SR-8713
Radar rdar://problem/44228594
Original Reporter @jckarter
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 027eff7f9618cd5c78a13cf89a0cfa28

Issue Description:

The following code type checks, but is unsound and should be rejected:

protocol P {}
extension P {
  init(_with instance: Self) {
    self = instance
  }
}
class Animal : P {
  init() {}
  convenience init(with instance: Animal) {
    self.init(_with: instance)
  }
}
class Cat : Animal {
  override init() {}
}
class Dog : Animal {
  override init() {}
}

let cat: Cat = Cat(with: Dog()) // Dog with Cat type???

Specifically, inside Animal.init(with: ) the call self.init(_with: instance) should be invoking the protocol extension method P.init(_with: ) with /*protocol*/ Self == /*dynamic*/ Self, but the type checker checks it as if Self == Animal, allowing the initializer to unsoundly initialize self with a different Animal type from the dynamic Self it was invoked on.

@jckarter
Copy link
Member Author

jckarter commented Sep 7, 2018

@swift-ci create

@jckarter
Copy link
Member Author

jckarter commented Sep 7, 2018

@slavapestov notes that this is already fixed if you use -swift-version 5. Maybe we should warn in Swift 4 mode still.

@slavapestov
Copy link
Member

slavapestov commented Sep 7, 2018

#11637

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

No branches or pull requests

2 participants