Skip to content

[SR-103] Protocol Extension: function's implementation cannot be overridden by a subclass #42725

@swift-ci

Description

@swift-ci
Previous ID SR-103
Radar rdar://problem/21141185
Original Reporter tarunon (JIRA User)
Type Bug
Environment

Reported: Mac OSX 10.11.1, Swift 2.2-dev

Tested & remains an an issue on:

  • macOS 10.14, Swift 4.2 - 25th September 2018

  • macOS 10.14.1, Swift 5.1 (Xcode 11 beta 5) - 19th August 2019

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

md5: 57cd9bdded34d37cb640b9c376cca165

is duplicated by:

  • SR-118 Protocol extensions cannot be overridden in subclasses
  • SR-1271 Protocol extension method being called over subclass implementation.
  • SR-2119 Protocol methods of superclass should be treated as superclass API in subclasses
  • SR-2859 Cannot override methods from a default protocol extension in a subclass of a class that relies on the default implementation
  • SR-3616 Default implementation being called when specialized one should be picked
  • SR-5654 Class vtables should have entries for default implementations
  • SR-6681 A subclass of a base class which conforms to a protocol with a requirement fulfilled by a default implementation cannot override the base class' method
  • SR-7129 Inconsistent dispatch with protocol extensions
  • SR-8473 Broken protocol/class interaction
  • SR-10443 Dynamic dispatch is sometimes ignored when passing a subclass object to a function that takes a protocol argument
  • SR-11398 Self type does not behave correctly in subclasses of a protcol implementation

Issue Description:

A function(x') is not called that we thought If there is following conditions.

  1. A protocol(A) is defined a function(x).

  2. A has implement of x in the protocol extension.

  3. A class(B) is implement A, but doesn't have implement x.

  4. A subclass(C) is inheritance B, and have implement x(x').

  5. A instance of C typed A, and call x.

// Defined protocol.
protocol A {
    func a() -> Int
}
extension A {
    func a() -> Int {
        return 0
    }
}

// A class doesn't have implement of the function.
class B: A {}

class C: B {
    func a() -> Int {
        return 1
    }
}

// A class has implement of the function.
class D: A {
    func a() -> Int {
        return 1
    }
}

class E: D {
    override func a() -> Int {
        return 2
    }
}

// Failure cases.
B().a() // 0
C().a() // 1
(C() as A).a() // 0 # We thought return 1. 

// Success cases.
D().a() // 1
(D() as A).a() // 1
E().a() // 2
(E() as A).a() // 2

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions