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-4206] Override checking does not properly enforce requirements #46789

Closed
swift-ci opened this issue Mar 9, 2017 · 3 comments
Closed

[SR-4206] Override checking does not properly enforce requirements #46789

swift-ci opened this issue Mar 9, 2017 · 3 comments

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Mar 9, 2017

Previous ID SR-4206
Radar rdar://problem/23626260
Original Reporter Pr0Ger (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Swift Ver. 3.0.2

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

md5: 09af3d026b3ca16963a2c72d8d00ef63

is duplicated by:

  • SR-4986 Overriding of a method with a constrained generic placeholder is incorrectly permitted
  • SR-7573 Overridden class function with generic constraint causes unsafe behavior
  • SR-10076 [Swift 5] Generic parameter cannot get correct type with generic override constraint
  • SR-10198 Cannot override more than one superclass declaration
  • SR-10603 Overloading Overriden Method By Generic Constraint-ness Causes Compiler Error
  • SR-11904 EXC_BAD_ACCESS when calling overridden generic function as parent's one.
  • SR-11098 swiftc segfault when compiling in release mode

relates to:

  • SR-10582 EXC_BAD_ACCESS with protocol
  • SR-10603 Overloading Overriden Method By Generic Constraint-ness Causes Compiler Error

Issue Description:

protocol Parent {}

protocol A: Parent {}
protocol B: Parent {}

struct Foo: A {}

class ClassA {
    func foo<T: A>(param: T) {
        print("A")
    }
}

class ClassB: ClassA {
    override func foo<T: A>(param: T) {
        print("A")
    }

    override func foo<T: B>(param: T) {
        print("B")
    }

}

let foo = Foo()
let classB = ClassB()
classB.foo(param: foo)

Expected result: "A" will be printed
Actual result: "B" is printed

If we remove foo<T: A> overload compiler will complain argument type 'Foo' does not conform to expected type 'B'.

@belkadan
Copy link
Contributor

@belkadan belkadan commented Mar 10, 2017

This is "fixed" in Swift 3.1:

<stdin>:19:19: error: 'foo(param:)' has already been overridden
    override func foo<T: B>(param: T) {
                  ^
<stdin>:15:19: note: 'foo(param:)' previously overridden here
    override func foo<T: A>(param: T) {
                  ^

but if you take out the first override the compiler incorrectly accepts the code:

protocol Parent {}

protocol A: Parent {}
protocol B: Parent {}

class ClassA {
    func foo<T: A>(param: T) {
        print("A")
    }
}

class ClassB: ClassA {
    override func foo<T: B>(param: T) {
        print("B")
    }
}

@belkadan
Copy link
Contributor

@belkadan belkadan commented Mar 10, 2017

@swift-ci create

@slavapestov
Copy link
Member

@slavapestov slavapestov commented Jul 16, 2019

#24484

@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