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-10076] [Swift 5] Generic parameter cannot get correct type with generic override constraint #52478

Closed
swift-ci opened this issue Mar 10, 2019 · 2 comments

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Mar 10, 2019

Previous ID SR-10076
Radar None
Original Reporter andyl (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Error is reproduced with macOS 10.14.3 with Xcode 10.2 beta 4 (Swift 5) and does not happen with Xcode 10.1 (Swift 4.2)

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, 5.0Regression
Assignee @slavapestov
Priority Medium

md5: fc407436e0f058c43c437612c34564ad

duplicates:

  • SR-4206 Override checking does not properly enforce requirements

Issue Description:

In Swift 5, when we override a method that takes in a generic constraint that adds on another generic constraint, when we call the methods from subclass objects with superclass type. An example snippet is provided below to show this behaviour.

I was able to reproduce this with the latest Swift 5 beta from Xcode 10.2 beta 4. This behaviour does not happen in Swift 4.

class A {}
class SubA<T>: A {}

class B {
  func test<E: A>(_: E) {}
}
class SubB<T>: B {
  override func test<E>(_: E) where E: SubA<T> {
    print(T.self)
  }
}

let subA = SubA<Int>(), subB = SubB<Int>()
let a: A = subA, b: B = subB

subB.test(subA) // works, prints Int
b.test(subA) // works, prints Int
b.test(a) // fails, prints Swift._SwiftObject (instead of Int)
@belkadan
Copy link
Contributor

@belkadan belkadan commented Mar 14, 2019

That shouldn't even build; it's not a valid override. An override always has to be able to accept anything that the original method can accept.

cc @slavapestov

@slavapestov
Copy link
Member

@slavapestov slavapestov commented Mar 14, 2019

I have several dupes of this. We need to check generic constraints when checking for a valid override; in general we should not allow adding new constraints via a 'where' clause. I'll consolidate the dupes later.

@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