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-4986] Overriding of a method with a constrained generic placeholder is incorrectly permitted #47563

Closed
hamishknight opened this issue May 24, 2017 · 1 comment
Labels
accepts invalid Bug: Accepts invalid bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-4986
Radar rdar://problem/32378463
Original Reporter @hamishknight
Type Bug
Status Resolved
Resolution Duplicate
Environment

Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42)
Apple Swift version 4.0-dev (LLVM 4a2b8e688d, Clang 20de1703ca, Swift aa5754b)

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

md5: 830bfba804646fa9111fcb97df051aca

duplicates:

  • SR-4206 Override checking does not properly enforce requirements

Issue Description:

The following compiles:

protocol Food {}

class Meat: Food {
    var a = 0, b = 0, c = 0, d = 0
}

class Cake: Food {}

class Person {
    func eat<T : Food>(_: T) {
        print("eat food")
    }
}

class Man : Person {
    override func eat<T : Meat>(_ m: T) {
        print("eat meat", m.d) // EXC_BAD_ACCESS
    }
}

let man: Person = Man()
man.eat(Cake())

This shouldn't compile, because you should not be able to override the method eat<T : Food>(_: T) with eat<T : Meat>(_: T), as Meat is a subtype of Food.

Same goes for subclasses:

class A {
    var a = 1
}

class B : A {
    var b = 2
}

class Person {
    func eat<T : A>(_ : T) {
        print("eat A")
    }
}

class Man : Person {
    override func eat<T : B>(_ t: T) {
        print("eat B", t.b) // EXC_BAD_ACCESS
    }
}

let man: Person = Man()
man.eat(A())
@belkadan
Copy link
Contributor

@swift-ci create

@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
accepts invalid Bug: Accepts invalid 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

2 participants