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-6645] EXC_BAD_ACCESS in code using protocol extension for defining default implementation #49194

Closed
swift-ci opened this issue Dec 19, 2017 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Collaborator

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

XCode 9.2 (9C40b), Swift 4, MacOS Sierra 10.12.6

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

md5: f7d4aff017dfdc868f6ea443891309f9

duplicates:

  • SR-55 non-@objc protocol existentials do not conform to their own protocol type

Issue Description:

The following playground sample results in a crash (EXC_BAD_ACCESS) while it should work perfectly fine as far as I can tell:

public protocol Observable: class {
    associatedtype Observer: AnyObject
    var observers: [Observer] { get set }
    func addObserver(_ observer: Observer) -> Void
}

public extension Observable {
    public func addObserver(_ observer: Observer) -> Void {
        observers.append(observer)
    }
}

protocol TestObservableListener: class {
    func bla()
}

protocol TestObservable: Observable where Observer == TestObservableListener {
}

class TestObservableImpl: TestObservable {
    typealias Observer = TestObservableListener
    public var observers = [Observer]()
    func broadcast() {
        for observer in observers {
            //2
            observer.bla()
        }
    }
}

class TestObserverImpl: TestObservableListener {
    func bla() {
        print("bla")
    }
}

let observable = TestObservableImpl()
let observer = TestObserverImpl()
observable.addObserver(observer)
//1
observable.broadcast()

It crashes on line marked with 2, when called from 1.

It seems to occur because a protocol extension is defined with a default implementation for the addObserver method.

The full crash:

error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0xffff9f7ffffa0440).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

See also: https://stackoverflow.com/questions/47901032/swift-exc-bad-access-with-default-implementation-defined-in-protocol-extension

@belkadan
Copy link
Contributor

Compiling on master errors with

<stdin>:20:7: error: type 'TestObservableImpl' does not conform to protocol 'Observable'
class TestObservableImpl: TestObservable {
      ^
<stdin>:21:15: note: possibly intended match 'TestObservableImpl.Observer' (aka 'TestObservableListener') does not conform to 'AnyObject'
    typealias Observer = TestObservableListener
              ^

which seems suspect to me: while TestObservableListener as a type doesn't conform to TestObservableListener as a protocol, it should still conform to AnyObject. @slavapestov, am I missing something?

(Still, the error is probably indicating the same problem as the run-time crash in Xcode 9.2.)

@slavapestov
Copy link
Member

This is correct behavior. Existentials of class-bound protocols do not conform to AnyObject – only concrete class types can.

@belkadan
Copy link
Contributor

Maybe we should have a separate bug about diagnostics at this point, since SR-55 is a non-trivial change that likely won't happen any time soon.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 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. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

4 participants